HOJ 1917 POI 2001 Peaceful Commission 2-SAT问题

The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.
The Commission has to fulfill the following conditions:

Each party has exactly one representative in the Commission,
If two deputies do not like each other, they cannot both belong to the Commission.
Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .
Task
Write a program, which:

reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
decides whether it is possible to establish the Commission, and if so, proposes the list of members,
writes the result in the text file SPO.OUT.
Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.
There are multiple test cases. Process to end of file.
Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write any of them.
Sample Input

3 2
1 3
2 4
Sample Output

1
4
5



传送门

题意就是……n对点,第i对点的编号是i*2和i*2+1,每对点里面选出一个,
那么就构成了一个方案。
题目给出一些条件x,y,表示x点和y点不能被同时选出。让你求出任意一种方案。

这个问题就是2-SAT问题的模板题……
2-SAT有一些不错的论文的,可以百度百度。
具体做法就是先tarjan弄个缩点,接着拓扑排序求出一种方案即可。
对了求完强连通分量之后,要把边反一下……不然倒着怎么走= =

注意一下多组数据!


#include
#include
#include
#include
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int 
	N=16010,
	M=40005;
int n,m,scc,Ecnt,Time,top;
int DFN[N],LOW[N],stk[N],num[N];
int X[M<<1],Y[M<<1],TY[N];
int into[N],color[N];
bool vis[N],instack[N];
queueq;

struct Edge{
	int next,to;
}E[M<<1];int head[N];
int ty(int x){
	if (x&1) return x+1;
		else return x-1;
}
void add(int u,int v){
	E[++Ecnt].next=head[u];
	E[Ecnt].to=v;
	head[u]=Ecnt;
}
void tarjan(int u){
	DFN[u]=LOW[u]=++Time;
	stk[++top]=u;
	instack[u]=1;
	for (int i=head[u];i;i=E[i].next){
		int j=E[i].to;
		if (!DFN[j]){
			tarjan(j);
			LOW[u]=min(LOW[u],LOW[j]);
		} else
		if (instack[j]) LOW[u]=min(LOW[u],DFN[j]);
	}
	if (DFN[u]==LOW[u]){
		int now=-1;scc++;
		for (;now!=u;top--){
			now=stk[top];
			num[now]=scc;
			instack[now]=0;
		}
	}
}
bool ok(){
	for (int i=1;i<=n;i+=2)
		if (num[i]==num[i+1]) return 0;
	return 1;
}
void suodian(){
	scc=0;
	memset(DFN,0,sizeof(DFN));
	memset(LOW,0,sizeof(LOW));
	for (int i=1;i<=n;i++)
		if (!DFN[i]) Time=top=0,tarjan(i);

	Ecnt=0;
	memset(head,0,sizeof(head));
	memset(into,0,sizeof(into));

	for (int i=1;i<=(m<<1);i++)
		if (num[X[i]]!=num[Y[i]])
			add(num[Y[i]],num[X[i]]),into[num[X[i]]]++;
	for (int i=1;i<=n;i+=2)
		TY[num[i]]=num[i+1],TY[num[i+1]]=num[i];
}
void toposort(){
	memset(color,0,sizeof(color));
	for (int i=1;i<=scc;i++)
		if (!into[i]) q.push(i);
	while (!q.empty()){
		int u=q.front();
		q.pop();
		if (!color[u]) color[u]=1,color[TY[u]]=2;
		for (int i=head[u];i;i=E[i].next){
			int v=E[i].to;
			into[v]--;
			if (!into[v]) q.push(v);
		}
	}
}
void print(){
	for (int i=1;i<=n;i++)
		if (color[num[i]]==1) printf("%d\n",i); 
}
void solve(){
	int x,y;n<<=1;
	memset(head,0,sizeof(head));
	for (int i=1;i<=m;i++){
		x=read(),y=read();
		add(x,ty(y)),add(y,ty(x));
		X[(i<<1)-1]=x,Y[(i<<1)-1]=ty(y); 
		X[i<<1]=y,Y[i<<1]=ty(x);
	}

	suodian();
	if (!ok()){puts("NIE");return;}

	toposort();
	print();
}
int main(){
	while (~scanf("%d%d",&n,&m)) solve();
	return 0;
}




你可能感兴趣的:(tarjan,topo(拓扑),2-SAT)