#POJ Knights of the Round Table (DFS二分染色 + 点双联通)

Knights of the Round Table

Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 16039 Accepted: 5315

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.

题目大意 : 有N个骑士, M对敌对关系(互相敌对), 只有两两不敌对的骑士才可以坐一起,要求所有骑士坐一圈,并且数量为奇数, 问你至少删除多少个人

思路 :很明显题目是让我们求奇环, 而且反向建图后图可能不连通,所以对每一个点双联通的块用dfs进行二分染色,之所以不用bfs是因为你不知道终止点在哪里,所以无法用形如链表的方式来存编号,所以可以在弹出栈内元素的时候将所有点保存到数组里, 再dfs染色,如果有奇环, 那么该点双联通块内的元素都标记上, 最后看谁没有被标记, 就将他踢出去就好

Accepted code

#include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int MAXM = 2e3 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

vector <int> e[MAXN << 1];
int dfn[MAXM], low[MAXM], n, m, X;
int op[MAXM], pre[MAXM], tot, scnt;
bool vis[MAXM], cut[MAXM], mp[MAXM][MAXM];
stack <int> st;
void init() {
	MEM(dfn, 0); MEM(low, 0); MEM(op, 0);
	MEM(vis, 0); MEM(cut, 0); MEM(mp, 0);
	for (int i = 1; i <= n; i++) e[i].clear();
}
bool dfs(int x, int c) { // 交叉染色
	op[x] = c;
	for (int i = 0; i < SZ(e[x]); i++) {
		int vi = e[x][i];
		if (!vis[vi]) continue;
		if (op[vi] == c) return true;
		if (op[vi] == -1 && dfs(vi, !c)) return true;
	}
	return false;
}
void tarjan(int x) {
	dfn[x] = low[x] = ++tot;
	st.push(x);
	for (int i = 0; i < SZ(e[x]); i++) {
		int vi = e[x][i];
		if (!dfn[vi]) {
			tarjan(vi);
			Min(low[x], low[vi]);
			if (low[vi] >= dfn[x]) {
				int k; MEM(vis, 0); X = 0;
				do {
					k = st.top();
					st.pop();
					pre[++X] = k;
					vis[k] = true;
				} while (k != vi);
				MEM(op, -1); vis[x] = true; // 不要忘了割点
				if (dfs(x, 0)) { // 有奇环标记
					cut[x] = true;
					for (int i = 1; i <= X; i++) cut[pre[i]] = true;
				}
			}
		}
		else Min(low[x], dfn[vi]);
	}
}

int main()
{
	while (~sc("%d %d", &n, &m) && n + m) {
		init();
		for (int i = 0; i < m; i++) {
			int ui, vi;
			sc("%d %d", &ui, &vi);
			mp[ui][vi] = mp[vi][ui] = true;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (i != j && !mp[i][j]) e[i].push_back(j);  // 反向建图
			}
		}
		for (int i = 1; i <= n; i++) {
			if (!dfn[i]) tarjan(i);
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (!cut[i]) ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值