一条边<u,v>表示u选那么v一定被选。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxm=21000; 7 const int Maxn=2010; 8 struct EDGE{int to,next;}edge[Maxm]; 9 int T,m,Stack[Maxn],head[Maxn],Belong[Maxn],Id[Maxn],Dfn[Maxn],Low[Maxn],h1,h2,h,Stamp,scc,top,cnt; 10 bool vis[Maxn]; 11 inline int Min(int x,int y) {return x>y?y:x;} 12 inline void Add(int u,int v){edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;} 13 inline bool Check() 14 { 15 for (int i=0;i<T;i++) if (Belong[i<<1]==Belong[i<<1|1]) return false; 16 return true; 17 } 18 void Tarjan(int u) 19 { 20 Stack[++top]=u; vis[u]=true; 21 Low[u]=Dfn[u]=++Stamp; 22 for (int i=head[u];i!=-1;i=edge[i].next) 23 if (Dfn[edge[i].to]==-1) Tarjan(edge[i].to),Low[u]=Min(Low[u],Low[edge[i].to]); else 24 if (vis[edge[i].to]) Low[u]=Min(Low[u],Dfn[edge[i].to]); 25 if (Dfn[u]==Low[u]) 26 { 27 while (true) 28 { 29 int v=Stack[top--]; 30 Belong[v]=scc; 31 vis[v]=false; 32 if (u==v) break; 33 } 34 scc++; 35 } 36 } 37 38 int main() 39 { 40 41 while (scanf("%d%d",&T,&m)!=EOF) 42 { 43 for (int i=0;i<T;i++) 44 { 45 scanf("%d%d%d",&h,&h1,&h2); 46 Id[h]=i<<1; 47 Id[h1]=Id[h2]=i<<1|1; 48 } 49 memset(head,-1,sizeof(head)); 50 memset(Dfn,-1,sizeof(Dfn)); 51 cnt=Stamp=top=0; 52 for (int i=1;i<=m;i++) 53 { 54 scanf("%d%d",&h1,&h2); 55 Add(Id[h1],Id[h2]^1),Add(Id[h2],Id[h1]^1); 56 } 57 58 for (int i=0;i<T*2;i++) if (Dfn[i]==-1) Tarjan(i); 59 puts(Check()?"yes":"no"); 60 } 61 return 0; 62 }
HDU 1824
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxm=1000100; 7 const int Maxn=2010; 8 struct EDGE{int to,next;}edge[Maxm]; 9 int head[Maxn],Dfn[Maxn],Low[Maxn],Belong[Maxn],scc,Stk[Maxn],top,cnt,Stamp,n,m; 10 int p1,p2,c1,c2; 11 bool vis[Maxn]; 12 inline int Min(int x,int y) {return x>y?y:x;} 13 inline int Id(int num,int pos){return (num<<1)+pos+1;} 14 inline void Add(int u,int v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;} 15 inline bool Check() 16 { 17 for (int i=0;i<n;i++) if (Belong[Id(i,0)]==Belong[Id(i,1)]) return false; 18 return true; 19 } 20 void Tarjan(int u) 21 { 22 Stk[++top]=u; vis[u]=true; 23 Dfn[u]=Low[u]=++Stamp; 24 for (int i=head[u];i!=-1;i=edge[i].next) 25 if (Dfn[edge[i].to]==-1) Tarjan(edge[i].to),Low[u]=Min(Low[u],Low[edge[i].to]); else 26 if (vis[edge[i].to]) Low[u]=Min(Low[u],Dfn[edge[i].to]); 27 if (Low[u]==Dfn[u]) 28 { 29 while (true) 30 { 31 int v=Stk[top--]; 32 vis[v]=false; 33 Belong[v]=scc; 34 if (v==u) break; 35 } 36 scc++; 37 } 38 } 39 int main() 40 { 41 while (scanf("%d%d",&n,&m)!=EOF) 42 { 43 memset(head,-1,sizeof(head)); 44 memset(Dfn,-1,sizeof(Dfn)); 45 memset(Belong,0,sizeof(Belong)); 46 cnt=top=scc=0; 47 for (int i=1;i<=m;i++) 48 { 49 scanf("%d%d%d%d",&p1,&p2,&c1,&c2); 50 Add(Id(p1,c1),Id(p2,1-c2)),Add(Id(p2,c2),Id(p1,1-c1)); 51 } 52 for (int i=1;i<=2*n;i++) if (Dfn[i]==-1) Tarjan(i); 53 puts(Check()?"YES":"NO"); 54 } 55 return 0; 56 }
HDU 3062
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxn=2010; 7 const int Maxm=4010000; 8 int One[Maxn],Zero[Maxn],Stack[Maxn],Belong[Maxn],head[Maxn],Low[Maxn],Dfn[Maxn],n,m,u,v,w,cnt,Stamp,scc,top; 9 bool vis[Maxn]; 10 char ch[20]; 11 struct EDGE{int to,next;}edge[Maxm]; 12 inline int Min(int x,int y){return x>y?y:x;} 13 inline void Add(int u,int v){edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;} 14 inline bool Check() 15 { 16 for (int i=0;i<n;i++) if (Belong[One[i]]==Belong[Zero[i]]) return false; 17 return true; 18 } 19 void Tarjan(int u) 20 { 21 Stack[++top]=u; vis[u]=true; 22 Low[u]=Dfn[u]=++Stamp; 23 for (int i=head[u];i!=-1;i=edge[i].next) 24 if (Dfn[edge[i].to]==-1) Tarjan(edge[i].to),Low[u]=Min(Low[u],Low[edge[i].to]); else 25 if (vis[edge[i].to]) Low[u]=Min(Low[u],Dfn[edge[i].to]); 26 if (Low[u]==Dfn[u]) 27 { 28 while (true) 29 { 30 int v=Stack[top--]; 31 vis[v]=false; 32 Belong[v]=scc; 33 if (u==v) break; 34 } 35 scc++; 36 } 37 } 38 int main() 39 { 40 scanf("%d%d",&n,&m); 41 for (int i=0;i<n;i++) One[i]=i<<1,Zero[i]=i<<1|1; 42 memset(head,-1,sizeof(head)); 43 memset(Dfn,-1,sizeof(Dfn)); 44 for (int i=1;i<=m;i++) 45 { 46 scanf("%d%d%d%s",&u,&v,&w,ch); 47 if (ch[0]==‘A‘) 48 { 49 if (w==1) Add(Zero[u],One[u]),Add(Zero[v],One[v]); 50 if (w==0) Add(One[u],Zero[v]),Add(One[v],Zero[u]); 51 } 52 if (ch[0]==‘O‘) 53 { 54 if (w==1) Add(Zero[u],One[v]),Add(Zero[v],One[u]); 55 if (w==0) Add(One[v],Zero[v]),Add(One[u],Zero[u]); 56 } 57 if (ch[0]==‘X‘) 58 { 59 if (w==1) Add(One[u],Zero[v]),Add(Zero[u],One[v]),Add(Zero[v],One[u]),Add(One[v],Zero[u]); 60 if (w==0) Add(One[u],One[v]),Add(One[v],One[u]),Add(Zero[u],Zero[v]),Add(Zero[v],Zero[u]); 61 } 62 } 63 64 for (int i=0;i<n<<1;i++) if (Dfn[i]==-1) Tarjan(i); 65 puts(Check()?"YES":"NO"); 66 return 0; 67 }
POJ 3578
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int Maxm=80100; 7 int Stk[Maxm],Low[Maxm],Dfn[Maxm],head[Maxm],Belong[Maxm],In[Maxm],Out[Maxm],Pos[Maxm]; 8 int cnt,top,Stamp,KASE,scc,n,m,x,u[Maxm],v[Maxm],tot; 9 bool vis[Maxm]; 10 struct EDGE{int to,next;}edge[Maxm<<4]; 11 inline int Min(int x,int y) {return x>y?y:x;} 12 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;} 13 inline void Add(int u,int v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;} 14 inline bool Check() 15 { 16 for (int i=1;i<=m;i++) if (Belong[In[i]]==Belong[Out[i]]) return false; 17 return true; 18 } 19 void Tarjan(int u) 20 { 21 Stk[++top]=u; vis[u]=true; 22 Low[u]=Dfn[u]=++Stamp; 23 for (int i=head[u];i!=-1;i=edge[i].next) 24 if (Dfn[edge[i].to]==-1) Tarjan(edge[i].to),Low[u]=Min(Low[u],Low[edge[i].to]); else 25 if (vis[edge[i].to]) Low[u]=Min(Low[u],Dfn[edge[i].to]); 26 if (Low[u]==Dfn[u]) 27 { 28 while (true) 29 { 30 int v=Stk[top--]; 31 vis[v]=false; 32 Belong[v]=scc; 33 if (u==v) break; 34 } 35 scc++; 36 } 37 } 38 int main() 39 { 40 scanf("%d",&KASE); 41 for (int Kase=1;Kase<=KASE;Kase++) 42 { 43 scanf("%d%d",&n,&m); 44 for (int i=1;i<=m;i++) scanf("%d%d",&u[i],&v[i]); 45 for (int i=1;i<=n;i++) scanf("%d",&x),Pos[x]=i; 46 if(m>3*n-6){puts("NO");continue;} 47 for (int i=0;i<m;i++) In[i+1]=i<<1,Out[i+1]=i<<1|1; 48 tot=0; 49 for (int i=1;i<=m;i++) 50 { 51 u[i]=Pos[u[i]],v[i]=Pos[v[i]]; 52 if (v[i]<u[i]) Swap(u[i],v[i]); 53 if ((v[i]-u[i]==1) || (v[i]==n && u[i]==1)) continue; 54 u[++tot]=u[i],v[tot]=v[i]; 55 } 56 m=tot; 57 memset(Dfn,-1,sizeof(Dfn)); 58 memset(head,-1,sizeof(head)); 59 memset(vis,false,sizeof(vis)); 60 Stamp=cnt=top=scc=0; 61 62 for (int i=1;i<=m;i++) 63 for (int j=i+1;j<=m;j++) 64 if (((u[i]<u[j] && u[j]<v[i] && v[i]<v[j]) || (u[j]<u[i] && u[i]<v[j] && v[j]<v[i]))) 65 Add(In[i],Out[j]),Add(In[j],Out[i]),Add(Out[i],In[j]),Add(Out[j],In[i]); 66 67 for (int i=0;i<m<<1;i++) if (Dfn[i]==-1) Tarjan(i); 68 puts(Check()?"YES":"NO"); 69 } 70 return 0; 71 } 72
BZOJ 1997
时间: 2024-10-25 11:30:32