标签:des style class code java tar
2sat.... 队长离开时两个队员必须同时留下,两个队员中只要有人离开队长就得留下...
1 2 0 1 2 0 1 1 2 2 4 0 1 2 3 4 5 0 3 0 4 1 3 1 4
yes no
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=5500; struct Edge { int to,next; }edge[maxn*4]; 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 Low[maxn],DFN[maxn],Belong[maxn],Instack[maxn]; int top,Stack[maxn],Index,scc; void Tarjan(int u) { Low[u]=DFN[u]=++Index; Stack[top++]=u; Instack[u]=true; int v; 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); } } void scc_solve(int n) { memset(DFN,0,sizeof(DFN)); memset(Belong,0,sizeof(Belong)); memset(Instack,0,sizeof(Instack)); top=scc=Index=0; for(int i=0;i<n;i++) { if(!DFN[i]) Tarjan(i); } for(int i=0;i<n;i+=2) { if(Belong[i]==Belong[i^1]) { puts("no"); return ; } } puts("yes"); } int T,M; int main() { while(scanf("%d%d",&T,&M)!=EOF) { int n=T*3*2,a,b,c; init(); for(int i=0;i<T;i++) { scanf("%d%d%d",&a,&b,&c); Add_Edge(a<<1|1,b<<1); Add_Edge(a<<1|1,c<<1); Add_Edge(b<<1|1,a<<1); Add_Edge(c<<1|1,a<<1); } while(M--) { scanf("%d%d",&a,&b); Add_Edge(a<<1,b<<1|1); Add_Edge(b<<1,a<<1|1); } scc_solve(n); } return 0; }
HDOJ 1824 Let's go home,布布扣,bubuko.com
标签:des style class code java tar
原文地址:http://blog.csdn.net/ck_boss/article/details/24832743