| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | 13110572105 Log Out Mail:2(1) Login Log Archive |
Language:
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 |
[Submit] [Go Back] [Status] [Discuss]
给出一个无向图,然后计算出该图的补图,然后再计算补图中不再任一奇圈内的点的个数。
首先要求出所有的点的双连通分量,然后判断每一个双连通分量中是否存在奇圈,若有,则该双连通分量中的任意一点均在某一个奇圈上。
求解双连通分量的方法:
设 rv[ ] 表示每个点在DFS时被访问的次序,low[ ] 表示节点 u 及其子孙所连接到点中 rv[ ] 最小的值。
首先需要一个辅助栈来存放每一个双连通分量的边。
若边( u , v )仅符合下面两种情况的任意一种则将边压入栈中:
1,v 尚未被访问。
2,rv[ v ] <= low[u] 且 v 正在等待回溯。
若当 u 的 子节点 v 回溯完成时有 low[v] >= rv[u],则说明 u 是一个割点,此时要将栈中的在(u , v )及在其后面压入的边弹出,这些边就组成了一个点的双连通分量。
判断奇圈可以用BFS染色法。可见判断二分图的方法
书上学习二分图的一个例题。
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;
const int maxe=1010;
const int maxv=10e5+10;
struct Edge
{
int u;
int v;
};
int n,m,odd[maxv],color[maxv];
int a[maxe][maxe],pre[maxe],bccno[maxe],dfs_clock,bcc_cnt;
bool iscut[maxe];
vector<int> g[maxe],bcc[maxe];
stack<Edge> s;
int dfs(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
int child=0;
for(int i=0; i<g[u].size(); i++)
{
int v=g[u][i];
Edge e=(Edge)
{
u,v
};
if(!pre[v])
{
s.push(e);
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u])
{
iscut[u]=1;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;)
{
Edge x=s.top();
s.pop();
if(bccno[x.u]!=bcc_cnt)
{
bcc[bcc_cnt].push_back(x.u);
bccno[x.u]=bcc_cnt;
}
if(bccno[x.v]!=bcc_cnt)
{
bcc[bcc_cnt].push_back(x.v);
bccno[x.v]=bcc_cnt;
}
if(x.u==u&&x.v==v)
break;
}
}
}
else if(pre[v]<pre[u]&&v!=fa)
{
s.push(e);
lowu=min(lowu,pre[v]);
}
}
if(fa<0&&child==1)
iscut[u]=0;
return lowu;
}
void find_bcc(int n)
{
memset(pre,0,sizeof(pre));
memset(iscut,0,sizeof(iscut));
memset(bccno,0,sizeof(bccno));
dfs_clock=bcc_cnt=0;
for(int i=0; i<n; i++)
if(!pre[i])
dfs(i,-1);
}
bool bipartite(int u,int b)
{
for(int i=0; i<g[u].size(); i++)
{
int v=g[u][i];
if(bccno[v]!=b)
continue;
if(color[v]==color[u])
return false;
if(!color[v])
{
color[v]=3-color[u];
if(!bipartite(v,b))
return false;
}
}
return true;
}
int main()
{
while(scanf("%d %d",&n,&m)&&(n||m))
{
for(int i=0; i<n; i++)
g[i].clear();
memset(a,0,sizeof(a));
for(int i=0; i<m; i++)
{
int ita,itb;
scanf("%d%d",&ita,&itb);
ita--;
itb--;
a[ita][itb]=a[itb][ita]=1;
}
for(int u=0; u<n; u++)
for(int v=u+1; v<n; v++)
if(!a[u][v])
{
g[u].push_back(v);
g[v].push_back(u);
}
find_bcc(n);
memset(odd,0,sizeof(odd));
for(int i=1; i<=bcc_cnt; i++)
{
memset(color,0,sizeof(color));
for(int j=0; j<bcc[i].size(); j++)
bccno[bcc[i][j]]=i;
int u=bcc[i][0];
color[u]=1;
if(!bipartite(u,i))
for(int j=0; j<bcc[i].size(); j++)
odd[bcc[i][j]]=1;
}
int ans=n;
for(int i=0; i<n; i++)
if(odd[i])
ans--;
printf("%d\n",ans);
}
return 0;
}
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator