点双联通和二分图着色(二分图判定)模板
2013-08-20 21:12
316 查看
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <cmath> #include <stack> #define LL long long using namespace std; const int maxn=1000+10; const int maxm=1000000+10; struct node { int u,v; }; stack<node> S; vector<int> G[maxn]; int e[maxn][maxn],pre[maxn],is_cut[maxn],bcc_num[maxn],odd[maxn],color[maxn]; int n,m,tot; int dfs_clock; int dfs(int u,int fa) { int lowu,lowv; pre[u]=lowu=++dfs_clock; int child=0; int i,j; node tem; for(i=1;i<=n;i++) // { if(!e[u][i]) { if(!pre[i]) { S.push(node{u,i}); child++; lowv=dfs(i,u); lowu=min(lowu,lowv); if(lowv>=pre[u]) { is_cut[u]=1; tot++; G[tot].clear(); for(;;) { tem=S.top(); S.pop(); if(bcc_num[tem.u]!=tot) { bcc_num[tem.u]=tot; G[tot].push_back(tem.u); } if(bcc_num[tem.v]!=tot) { bcc_num[tem.v]=tot; G[tot].push_back(tem.v);} if(tem.u==u&&tem.v==i) break; } } } else { if(pre[i]<pre[u]&&i!=fa) { S.push(node{u,i}); lowu=min(lowu,pre[i]); } } } } if(fa<0&&child==1) is_cut[u]=0; return lowu; } void find_BCC() { memset(pre,0,sizeof(pre)); memset(bcc_num,0,sizeof(bcc_num)); dfs_clock=0; tot=0; int i; for(i=1;i<=n;i++) if(!pre[i]) dfs(i,-1); } int bi_color(int u,int num) { int v,i; for(i=0;i<G[num].size();i++) { v=G[num][i]; if(v!=u&&!e[u][v]) { if(color[u]==color[v]) return 0; if(!color[v]) { color[v]=3-color[u]; if(!bi_color(v,num)) return 0; } } } return 1; } int main() { while(~scanf("%d%d",&n,&m)) { if(!n&&!m) break; int i,u,v; memset(e,0,sizeof(e)); for(i=0;i<m;i++) { scanf("%d%d",&u,&v); e[u][v]=e[v][u]=1; } find_BCC(); memset(odd,0,sizeof(odd)); int j; for(i=1;i<=tot;i++) { memset(color,0,sizeof(color)); for(j=0;j<G[i].size();j++) { v=G[i][j]; bcc_num[v]=i; } color[G[i][0]]=1; if(!bi_color(G[i][0],i)) for(j=0;j<G[i].size();j++) odd[G[i][j]]=1; } int ans=n; for(i=1;i<=n;i++) if(odd[i]) ans--; printf("%d\n",ans); } return 0; }
相关文章推荐
- 二分图算法模板以及相关知识(判定二分图)
- 二分图的判定(模板)
- hdu 3478 //判联通+二分图判定
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
- 二分图的判定模板(dfs,bfs)
- 二分图判定 【模板】
- 二分图判定 nyoj1015(模板)
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
- 点的双联通+二分图的判定(poj2942)
- NP-Hard Problemd(二分图判定着色)
- 二分图判定 (黑白二着色法)
- 二分图的判定 -- 着色
- 图论模板 求割顶/判断二分图
- HDU 2444 The Accomodation of Students (二分图判定、求最大匹配)
- 【和二分图判定问题的例题】
- 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)
- POJ 2942Knights of the Round Table(二分图判定+双连通分量)
- 二分图的匈牙利算法 HDU 2063(裸模板,待增加)
- 二分图判定
- HDU 2063 过山车(模板—— 二分图最大匹配问题)