题解:
2-sat
nm暴力
虽然似乎复杂度最低
代码:
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int N=200005; int num,ne[N],fi[N],n,m,x,y,f[N],zz[N],vis[N],S[N],top; void jb(int x,int y) { ne[++num]=fi[x]; fi[x]=num; zz[num]=y; } int dfs(int x) { if (vis[x^1])return 0; if (vis[x])return 1; vis[x]=1; S[top++]=x; for (int i=fi[x];i;i=ne[i]) if (!dfs(zz[i]))return 0; return 1; } int slove() { memset(vis,0,sizeof vis); for (int i=0;i<2*n;i+=2) { if (vis[i]||vis[i^1])continue; top=0; if (!dfs(i)) { while (top)vis[S[--top]]=0; if (!dfs(i^1))return 0; } } return 1; } int main() { while (~scanf("%d%d",&n,&m)) { memset(fi,0,sizeof fi); num=0; while (m--) { scanf("%d%d",&x,&y); x--;y--; jb(x,y^1);jb(y,x^1); } if (slove()) { for (int i=0;i<2*n;i++) if (vis[i])printf("%d\n",i+1); } else puts("NIE"); } return 0; }