模板题啦 暴力dfs的理论复杂度据说是
O(nm)
的 但是表现很优越嘛
这个跟tarjan比有个好 就是能求字典序最小解
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
const int N=20005;
struct edge{
int u,v,next;
}G[N<<1];
int head[N],inum;
inline void add(int u,int v,int p){
G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
int vst[N];
int pnt,Stack[N];
#define V G[p].v
inline int dfs(int u){
if (vst[u^1]) return 0;
if (vst[u]) return 1;
Stack[++pnt]=u; vst[u]=1;
for (int p=head[u];p;p=G[p].next)
if (!dfs(V))
return 0;
return 1;
}
int n,m;
inline int Solve(){
cl(vst);
for(int i=0;i<2*n;i+=2)
if (!vst[i] && !vst[i^1]){
pnt=0;
if (!dfs(i)){
while (pnt) vst[Stack[pnt--]]=0;
if (!dfs(i^1)) return 0;
}
}
return 1;
}
int main(){
int iu,iv;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
while (~scanf("%d%d",&n,&m)){
for(int i=1;i<=m;i++)
scanf("%d %d",&iu,&iv),iu--,iv--,add(iu,iv^1,++inum),add(iv,iu^1,++inum);
if (Solve())
for(int i=0;i<2*n;i+=2)
printf("%d\n",vst[i]?i+1:i+2);
else
printf("NIE\n");
cl(head); inum=0;
}
return 0;
}