题意:
先建图,将能坐在一起的点连边。所以能坐一桌的人就将围成一个环(因为是求有多少个骑士无论如何都不能参加会议,也就是说它们绝对不属于任一个奇环)
找点双连通分量,对于每个连通分量,找出其中的点双连通分量。
只要该点双分量不是二分图,则它一定包含奇圈,而且任意点一定在一个奇圈上。(证明自寻)
于是点双连通分量中的点都是可以参加会议的。
二分图染色就是随便挨着染成不同的颜色,看会不会矛盾,矛盾为奇环,否则为偶环。
Code:
Status | Accepted |
---|---|
Time | 9ms |
Length | 3194 |
Lang | C++ 5.3.0 |
Submitted | 2017-10-18 21:30:38 |
Shared | |
RemoteRunId | 2290584 |
#include
#include
#include
#include
#include
#include
using namespace std;
const int Maxn = 1000;
const int Maxm = Maxn * Maxn;
struct EDGE{
int v, nxt;
}edge[Maxm + 5];
struct node{
int s, t;
node(){}
node(int a, int b){s = a, t = b;}
}Stk[Maxm + 5];
int N, M, cnt, Dfs_Clock, tp, pcc;
int fir[Maxn + 5], Dfn[Maxn + 5], Low[Maxn + 5], Pccno[Maxn + 5], Col[Maxn + 5];
bool ban[Maxn + 5][Maxn + 5], Ans[Maxn + 5];
vectorPccn[Maxn + 5];
void getint(int & num){
char c; int flg = 1; num = 0;
while((c = getchar()) < '0' || c > '9') if(c == '-') flg = -1;
while(c >= '0' && c <= '9'){ num = num * 10 + c - 48; c = getchar(); }
num *= flg;
}
void addedge(int a, int b){
edge[++ cnt].v = b, edge[cnt].nxt = fir[a], fir[a] = cnt;
}
void Tarjan(int u, int ff){
Dfn[u] = Low[u] = ++ Dfs_Clock;
for(int i = fir[u]; i; i = edge[i].nxt){
int v = edge[i].v;
if(! Dfn[v]){
Stk[++ tp] = node(u, v);
Tarjan(v, u);
Low[u] = min(Low[u], Low[v]);
if(Low[v] >= Dfn[u]){
Pccn[++ pcc].clear();
while(1){
int l = Stk[tp].s, r = Stk[tp].t;
-- tp;
if(Pccno[l] != pcc)
Pccno[l] = pcc, Pccn[pcc].push_back(l);
if(Pccno[r] != pcc)
Pccno[r] = pcc, Pccn[pcc].push_back(r);
if(l == u && r == v) break;
}
}
}
else if(v != ff) Low[u] = min(Low[u], Dfn[v])//这里不需要再入队,因为之前一定已经入队。
}
}
bool Dfs(int u, int ff){
for(int i = fir[u]; i; i = edge[i].nxt)
if(edge[i].v != ff && Pccno[edge[i].v] == Pccno[u]){
int v = edge[i].v;
if(Col[v] == Col[u]) return 0;
if(! Col[v]){
Col[v] = 3 - Col[u];
if(Dfs(v, u) == 0) return 0;
}
}
return 1;
}
void Colr(){
for(int i = 1; i <= pcc; ++ i){
for(int j = 0; j < Pccn[i].size(); ++ j)
Pccno[Pccn[i][j]] = i, Col[Pccn[i][j]] = 0;
Col[Pccn[i][0]] = 1;
if(Dfs(Pccn[i][0], 0) == 0)
for(int j = 0; j < Pccn[i].size(); ++ j)
Ans[Pccn[i][j]] = 1;
}
}
int main(){
while(~scanf("%d%d", &N, &M) && (N || M)){
memset(fir, 0, sizeof fir );
memset(Dfn, 0, sizeof Dfn );
memset(Ans, 0, sizeof Ans );
memset(Pccno, 0, sizeof Pccno );
cnt = Dfs_Clock = pcc = tp = 0;
int u, v;
for(int i = 1; i <= M; ++ i){
getint(u), getint(v);
ban[u][v] = ban[v][u] = 1;
}
for(int i = 1; i < N; ++ i)
for(int j = i + 1; j <= N; ++ j) if(! ban[i][j])
addedge(i, j), addedge(j, i);
else ban[i][j] = ban[j][i] = 0;
for(int i = 1; i <= N; ++ i) if(! Dfn[i])
Tarjan(i, -1);
Colr();
int rt = 0;
for(int i = 1; i <= N; ++ i) rt += ! Ans[i];
printf("%d\n", rt);
}
return 0;
}
/*
5 5
1 4
1 5
2 5
3 4
4 5
0 0
*/