其他分享
首页 > 其他分享> > 【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)

【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)

作者:互联网

题干:

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country. 

Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:

Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled. 

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ). 

The input is terminated by a block with n = m = 0 . 
 

Output

For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled. 
 

Sample Input

5 5
1 4
1 5
2 5
3 4
4 5
0 0

Sample Output

2

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

题目大意:

亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1、  相互憎恨的两个骑士不能坐在直接相邻的2个位置; 2、  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。   如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。        现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?

一句话题意:

一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1。2.总人数为奇数。3.有仇恨的骑士不能挨着坐。问有几个骑士不能和任何人形成任何的圆圈。

解题报告:

注意这里的col数组不是给tarjan用的,而是给二分图染色用的。

注意割点会被flag[]=1多次,所以不能在过程中纪录ans,而是要标记can[]=1,最后遍历can数组看有多少等于1的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAXN = 1000 + 5;
const int MAXM = 2e6 + 5;
bool maze[MAXN][MAXN],can[MAXN];
struct Edge {
	int u,v;
	int ne;
} e[MAXM];
int dfn[MAXN],low[MAXN],stk[MAXN],clk,tot,index,bcc;
int flag[MAXN],tmp[MAXN],col[MAXN],head[MAXN];
int n,m;
void init() {
	for(int i = 1; i<=n; i++) {
		dfn[i]=low[i]=can[i]=0;//如果不初始化fa???
		head[i] = -1;
	}
	tot=0;clk=bcc=index=0;
}
void add(int u,int v) {
	e[++tot].u = u;
	e[tot].v = v;
	e[tot].ne = head[u];
	head[u] = tot;
}
bool bfs(int st) {
	queue<int> q;
	q.push(st);
	col[st] = 0;
	while(!q.empty()) {
		int cur = q.front();q.pop();
		for(int i = head[cur]; ~i; i = e[i].ne) {
			int v = e[i].v;
			if(flag[v] == 0) continue;
			if(col[v] != -1) {
				if(col[v] == col[cur]) return 1;
			}
			else {
				col[v] = !col[cur];
				q.push(v);
			}
		}
	}
	return 0;
}
void tarjan(int x,int fa) {
	dfn[x] = low[x] = ++clk;
	stk[++index] = x;
	for(int i = head[x]; ~i; i = e[i].ne) {
		int v = e[i].v;
		if(v == fa) continue;
		if(dfn[v] == 0) {
			tarjan(v,x);
			low[x] = min(low[x],low[v]);
			if(low[v] >= dfn[x])  {
				bcc++;//这里点双也用bcc表示一下。。
				for(int i = 1; i<=n; i++) flag[i] = 0,col[i]=-1; 
				int cnt = 0;
				tmp[++cnt] = x; 
				while(1) {
					int tmpp = stk[index];index--;
					tmp[++cnt] = tmpp;
//					col[tmp] = bcc; //这里不需要记录颜色,col数组用来二分图染色比较好。 					
					if(tmpp == v) break;//注意这里不是tmp==x,因为x还要在栈内,做其他点双联通分量的成员。而当前bcc,将x单独处理已下架就好。 
				} 
				//别忘处理割点:
				for(int j = 1; j<=cnt; j++) flag[tmp[j]] = 1;
				if(bfs(x) == 1) {
					for(int j = 1; j<=cnt; j++) can[tmp[j]]=1;
				}
			} 
		}
		else low[x] = min(low[x],dfn[v]);
	}
}
int main()
{
	
	while(~scanf("%d%d",&n,&m)) {
		if(n == 0 && m == 0) break;
		memset(maze,1,sizeof maze);
		init();// 每次都是 写好了init函数,忘了加到主函数中。。 
		for(int u,v,i = 1; i<=m; i++) {
			scanf("%d%d",&u,&v);
			maze[u][v] = maze[v][u] = 0;
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				if(i!=j && maze[i][j]) add(i,j);
			}
		}
		for(int i = 1; i<=n; i++) {
			if(dfn[i] == 0) tarjan(i,-1);
		}
		int ans = 0;
		for(int i = 1; i<=n; i++) {
			ans += can[i];
		}
		printf("%d\n",n - ans);
	}
	return 0 ;
}

 

标签:include,点双,int,knight,MAXN,knights,骑士,POJ,Round
来源: https://blog.csdn.net/qq_41289920/article/details/96470265