Cycle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 131 Accepted Submission(s): 39
Problem Description Ery is interested in graph theory, today he ask BrotherK a problem about it: Given you a undirected graph with N vertexes and M edges, you can select a vertex as your starting point, then you need to walk in the graph along edges. However, you can't pass a edge more than once, even opposite direction is forbidden. At the end, you should come back to the starting point. Assume you has passed X edges, there are two questions:
Question 1: Can X be a odd number ?
Question 2: Can X be a even number ?
(note: you must walk, so X can't be 0)
Input The first line contains a single integer T, indicating the number of test cases.
Each test case begins with two integer N, M, indicating the number of vertexes and the number of edges. Following M lines, each line contains two integers Ui, Vi, indicating there are a edge between vertex Ui and vertex Vi.
T is about 30
1 ≤ N ≤ 100000
0 ≤ M ≤ 300000
1 ≤ Ui,Vi ≤ N
Ui will not equal to Vi
There is at most one edge between any pair of vertex.
Output For each test, print two lines.
The first line contains "YES" or "NO" for question 1.
The second line contains "YES" or "NO" for question 2.
Sample Input
3 1 0 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
Sample Output
NO NO YES NO NO YES Hint If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
Source 賽碼"BestCoder"杯中國大學生程式設計冠軍賽
題目串連 http://acm.hdu.edu.cn/showproblem.php?pid=5215
題意:給你一個無向圖,從一個點出發然後回到該點,且每條邊只能走一次,問是否存在奇環和偶環。
思路:判斷奇環,可以用dfs染色判斷是不是二分圖,二分圖中必定不存在奇環,而非二分圖中必定存在奇環。
判斷偶環,可以找出該圖的雙連通分量是否只存在一個單獨的奇環,否則必定存在偶環,因為由多個環組成的雙連通分量必定存在偶環。
代碼:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>#include<queue>#include<cmath>#include<stack>#include<set>#include<map>#define INF 0xfffffff#define MAX 1000005#define mod 10000007#define CLR(a,b) memset((a),(b),sizeof((a)))#pragma comment(linker, "/STACK:102400000,102400000")#define ul u<<1#define ur (u<<1)|1using namespace std;typedef long long ll;struct edge { int v,next;} e[MAX*4];int pre[MAX],low[MAX],bcc_cnt=0,dfs_clock=0;int head[MAX],bridge[MAX*4],tot,vis[MAX],color[MAX];void addedge(int u,int v) { e[tot].v=v; e[tot].next=head[u]; head[u]=tot++;}int odd=0,even=0;void dfs(int u) { if(odd) return ; for(int i=head[u]; i!=-1; i=e[i].next) { int v=e[i].v; if(color[v]==-1) { color[v]=(color[u]+1)%2; dfs(v); } else if(color[v]==color[u]) { odd=1; return ; } }}void tdfs(int u,int fa) { pre[u]=low[u]=++dfs_clock; for(int i=head[u]; i!=-1; i=e[i].next) { int v=e[i].v; if(!pre[v]) { tdfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>pre[u]) { bridge[i]=bridge[i^1]=1; } } else if(pre[v]<pre[u]&&v!=fa) low[u]=min(low[u],pre[v]); }}void find_bcc(int n) { CLR(pre,0); CLR(low,0); for(int i=1; i<=n; i++) { if(!pre[i]) { tdfs(i,-i); } }}int v_cnt,e_cnt;void dfs2(int u) { vis[u]=1; v_cnt++; for(int i=head[u]; i!=-1; i=e[i].next) { if(bridge[i]) continue; int v=e[i].v; e_cnt++; if(!vis[v]) { dfs2(v); } }}void init() { tot=0; CLR(head,-1); CLR(color,-1); CLR(vis,0); CLR(bridge,0);}int main() { int t; scanf("%d",&t); while(t--) { init(); int n,m,u,v; scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } odd=even=0; for(int i=1; i<=n; i++) { if(color[i]==-1) { color[i]=0; dfs(i); if(odd) break; } } find_bcc(n); for(int i=1; i<=n; i++) { v_cnt=e_cnt=0; if(!vis[i]) { dfs2(i); }else continue; if(v_cnt==1) continue; e_cnt/=2; if(!(e_cnt==v_cnt&&v_cnt%2==1)) { even=1; break; } } if(odd) printf("YES\n"); else printf("NO\n"); if(even) printf("YES\n"); else printf("NO\n"); } return 0;}
並查集:
將u,v 兩點拆成2u,2u+1,v,2v+1;如果2u和2v+1已經相連說明存在偶環,否則2u與2v+1相連,2u+1與2v相連,每次串連後發現2u和2u+1相連說明存在奇環。
證明:將點拆分然後交叉相連,點u經過奇數次回到u,會導致自己兩個點回到一個集合,如果經過偶數次回到u,這是就會發現,要串連的時候2u,已經和2v+1在同一個集合了。
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>#include<queue>#include<cmath>#include<stack>#include<set>#include<map>#define INF 0xfffffff#define MAX 1000005#define mod 10000007#define CLR(a,b) memset((a),(b),sizeof((a)))#pragma comment(linker, "/STACK:102400000,102400000")#define ul u<<1#define ur (u<<1)|1using namespace std;typedef long long ll;int pre[2*MAX];int find(int r) { while(r!=pre[r]) { r=pre[r]=pre[pre[r]]; } return r;}int even,odd;void add(int u,int v) { int a=find(u); int b=find(v); if(a!=b) { pre[a]=b; }}int main() { int t; scanf("%d",&t); while(t--) { int n,m,u,v; scanf("%d%d",&n,&m); odd=0,even=0; for(int i=1;i<=2*(n+1);i++) pre[i]=i; for(int i=1; i<=m; i++) { scanf("%d%d",&u,&v); if(odd&&even) continue; if(find(2*u)==find(2*v+1)) even=1; add(2*u,2*v+1); add(2*u+1,2*v); if(find(u*2)==find(u*2+1)) odd=1; } if(odd) printf("YES\n"); else printf("NO\n"); if(even) printf("YES\n"); else printf("NO\n"); } return 0;}