标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2853 Accepted Submission(s): 901
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <stack> #include <vector> #include <algorithm> struct edge{ int st; int to; int next; }; const int inf = 0x3f3f3f; const int MAXN = 8e3+10; const int MAXNN = 2e4+10; using namespace std; int first[2*MAXN]; edge e[2*MAXNN]; int color[2*MAXN]; int S[2*MAXN]; int top,l; int n,m; int flag; void init(){ l = 0; memset(first,-1,sizeof(first)); memset(color,0,sizeof(color)); flag= 1; } void addEdge(int u,int v){ e[l].st = u; e[l].to = v; e[l].next = first[u]; first[u] = l++; } int dfs(int v){ if(color[v]==2)return 0; //如果深搜到已经染色为2的,不成立 if(color[v]==1)return 1; color[v] = 1; //本节点染色1 color[v^1] = 2; //相邻的染色2 S[top++] = v; for(int i=first[v];i!=-1;i=e[i].next){ if(!dfs(e[i].to))return 0; } return 1; } int main() { int a,b; while(scanf("%d%d",&n,&m)!=EOF){ init(); for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); a--; b--; addEdge(a,b^1); addEdge(b,a^1); } int k; for(int i=0;i<2*n;i+=2){ if(!color[i]){ top = 0; if(!dfs(i)){ while(top){ k = S[--top]; color[k] = color[k^1] = 0; } if(!dfs(i+1)){ //2*i-1节点不能选择,必然要选择2*i节点,如果还不满足,说明no solution flag = 0; break; } } } } if(!flag){ cout<<"NIE"<<endl; continue; } for(int i=0;i<2*n;i++){ if(color[i]==1){ cout<<i+1<<endl; } } } //cout << "Hello world!" << endl; return 0; } /* 6 6 1 3 4 6 5 7 9 11 10 11 5 12 */
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5468881.html