标签:
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1523 Accepted Submission(s): 616
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> using namespace std; stack<int> s; vector<int> e[7010]; int n,m,Dfs[7010],use[7010],low[7010],isstack[7010]; int top,newflag; void init() { memset(Dfs,0,sizeof(Dfs)); memset(use,0,sizeof(use)); memset(low,0,sizeof(low)); memset(isstack,0,sizeof(isstack)); top=0,newflag=0; for(int i=1;i<=6*n;i++) e[i].clear(); while(!s.empty()) s.pop(); } void tarjan(int u) { Dfs[u]=low[u]=++top; isstack[u]=1; s.push(u); for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(!Dfs[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(isstack[v]) low[u]=min(low[u],Dfs[v]); } if(Dfs[u]==low[u]) { newflag++; int x; do { x=s.top(); s.pop(); isstack[x]=0; use[x]=newflag; }while(x!=u); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); a++,b++,c++; e[a+3*n].push_back(b); e[a+3*n].push_back(c); e[b+3*n].push_back(a); e[c+3*n].push_back(a); } for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); a++,b++; e[a].push_back(b+3*n); e[b].push_back(a+3*n); } for(int i=1;i<=6*n;i++) { if(!Dfs[i]) tarjan(i); } bool flag=true; for(int i=1;i<=6*n;i++) { if(use[i]==use[i+n]) { flag=false; break; } } if(flag) printf("yes\n"); else printf("no\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/water-full/p/4531578.html