zoukankan      html  css  js  c++  java
  • [SPOJ962]Intergalactic Map 拆点+最大流

    Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Amidala to save Naboofrom an invasion by the Trade Federation. They must leave Naboo immediately and go to Tatooine to pick up the proof of the Federation’s evil design. They then must proceed on to the Republic’s capital planet Coruscant to produce it in front of the Republic’s Senate. To help them in this endeavor, the queen’s captain provides them with an intergalactic map. This map shows connections between planets not yet blockaded by the Trade Federation. Any pair of planets has at most one connection between them, and all the connections are two-way. To avoid detection by enemy spies, the knights must embark on this adventure without visiting any planet more than once. Can you help them by determining if such a path exists? 

    Note - In the attached map, the desired path is shown in bold.

    Input Description

    The first line of the input is a positive integer t ≤ 20, which is the number of test cases. The descriptions of the test cases follow one after the other. The first line of each test case is a pair of positive integers n, m (separated by a single space). 2 ≤ n ≤ 30011 is the number of planets and m ≤ 50011 is the number of connections between planets. The planets are indexed with integers from 1 to n. The indices of Naboo, Tatooine and Coruscant are 1, 2, 3 respectively. The next m lines contain two integers each, giving pairs of planets that have a connection between them.

    Output Description

    The output should contain t lines. The ith line corresponds to the ith test case. The output for each test case should be YES if the required path exists and NO otherwise.

    Example


    Input
    2
    3 3
    1 2
    2 3
    1 3
    3 1
    1 3

    Output
    YES
    NO

    题目大意:

    求是否存在1->2再从2->3的不重复路径

    题解:

    先拆点 保证每个点只经过一次 (i,i+n,1)。

    然后 (s,2+n,2) (1,T,1)(3,T,1)最后看最大流是否等于2即可

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 using namespace std;
     8 const int N=101011,M=81011,INF=1999999999;
     9 int gi(){
    10     int str=0;char ch=getchar();
    11     while(ch>'9' || ch<'0')ch=getchar();
    12     while(ch>='0' && ch<='9')str=str*10+ch-'0',ch=getchar();
    13     return str;
    14 }
    15 int n,m,num=1,head[N],s=0,T;
    16 struct Lin{
    17     int next,to,dis;
    18 }a[M*4];
    19 void init(int x,int y,int z){
    20     a[++num].next=head[x];
    21     a[num].to=y;
    22     a[num].dis=z;
    23     head[x]=num;
    24     a[++num].next=head[y];
    25     a[num].to=x;
    26     a[num].dis=0;
    27     head[y]=num;
    28 }
    29 int q[N*2],dep[N];
    30 bool bfs()
    31 {
    32     memset(dep,0,sizeof(dep));
    33     q[1]=s;dep[s]=1;int t=0,sum=1,x,u;
    34     while(t!=sum)
    35     {
    36         x=q[++t];
    37         for(int i=head[x];i;i=a[i].next){
    38             u=a[i].to;
    39             if(dep[u]||a[i].dis<=0)continue;
    40             dep[u]=dep[x]+1;q[++sum]=u;
    41         }
    42     }
    43     return dep[T];
    44 }
    45 int dfs(int x,int flow)
    46 {
    47     if(x==T || !flow)return flow;
    48     int u,tmp,sum=0;
    49     for(int i=head[x];i;i=a[i].next){
    50         u=a[i].to;
    51         if(dep[u]!=dep[x]+1 || a[i].dis<=0)continue;
    52         tmp=dfs(u,min(flow,a[i].dis));
    53         a[i].dis-=tmp;a[i^1].dis+=tmp;
    54         sum+=tmp;flow-=tmp;
    55         if(!flow)break;
    56     }
    57     return sum;
    58 }
    59 int maxflow(){
    60     int tmp,tot=0;
    61     while(bfs()){
    62         tmp=dfs(s,INF);
    63         while(tmp)tot+=tmp,tmp=dfs(s,INF);
    64     }
    65     return tot;
    66 }
    67 void work()
    68 {
    69     int x,y;
    70     n=gi();m=gi();T=(n<<1)+1;
    71     for(int i=1;i<=n;i++)init(i,i+n,1);
    72     for(int i=1;i<=m;i++){
    73         x=gi();y=gi();
    74         if(x>n || x<1 || y>n || y<1)continue;
    75         init(x+n,y,1);init(y+n,x,1);
    76     }
    77     init(s,2+n,2);init(1,T,1);init(3,T,1);
    78     int tp=maxflow();
    79     if(tp==2)printf("YES
    ");
    80     else printf("NO
    ");
    81 }
    82 void Clear(){
    83     num=1;
    84     memset(head,0,sizeof(head));
    85 }
    86 int main()
    87 {
    88     int TT=gi();
    89     while(TT--){
    90         work();
    91         Clear();
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    uploadify
    mark down pad2
    yii1.1.3主从(多从)、读写分离配置
    yii多数据库
    Uploadify上传问题
    出现upstream sent too big header while reading response header from upstream错误
    Nginx 启动脚本/重启脚本
    VB6_小林的气象类模块
    进程与线程
    JDK动态代理与CGLib
  • 原文地址:https://www.cnblogs.com/Yuzao/p/6854146.html
Copyright © 2011-2022 走看看