poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
来源:互联网 发布:华为p8root软件 编辑:程序博客网 时间:2024/06/06 05:26
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long using namespace std; const int maxn = 1000 + 10;//const int INF = 0x3f3f3f3f;int n, m;int A[maxn][maxn];//计算点—双连通分量,用栈S来保留当前bcc中的边int pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;vector<int> G[maxn], bcc[maxn];struct Edge {int u, v;Edge(int u = 0, int v = 0) : u(u), v(v) {}};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] = true;bcc_cnt++; bcc[bcc_cnt].clear(); //注意!bcc从1开始编号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);}}//dfs给二分图进行黑白二着色,用颜色1表示黑色,颜色2表示白色,0表示没着色 int color[maxn]; //判断节点u所在的连通分量是否为二分图bool bipartite(int u, int id) {for(int i = 0; i < G[u].size(); i++) {int v = G[u][i];if(bccno[v] != id) continue;if(color[v] == color[u]) return false;if(!color[v]) {color[v] = 3 - color[u];if(!bipartite(v, id)) return false;}}return true;} int odd[maxn];void init() {int u, v;memset(A, 0, sizeof(A));memset(odd, 0, sizeof(odd));while(m--) {scanf("%d%d", &u, &v);u--; v--;A[u][v] = A[v][u] = 1;}for(int i = 0; i < n; i++) G[i].clear();for(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);}void solve() {find_bcc(n);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 = 0;for(int i = 0; i < n; i++) if(!odd[i])ans++;cout << ans << endl;}int main() {//freopen("input.txt", "r", stdin);while(scanf("%d%d", &n, &m) == 2 && n) {init();solve();}return 0;}
题意:有n个骑士开会,每次至少三个人且人数必须为奇数,相互憎恨的人不能相邻,给出骑士们相互的憎恨关系,求多少骑士不能参加任何一个会议。
大白书经典例题,写完以后感觉收获很大。
1.以骑士为节点建立无向图,如果两个骑士不憎恨,那么在他们之间连一条边,则题目转化为求不在任一简单奇圈上的结点个数。
2.简单圈上的所有节点必然属于同一双连通分量,因此需要先找出所有双连通分量。
3.很重要的一个结论!!!!!!!!对于一个双连通分量来说,如果他是二分图,那么是没有奇圈的,反之有奇圈的图一定不是二分图,证明略。
4.也很重要的一个结论!!!!!!!如果结点v所属的一个双连通分量不是二分图,那么v一定属于一个奇圈。现在证明这个结论,如果双连通分量B不是一个二分图,根据3,B中一定含有一个奇圈,那么对于不属于这个奇圈的v来说,根据双连通性,一定存在两条不相交路径(除起点外无公共结点),使得v能到达这个奇圈上的两个不同点,设为v1,v2.
而因为v1和v2在一个不同的奇圈上,那么从v1到v2的两条路径长度一奇一偶,所以总能构造出一条经过v的奇圈。
5.注意,每个割顶属于多个双连通分量,可能被标记多次。
0 0
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
- poj 2942 Knights of the Round Table 补图+点双连通分量+判定二分图
- poj 2942 Knights of the Round Table(双连通分量+tarjan+二分图判定)
- POJ 2942 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVALive - 3523 Knights of the Round Table(无向图的双连通分量)
- POJ 2942 Knights of the Round Table(双连通分量+二分图)
- POJ 2942 Knights of the Round Table [二分图染色][点双连通分量]
- LA 3523 Knights of the Round Table 边双连通分量+二分图判定
- poj 2942 Knights of the Round Table 双连通分量
- POJ 2942 Knights of The Round Table <双连通分量>
- POJ 2942 Knights of the Round Table (点-双连通分量 + 交叉法染色判二分图)
- poj 2942 Knights of the Round Table 点-双连通分量 图论综合题
- POJ_2942_Knights of the Round Table(点的双连通分量+二分图判定)
- poj 2942 Knights of the Round Table(补图的求取+双连通分量+奇环的判断)
- 【poj2942】圆桌骑士Knights of the Round Table【双连通分量】【二分图】【奇圈】
- UVALive 3523Knights of the Round Table(二分图+双连通分量)
- LA 3523 Knights of the Round Table(点双连通分量+二分图判断)
- poj_2942 Knights of the Round Table(判断二分图+双连通分量)
- openerp学习笔记 对象间关系【多对一(一对一)、一对多(主细结构)、多对多关系、自关联关系(树状结构)】
- C++ 全局变量 局部变量 静态变量 及 extern
- openerp学习笔记 对象间关系【多对一(一对一)、一对多(主细结构)、多对多关系、自关联关系(树状结构)】
- openerp学习笔记 对象间关系【多对一(一对一)、一对多(主细结构)、多对多关系、自关联关系(树状结构)】
- 转发一位学员一次性过高项的感言
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
- redis学习-事物,生存时间
- Struts2数据传输的背后机制:ValueStack(值栈)
- 安卓计算器
- 控制台相关语法
- 在MacBook上查看Java的安装路径并设置JAVA_HOME变量
- Codeforces Round #309 (Div. 1) D. Nudist Beach 二分 贪心
- STM32中的(bit-band)
- 黑马程序员----java中的多态