题目大意:有(1,2),(3,4), ...(2n-1,2n),n对数,要在每对中选出一个共n个,并且满足m个约束条件,条件(a,b)表示ab不能同时被选,按字典序输出答案;
题目解析:如果有a b有约束,那么建边A->B^1,B->A^1,字典序最后直接遍历输出;
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int maxn=8010*2;
const int maxm = 20010;
int first[maxn];
int vv[maxm*4],nxt[maxm*4],S[maxm*4];
int e,c,n,m;
bool vis[maxn];
void addedge(int u,int v)
{
vv[e] = v; nxt[e] = first[u]; first[u] = e++;
}
bool dfs(int u)
{
if(vis[u^1]) return 0;
if(vis[u]) return 1;
vis[u] = 1;
S[c++] = u;
for(int i = first[u];i != -1;i = nxt[i])
{
int v = vv[i];
if(!dfs(v)) return false;
}
return true;
}
bool Judge()
{
for(int i = 0;i < n*2;i+=2)
{
if(!vis[i] && !vis[i+1])
{
c = 0;
if(!dfs(i))
{
while(c > 0) vis[S[--c]] = 0;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
memset(first,-1,sizeof(first));
e=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
u--;
v--;
addedge(u,v^1);
addedge(v,u^1);
}
if(Judge())
{
for(int i=0;i<n;i++)
{
if(vis[2*i+1]==1)
printf("%d\n",i*2+2);
else
printf("%d\n",i*2+1);
}
}
else
printf("NIE\n");
}
return 0;
}