您的位置:首页 > 其它

POJ 2942 Knights of the Round Table 点全连通分量 + DFS染色 做的好辛苦

2013-06-24 21:29 453 查看
题目描述:

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. 

题目大意是找出有几个人不能在任何连通的奇圈内。  解法是连通那些能坐在一起的人,然后用点强连通分量记录各自的人,然后对这些人进行DFS染色判断是否为二分图(奇圈是非二分图),最后来个visit数组记录那些人数,最后输出答案。

/*
* @author ipqhjjybj
* @date 20130623
*/
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <set>
using namespace std;

typedef vector<int> vi;

#define inf 0x3f3f3f3f
#define MAXN 1005
#define MAXM 100010
#define clr(x,k) memset((x),(k),sizeof(x))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)
#define Base 10000

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

#define White 1
#define Black 0
#define None -1
/*
struct Edge{
int st,ed;
Edge(){};
Edge(int a,int b){
st = a,ed = b;
}
};*/
int n,m;
int MAP[MAXN][MAXN];
vi viMap[MAXN];
bool flag;
int sig,tcc;
int low[MAXN],dfn[MAXN];
int Color[MAXN];
vector<int> block[MAXN];
int stack[MAXN],top;
int visit[MAXN];
void init(){
clr(MAP,0);
for(int i = 0;i <= n;i++) viMap[i].clear();
for(int i = 0,a,b;i < m;i++){
scanf("%d %d",&a,&b);
MAP[a][b]=MAP[b][a]=1;
}
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i!=j&&!MAP[i][j])
viMap[i].push_back(j);

clr(low,0),clr(dfn,0),clr(visit,0),clr(stack,0);
top = sig = tcc = 0;
for(int i = 0;i <= n;i++) block[i].clear();
}
int Tarjan(int cur,int &sig,int &tcc,int from){
low[cur] = dfn[cur]=++sig;
stack[top++] = cur;
for(vector<int>::iterator it = viMap[cur].begin();it != viMap[cur].end();it++){
if(*it == from) continue;
if(!dfn[*it]){
Tarjan(*it,sig,tcc,cur);
low[cur]=min(low[cur],low[*it]);
if(dfn[cur]<=low[*it]){
block[tcc].push_back(cur);
while(1){
int w = stack[--top];
block[tcc].push_back(w);
if(w == *it) break;

}
// printf("%d block size:%d\n",tcc,block[tcc].size());
if(block[tcc].size()<=2) block[tcc].clear();
else tcc++;
}
}else
low[cur] = min(low[cur],dfn[*it]);
}
return 0;
}
void dfs(int cur,int fa,int color,vi &bfsSi){
int oColor = (color == White?Black:White);
Color[cur] = color;
// printf("cur %d color : %d\n",cur,color);
foreach(it,bfsSi){
if(fa == *it || cur != *it && MAP[cur][*it] || cur == *it) continue;
if(None == Color[*it]){
dfs(*it,cur,oColor,bfsSi);
}else{
if(Color[*it] == Color[cur]){
// printf("it %d color %d cur %d color %d\n",*it,Color[*it],cur,Color[cur]);
flag = true;
return;
}
}
}
}
int check(vi & bfsSi){ //二分图检查,DFS染色
flag = false; //默认表示是二分图
clr(Color,None);
for(vi::iterator it = bfsSi.begin();it != bfsSi.end() && !flag; it++){
if(None == Color[*it]){
dfs(*it,-1,Black,bfsSi);
}
}
if(flag == true) return 1;
else return 0;
}
int main(){
// freopen("2942.in","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF){
if(!n&&!m)
break;
init();
for(int i = 1;i <= n;i++)
if(!dfn[i])
Tarjan(i,sig,tcc,-1);

for(int i = 0;i < tcc;i++){
if(check(block[i])){
foreach(it,block[i]){
visit[*it]=1;
}
}
}
int ans=0;
for(int i = 1;i <= n;i++)
if(!visit[i])
ans++;
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐