标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5558 Accepted Submission(s): 1809
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxn=5010; 6 int n,m; 7 int cnt,fir[maxn],nxt[maxn*600],to[maxn*600]; 8 void addedge(int a,int b){ 9 nxt[++cnt]=fir[a]; 10 fir[a]=cnt; 11 to[cnt]=b; 12 } 13 int scc[maxn],scnt; 14 int ID[maxn],low[maxn],tot; 15 int st[maxn],top; 16 17 void Tarjan(int x){ 18 ID[x]=low[x]=++tot;st[++top]=x; 19 for(int i=fir[x];i;i=nxt[i]){ 20 if(ID[to[i]]){ 21 if(!scc[to[i]]) 22 low[x]=min(low[x],ID[to[i]]); 23 } 24 else{ 25 Tarjan(to[i]); 26 low[x]=min(low[x],low[to[i]]); 27 } 28 } 29 if(low[x]==ID[x]){ 30 ++scnt; 31 while(true){ 32 int y=st[top--]; 33 scc[y]=scnt; 34 if(x==y)break; 35 } 36 } 37 } 38 bool Solve(){ 39 for(int i=0;i<2*n;i++) 40 if(!ID[i])Tarjan(i); 41 42 for(int i=0;i<n;i++) 43 if(scc[i*2]==scc[i*2+1]) 44 return false; 45 return true; 46 } 47 48 void Init(){ 49 memset(fir,0,sizeof(fir)); 50 memset(scc,0,sizeof(scc)); 51 memset(ID,0,sizeof(ID)); 52 cnt=0;tot=0;scnt=0; 53 } 54 55 int main(){ 56 while(scanf("%d%d",&n,&m)!=EOF){ 57 Init(); 58 while(m--){ 59 int a,b,c,d; 60 scanf("%d%d%d%d",&a,&b,&c,&d); 61 a=a*2+c;b=b*2+d; 62 addedge(a,b^1);addedge(b,a^1); 63 } 64 if(Solve()) 65 printf("YES\n"); 66 else 67 printf("NO\n"); 68 } 69 return 0; 70 }
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5631317.html