Knights of the Round Table
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:
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.
Source |
提示
题意:
亚瑟王要召开会议,有n个(1<=n<=1000)骑士将在圆桌开会,他们有m对(1<=m<=1000000)相互憎恨(没有单方面的),所以这m对人是不能坐到一起的(相邻),不然会掐起来。并且为了保证进行投票时不会因为票数一样而导致得不到最终结果。所以还要保证开会人数必须是奇数。为了世界核平请求出最少需要几个人被踢出会以才能让会议进行下去。
思路:
1.以憎恨关系建立图,但需要利用这个图再建立一个它的补图,补图表示的是能坐在一起的关系图。
3.并且要保证点数为奇数。每求出一组就要判断是否为奇圈,二分图就是奇圈,那么判断二分图就用交叉染色法。
此题没有什么技巧,就是对图知识点的考查,知识点的综合性相当的高,弄清楚知识点再来解题吧。
这片博客没看懂那么给出一个传送门:http://blog.csdn.net/lyy289065406/article/details/6756821
接下来给出对于Tarjan算法相关知识的博客:
缩点:http://blog.sina.com.cn/s/blog_85e103a40100uy30.html
Tarjan的理解:http://blog.csdn.net/u011026968/article/details/10283071
双连通分量与强连通分量:http://www.cnblogs.com/szy-wlxy/p/4639316.html
示例程序
Source Code
Problem: 2942 Code Length: 2988B
Memory: 4484K Time: 1282MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
int u,v,pos,next,vis;
}w[2000000];
int h[1000],numw,id,top,map[1000][1000],color[1000],dfn[1000],low[1000],mark[1000],stack[2000000],oddpoint[1000];
int min(int x,int y)
{
if(x<y)
{
return x;
}
else
{
return y;
}
}
void insert(int u,int v)
{
w[numw].u=u;
w[numw].v=v;
w[numw].pos=numw+1;
w[numw].next=h[u];
w[numw].vis=0;
h[u]=numw;
numw++;
w[numw].u=v;
w[numw].v=u;
w[numw].pos=numw-1;
w[numw].next=h[v];
w[numw].vis=0;
h[v]=numw;
numw++;
}
int find(int t)
{
int i,pos;
for(i=h[t];i!=-1;i=w[i].next)
{
pos=w[i].v;
if(mark[pos]==1)
{
if(color[pos]==-1)
{
color[pos]=(color[t]+1)%2;
return find(pos);
}
else if(color[pos]==color[t])
{
return 1;
}
}
}
return 0;
}
void judge(int t,int n)
{
int i;
memset(mark,0,sizeof(mark));
do
{
top--;
i=stack[top];
mark[w[i].u]=1;
mark[w[i].v]=1;
}while(w[i].u!=t);
memset(color,-1,sizeof(color));
color[t]=0;
if(find(t)==1)
{
for(i=0;n>i;i++)
{
if(mark[i]==1) //记录二分图上的点
{
oddpoint[i]=1;
}
}
}
}
void tarjan(int t,int n)
{
int i;
dfn[t]=id;
low[t]=id;
id++;
for(i=h[t];i!=-1;i=w[i].next)
{
if(w[i].vis==0)
{
stack[top]=i;
top++;
w[i].vis=1; //遍历过就不要再遍历了
w[w[i].pos].vis=1;
if(dfn[w[i].v]==-1)
{
tarjan(w[i].v,n);
low[t]=min(low[t],low[w[i].v]);
if(low[w[i].v]>=dfn[t])
{
judge(t,n); //每有一个双连通分量就要判一次二分图
}
}
else
{
low[t]=min(low[t],dfn[w[i].v]);
}
}
}
}
int main()
{
int n,m,i,i1,u,v,num;
scanf("%d %d",&n,&m);
while(n!=0||m!=0)
{
id=0;
numw=0;
top=0;
num=0;
memset(h,-1,sizeof(h));
memset(map,0,sizeof(map));
memset(dfn,-1,sizeof(dfn));
memset(oddpoint,0,sizeof(oddpoint));
for(i=1;m>=i;i++)
{
scanf("%d %d",&u,&v);
map[u-1][v-1]=1; //无向图
map[v-1][u-1]=1;
}
for(i=0;n>i;i++)
{
for(i1=i+1;n>i1;i1++)
{
if(map[i][i1]==0) //建立补图
{
insert(i,i1);
}
}
}
for(i=0;n>i;i++)
{
if(dfn[i]==-1) //枚举点求双连通分量
{
tarjan(i,n);
}
}
for(i=0;n>i;i++) //不在双连通分量上的就必须开除
{
if(oddpoint[i]==0)
{
num++;
}
}
printf("%d\n",num);
scanf("%d %d",&n,&m);
}
return 0;
}