POJ2942:Knights of the Round Table

时间:2022-05-29 08:17:14

传送门

点双练习。

很简单的一道模板题,建立反图,求出点双,二分图判定奇环。

//POJ 2942
//by Cydiater
//2016.11.2
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)		for(int i=j;i<=n;i++)
#define down(i,j,n)		for(int i=j;i>=n;i--)
#define cmax(a,b) 		a=max(a,b)
#define cmin(a,b )		a=min(a,b)
#define Auto(i,node)		for(int i=LINK[node];i;i=e[i].next)
#define vci vector<int>
const int MAXN=1e5+5;
const int oo=0x3f3f3f3f;
inline int read(){
	char ch=getchar();int x=0,f=1;
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int LINK[MAXN],len=0,dfn[MAXN],low[MAXN],stack[MAXN],top=0,dfs_clock=0,ans,N,M,cnt=0,head,tail,q[MAXN],col[MAXN];
bool E[1005][1005],OK[MAXN],avail[MAXN];
vci block;
struct edge{
	int y,next;
}e[MAXN];
namespace solution{
	void Clear(){
		len=dfs_clock=top=ans=0;
		memset(dfn,0,sizeof(dfn));
		memset(low,0,sizeof(low));
		memset(LINK,0,sizeof(LINK));
		memset(OK,0,sizeof(OK));
		memset(E,0,sizeof(E));
	}
	inline void insert(int x,int y){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;}
	inline void Insert(int x,int y){insert(x,y);insert(y,x);}
	void init(){
		Clear();
		N=read();M=read();if(N==0)exit(0);
		up(i,1,M){
			int x=read(),y=read();
			E[x][y]=E[y][x]=1;
		}
		up(i,1,N)up(j,i+1,N)if(!E[i][j])Insert(i,j);
	}
	bool check(){
		memset(col,0,sizeof(col));
		head=1;tail=0;q[++tail]=block[0];
		col[block[0]]=1;
		for(;head<=tail;head++){
			int node=q[head];
			Auto(i,node)if(avail[e[i].y]){
				if(col[e[i].y]==col[node])return 1;
				if(col[e[i].y]==0){
					col[e[i].y]=col[node]*(-1);
					q[++tail]=e[i].y;
				}
			}
		}
		return 0;
	}
	void color(){
		int siz=block.size();
		if(siz>1){
			memset(avail,0,sizeof(avail));
			up(i,0,siz-1)avail[block[i]]=1;
			if(check())up(i,0,siz-1)OK[block[i]]=1;
		}
	}
	void tarjan(int node){
		dfn[node]=low[node]=++dfs_clock;stack[++top]=node;
		Auto(i,node)if(!dfn[e[i].y]){
			tarjan(e[i].y);
			cmin(low[node],low[e[i].y]);
			if(dfn[node]<=low[e[i].y]){
				int tmp;block.clear();cnt++;
				do{
					tmp=stack[top--];
					block.push_back(tmp);
				}while(e[i].y!=tmp);
				block.push_back(node);
				color();
			}
		}else cmin(low[node],dfn[e[i].y]);
	}
	void slove(){
		up(i,1,N)if(!dfn[i])tarjan(i);
		up(i,1,N)if(!OK[i])ans++;
		printf("%d\n",ans);
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	while(1){
		init();
		slove();
	}
	return 0;
}