标签:style blog http color io os java ar for
2 1 0 1 1 1
YES
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 2010; 18 struct arc{ 19 int u,v,next; 20 arc(int x = 0,int y = 0,int z = 0){ 21 u = x; 22 v = y; 23 next = z; 24 } 25 }; 26 arc e[2000100]; 27 int dfn[maxn],low[maxn],belong[maxn]; 28 bool instack[maxn]; 29 int head[maxn],tot,scc,index,n,m; 30 stack<int>stk; 31 void add(int u,int v){ 32 e[tot] = arc(u,v,head[u]); 33 head[u] = tot++; 34 } 35 void tarjan(int u){ 36 dfn[u] = low[u] = ++index; 37 instack[u] = true; 38 stk.push(u); 39 for(int i = head[u]; ~i; i = e[i].next){ 40 if(!dfn[e[i].v]){ 41 tarjan(e[i].v); 42 if(low[e[i].v] < low[u]) 43 low[u] = low[e[i].v]; 44 }else if(instack[e[i].v] && low[u] > dfn[e[i].v]) 45 low[u] = dfn[e[i].v]; 46 } 47 if(dfn[u] == low[u]){ 48 scc++; 49 int now; 50 do{ 51 now = stk.top(); 52 stk.pop(); 53 belong[now] = scc; 54 instack[now] = false; 55 }while(now != u); 56 } 57 58 } 59 bool solve(){ 60 int k = n<<1; 61 while(!stk.empty()) stk.pop(); 62 for(int i = 0; i < k; i++) 63 if(!dfn[i]) tarjan(i); 64 for(int i = 0; i < n; i++) 65 if(belong[i<<1] == belong[i<<1|1]) return false; 66 return true; 67 } 68 int main() { 69 int u,v,x,y; 70 while(~scanf("%d %d",&n,&m)){ 71 for(int i = 0; i < maxn; i++){ 72 dfn[i] = belong[i] = 0; 73 head[i] = -1; 74 instack[i] = false; 75 } 76 tot = scc = index = 0; 77 for(int i = 0; i < m; i++){ 78 scanf("%d %d %d %d",&x,&y,&u,&v); 79 x = (x<<1)+u; 80 y = (y<<1)+v; 81 add(x,y^1); 82 add(y,x^1); 83 } 84 solve()?puts("YES"):puts("NO"); 85 } 86 return 0; 87 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3969118.html