2set的tarjan写法.....
2 1 0 1 1 1
YES
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=5000; struct Edge { int to,next; }edge[100000]; int Adj[maxn],Size; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } int DFN[maxn],Low[maxn],Instack[maxn],Belong[maxn]; int top,Stack[maxn],Index,scc; void Tarjan(int u) { int v; DFN[u]=Low[u]=++Index; Stack[top++]=u; Instack[u]=1; for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(!DFN[v]) { Tarjan(v); Low[u]=min(Low[u],Low[v]); } else if(Instack[v]) { Low[u]=min(Low[u],DFN[v]); } } if(Low[u]==DFN[u]) { scc++; do { v=Stack[--top]; Instack[v]=false; Belong[v]=scc; }while(u!=v); } } bool solve_scc(int n) { memset(DFN,0,sizeof(DFN)); memset(Instack,0,sizeof(Instack)); memset(Belong,0,sizeof(Belong)); top=Index=scc=0; for(int i=0;i<2*n;i++) { if(!DFN[i]) Tarjan(i); } for(int i=0;i<n;i++) { if(Belong[i<<1]==Belong[i<<1|1]) return false; } return true; } int main() { int n,m,a1,a2,c1,c2; while(scanf("%d%d",&n,&m)!=EOF) { init(); while(m--) { scanf("%d%d%d%d",&a1,&a2,&c1,&c2); Add_Edge((a1<<1)+c1,(a2<<1|1)-c2); Add_Edge((a2<<1)+c2,(a1<<1|1)-c1); } if(solve_scc(n)) puts("YES"); else puts("NO"); } return 0; }