POJ-2942 Knights of the Round Table 双连通分量[推荐]

  题目链接:http://poj.org/problem?id=2942

  题意:有n个骑士要举行圆桌会议。有如下几个限制条件:

      1.每次会议至少要3名骑士,且骑士个数为奇数。

      2.相互憎恨的骑士不能坐在一起。

  统计有多少个骑士不能参加任何一个会议。

  以骑士为结点建立无向图,如果骑士互相憎恨,那么建立无向边,题目转换为不在任何一个简单奇圈上的结点的个数。简单圈上的点必然属于个一个双连通分量,因此找出所有的双连通分量,判断哪些双连通分量不是二分图。因为二分图中一定没有奇圈,而非二分图中一定有一个奇圈,那么其他点都可以靠这个奇圈形成一个奇圈。那么算法就很清楚了,对于每个双连通分量,若它不是二分图,则标记所有节点。最后统计未被标记的节点的个数。

  1 //STATUS:G++_AC_1219MS_4892KB

  2 #include <functional>

  3 #include <algorithm>

  4 #include <iostream>

  5 //#include <ext/rope>

  6 #include <fstream>

  7 #include <sstream>

  8 #include <iomanip>

  9 #include <numeric>

 10 #include <cstring>

 11 #include <cassert>

 12 #include <cstdio>

 13 #include <string>

 14 #include <vector>

 15 #include <bitset>

 16 #include <queue>

 17 #include <stack>

 18 #include <cmath>

 19 #include <ctime>

 20 #include <list>

 21 #include <set>

 22 #include <map>

 23 using namespace std;

 24 //define

 25 #define pii pair<int,int>

 26 #define mem(a,b) memset(a,b,sizeof(a))

 27 #define lson l,mid,rt<<1

 28 #define rson mid+1,r,rt<<1|1

 29 #define PI acos(-1.0)

 30 //typedef

 31 typedef __int64 LL;

 32 typedef unsigned __int64 ULL;

 33 //const

 34 const int N=1010;

 35 const int INF=0x3f3f3f3f;

 36 const int MOD=100000,STA=8000010;

 37 const LL LNF=1LL<<60;

 38 const double EPS=1e-8;

 39 const double OO=1e15;

 40 const int dx[4]={-1,0,1,0};

 41 const int dy[4]={0,1,0,-1};

 42 //Daily Use ...

 43 inline int sign(double x){return (x>EPS)-(x<-EPS);}

 44 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

 45 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

 46 template<class T> inline T Min(T a,T b){return a<b?a:b;}

 47 template<class T> inline T Max(T a,T b){return a>b?a:b;}

 48 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

 49 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

 50 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

 51 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

 52 //End

 53 

 54 struct Edge{

 55     int u,v;

 56 }e[N*N];

 57 bool iscut[N];

 58 int first[N],next[N*N],low[N],pre[N],bccno[N],vis[N],color[N],ma[N][N];

 59 int n,m,mt,dfs_clock,bcnt;

 60 vector<int> bcc[N];

 61 stack<Edge> s;

 62 

 63 void adde(int a,int b)

 64 {

 65     e[mt].u=a;e[mt].v=b;

 66     next[mt]=first[a];first[a]=mt++;

 67     e[mt].u=b;e[mt].v=a;

 68     next[mt]=first[b];first[b]=mt++;

 69 }

 70 

 71 bool bipartite(int u,int a)

 72 {

 73     int i,v;

 74     for(i=first[u];i!=-1;i=next[i]){

 75         v=e[i].v;

 76         if(bccno[u]!=a)continue;

 77         if(color[v]==color[u])return false;

 78         if(!color[v]){

 79             color[v]=3-color[u];

 80             if(!bipartite(v,a))return false;

 81         }

 82     }

 83     return true;

 84 }

 85 

 86 void dfs(int u,int fa)

 87 {

 88     int i,j,v,child=0;

 89     pre[u]=low[u]=++dfs_clock;

 90     for(i=first[u];i!=-1;i=next[i]){

 91         child++;

 92         v=e[i].v;

 93         if(!pre[v]){

 94             s.push({u,v});

 95             dfs(v,u);

 96             low[u]=Min(low[u],low[v]);

 97             if(low[v]>=pre[u]){

 98                 iscut[u]=true;

 99                 Edge x;x.u=-1;

100                 bcnt++;bcc[bcnt].clear();

101                 while(x.u!=u || x.v!=v){

102                     x=s.top();s.pop();

103                     if(bccno[x.u]!=bcnt){bcc[bcnt].push_back(x.u);bccno[x.u]=bcnt;}

104                     if(bccno[x.v]!=bcnt){bcc[bcnt].push_back(x.v);bccno[x.v]=bcnt;}

105                 }

106             }

107         }

108         else if(v!=fa && pre[v]<pre[u]){

109             s.push({u,v});

110             low[u]=Min(low[u],pre[v]);

111         }

112     }

113     if(fa==-1 && child==1)iscut[u]=false;

114 }

115 

116 void find_bcc()

117 {

118     int i,j;

119     bcnt=dfs_clock=0;mem(pre,0);

120     mem(bccno,0);mem(iscut,0);

121     for(i=1;i<=n;i++){

122         if(!pre[i])dfs(i,-1);

123     }

124 }

125 

126 int main()

127 {

128  //   freopen("in.txt","r",stdin);

129     int i,j,a,b,ans;

130     while(~scanf("%d%d",&n,&m) && (n ||m ))

131     {

132         mem(ma,0);

133         while(m--){

134             scanf("%d%d",&a,&b);

135             ma[a][b]=ma[b][a]=1;

136         }

137         mt=0;

138         mem(first,-1);

139         for(i=1;i<=n;i++){

140             for(j=i+1;j<=n;j++){

141                 if(!ma[i][j])adde(i,j);

142             }

143         }

144 

145         find_bcc();

146 

147         mem(vis,0);

148         for(i=1;i<=bcnt;i++){

149             for(j=0;j<bcc[i].size();j++)bccno[bcc[i][j]]=i;

150             mem(color,0);

151             color[bcc[i][0]]=1;

152             if(!bipartite(bcc[i][0],i)){

153                 for(j=0;j<bcc[i].size();j++)vis[bcc[i][j]]=1;

154             }

155         }

156         ans=n;

157         for(i=1;i<=n;i++)ans-=vis[i];

158 

159         printf("%d\n",ans);

160     }

161     return 0;

162 }

 

你可能感兴趣的:(table)