您的位置:首页 > 其它

poj 2942 Knights of the Round Table 【无向图求BCC + 黑白染色判断二分图】

2015-07-16 22:05 633 查看
Knights of the Round Table
Time Limit: 7000MSMemory Limit: 65536K
Total Submissions: 10700Accepted: 3527
Description
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:

The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of
votes, and the argument goes on.)

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、 出席会议的骑士数必须是奇数。

对于那些无法出席所有会议的骑士,不幸的他要被剔除。 现在给定准备去开会的骑士数n,再给出m对憎恨关系(表示2个骑士之间是互相憎恨的),问至少要剔除多少个骑士才能顺利召开会议?




根据:
(1) 若连通分量含有奇圈,那么它的所有点都在奇圈里面;

(2) 如果一个双连通分量不是一个二分图,那它一定含有奇圈。反过来也成立,这是一个充要条件。


思路:在没有仇恨关系的骑士之间 建双向边,构图然后求出所有BCC并把各个BCC里面的点分别存储起来。最后依次判断每个BCC,若不是二分图,则标记该BCC里面所有骑士,说明他们在奇圈里面。最后判断有多少个骑士不在奇圈即可。



无语,一开始写的tarjan测试数据都过不了,最后重写就直接AC了。。。


#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define MAXN 1000+10 
#define MAXM 2000000+10 
#define INF 10000000
using namespace std;
struct Edge//这次用的结构体 只是个用于存储BCC边的媒介 
{
	int from, to;
};
vector<int> G[MAXN];//这个可变数组用来  存储图
int dfs_clock;//dfs次数 
int low[MAXN], dfn[MAXN];
bool iscut[MAXN];//是否为割点
int bccno[MAXN], bcc_cnt;//bccno[i]表明i属于哪个BCC bcc_cnt是BCC计算器 
vector<int> bcc[MAXN];//存储每个BCC里面的点 
stack<Edge> S;//存储每个BCC里面的边 
int n, m;//n个骑士 m个关系 
int color[MAXN];//黑白染色 用来判定图是否为二分图
bool odd[MAXN];//标记该骑士是否在奇圈上 
int hate[MAXN][MAXN];//记录骑士间关系 为1表明互相仇恨 
void init()
{
	memset(hate, 0, sizeof(hate));
	for(int i = 1; i <= n; i++)
	G[i].clear();//清空 
}
void getMap()//建图 
{
	int a, b;
	while(m--)
	{
		scanf("%d%d", &a, &b);
		hate[a][b] = hate[b][a] = 1;
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = i+1; j <= n; j++)
		{
			if(hate[i][j] == 0)
			{
				G[i].push_back(j);
				G[j].push_back(i);
			}
		}
	}
}
//void tarjan(int u, int fa)
//{
//	low[u] = dfn[u] = ++dfs_clock;
//	int child = 0; 
//	for(int i = 0; i < G[u].size(); i++)
//	{
//		int v = G[u][i];
//		Edge E = {u, v};
//		if(!dfn[v])
//		{
//			S.push(E);
//			child++;
//			tarjan(v, u);
//			low[u] = min(low[u], low[v]);
//			if(low[v] >= dfn[u])//割点 
//			{
//				iscut[u] = true;
//				bcc_cnt++;//BCC数目增加 
//				bcc[bcc_cnt].clear();//清空
//				for(;;)
//				{
//					Edge x = S.top(); S.pop();
//					if(bccno[x.from] != bcc_cnt) 
//					{
//						bcc[bcc_cnt].push_back(x.from);//存入 
//						bccno[x.from] = bcc_cnt;//标记为该BCC里面的点 
//					} 
//					if(bccno[x.to] != bcc_cnt)//同上 
//					{
//						bcc[bcc_cnt].push_back(x.to);
//						bccno[x.to] = bcc_cnt;
//					}
//					if(x.from == u && x.to == v) break;//检测到当前边  跳出 
//				} 
//			}
//			else if(dfn[v] < dfn[u] && v != fa)
//			{
//				S.push(E);
//				low[u] = min(low[u], dfn[v]);//更新反向边 
//			}
//		}
//	} 
//	if(fa < 0 && child == 1) iscut[u] = 0;//根节点是割点必须具有两个或两个以上的子节点
//}
void tarjan(int u, int fa)
{
	low[u] = dfn[u] = ++dfs_clock;
	int child = 0;//记录子节点数目 用于判断根节点是否为割点
	for(int i = 0; i < G[u].size(); i++)
	{
		int v = G[u][i];
		Edge E = {u, v};
		if(!dfn[v])
		{
			S.push(E);//存储边 
			child++;//累加子节点数目 
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u])//割点 
			{
				iscut[u] = true;
				bcc_cnt++;//BCC数目增加 
				bcc[bcc_cnt].clear();//清空 
				for(;;)
				{
					Edge x = S.top(); S.pop();
					if(bccno[x.from] != bcc_cnt)//不属于当前BCC
					{
						bcc[bcc_cnt].push_back(x.from);//存入 
					    bccno[x.from] = bcc_cnt;//标记为该BCC里面的点
					}
					if(bccno[x.to] != bcc_cnt)//同上 
					{
						bcc[bcc_cnt].push_back(x.to);   
					    bccno[x.to] = bcc_cnt;
					}
					if(x.from == u && x.to == v) break;//检测到当前边  跳出
				}
			}
		}
		else if(dfn[v] < dfn[u] && v != fa)
		{
			S.push(E);
			low[u] = min(low[u], dfn[v]);//更新反向边 
		}
	}
	if(fa < 0 && child == 1) iscut[u] = 0;//根节点是割点必须具有两个或两个以上的子节点
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(iscut, false, sizeof(iscut));
	memset(bccno, 0, sizeof(bccno));
	dfs_clock = bcc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1);
}
bool judge(int u, int num)//判断u所在的BCC是否为二分图    num表示当前u属于的BCC编号 
{
	for(int i = 0; i < G[u].size(); i++)//遍历全部 
	{
		int v = G[u][i];
		if(bccno[v] != num)//不在该BCC里面
		continue;//不做处理
		if(color[v] == color[u]) return false;//颜色相同失败
		if(!color[v])
		{
			color[v] = 3 - color[u];
			if(!judge(v, num)) return false;//下一次涂色失败 
		} 
    }
	return true;//是二分图 
} 
void solve()
{
	memset(odd, 0, sizeof(odd));
	for(int i = 1; i <= bcc_cnt; i++)//遍历所有BCC 
	{
		memset(color, 0, sizeof(color));//初始化 
		for(int j = 0; j < bcc[i].size(); j++)  bccno[bcc[i][j]] = i; 
		int u = bcc[i][0];//BCC的第一个点
	    color[u] = 1;//默认第一个为黑色 若找到染白色方案只需全部翻转即可 
		if(!judge(u, i))//不是二分图 有奇圈 
		{
			for(int j = 0; j < bcc[i].size(); j++)
			odd[bcc[i][j]] = 1;//标记 
		} 
	}
	int ans = n;
	for(int i = 1; i <= n; i++) if(odd[i]) ans--;
	printf("%d\n", ans);
}
int main()
{
	while(scanf("%d%d", &n, &m), n||m)
	{
		init();
		getMap();
		find_cut(1, n);//找到所有割点与BCC 
		solve();//判断是不是二分图  黑白染色 黑色为1,白色为2 
	} 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: