首页 > 代码库 > SPOJ 962 Intergalactic Map
SPOJ 962 Intergalactic Map
Intergalactic Map
64-bit integer IO format: %lld Java class name: Main
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 200000;18 struct arc{19 int to,flow,next;20 arc(int x = 0,int y = 0,int z = -1){21 to = x;22 flow = y;23 next = z;24 }25 };26 arc e[maxn*10];27 int head[maxn],d[maxn],cur[maxn];28 int tot,S,T,n,m;29 void add(int u,int v,int flow){30 e[tot] = arc(v,flow,head[u]);31 head[u] = tot++;32 e[tot] = arc(u,0,head[v]);33 head[v] = tot++;34 }35 int q[maxn],hd,tl;36 bool bfs(){37 hd = tl = 0;38 memset(d,-1,sizeof(d));39 q[tl++] = S;40 d[S] = 1;41 while(hd < tl){42 int u = q[hd++];43 for(int i = head[u]; ~i; i = e[i].next){44 if(e[i].flow && d[e[i].to] == -1){45 d[e[i].to] = d[u] + 1;46 q[tl++] = e[i].to;47 }48 }49 }50 return d[T] > -1;51 }52 int dfs(int u,int low){53 if(u == T) return low;54 int tmp = 0,a;55 for(int &i = cur[u]; ~i; i = e[i].next){56 if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(e[i].flow,low)))){57 e[i].flow -= a;58 e[i^1].flow += a;59 tmp += a;60 low -= a;61 if(!low) break;62 }63 }64 if(!tmp) d[u] = -1;65 return tmp;66 }67 int dinic(){68 int ans = 0;69 while(bfs()){70 memcpy(cur,head,sizeof(head));71 ans += dfs(S,INF);72 }73 return ans;74 }75 int main() {76 int cs,u,v;77 scanf("%d",&cs);78 while(cs--){79 scanf("%d %d",&n,&m);80 memset(head,-1,sizeof(head));81 S = tot = 0;82 T = n<<1|1;83 for(int i = 0; i < m; ++i){84 scanf("%d %d",&u,&v);85 if(u > n || v > n) continue;86 add(u+n,v,1);87 add(v+n,u,1);88 }89 for(int i = 1; i <= n; ++i) add(i,i+n,1);90 add(1+n,T,1);91 add(3+n,T,1);92 add(S,2+n,2);//注意细节啊93 printf("%s\n",dinic() == 2?"YES":"NO");94 }95 return 0;96 }
SPOJ 962 Intergalactic Map