poj2942 Knights of the Round Table,无向图点双联通,二分图判定
无向图点双联通。二分图判定
<span style="font-size:18px;">#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
#include <cstring> using namespace std; struct Edge{
int u, v;
};
const int maxn = 1005;
int pre[maxn], iscut[maxn], bccno[maxn],dfs_clock, bcc_cnt;
vector<int> G[maxn], bcc[maxn]; 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();
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);
} int odd[maxn], color[maxn];
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 A[maxn][maxn]; int main(){
#ifndef ONLINE_JUDGE
freopen("in.cpp", "r", stdin);
freopen("out.cpp", "w", stdout);
#endif // ONLINE_JUDGE
int kase = 0, n, m;
while(scanf("%d%d", &n, &m)==2 &&n){
for(int i=0; i<n; ++i)G[i].clear();
memset(A, 0, sizeof A );
for(int i=0; i<m; ++i){
int u, v;
scanf("%d%d", &u, &v);
u--; v--;
A[u][v] = A[v][u] = 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;
}
</span>
poj2942 Knights of the Round Table,无向图点双联通,二分图判定的更多相关文章
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 「题解」:[POJ2942]Knights of the Round Table
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- UVA-1364.Knights of the Round Table 无向图BCC
题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...
- POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
- [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
- POJ2942:Knights of the Round Table
传送门 点双练习. 很简单的一道模板题,建立反图,求出点双,二分图判定奇环. //POJ 2942 //by Cydiater //2016.11.2 #include <iostream> ...
随机推荐
- 打破“中规中矩”,手机QQ何以萌翻众人?
随着移动互联网的迅猛发展,越来越多的手机应用展现在了用户面前,不过,面对林林总总的手机应用,有时候我们却提不起兴趣,因为功能的同质化,UI的千篇一律已经让我们多少有些审美疲劳的感觉. ...
- 适用函数ALSM_EXCEL_TO_INTERNAL_TABLE把excel文件传输到内表中
FM:ALSM_EXCEL_TO_INTERNAL_TABLE 是上载Excel文件的一个函数,但是这个函数有两个限制. 一是每个CELL只能导入前50个字符,二是如果超过9999行,行号会初始化为从 ...
- Delphi事件的广播 good
明天就是五一节了,辛苦了好几个月,借此机会应该尽情放松一番.可是想到Blog好久没有写文章,似乎缺些什么似的.这几个月来在项目中又增长了许多经验,学到许多实际应用的知识.不如把一些比较有用的记录下来, ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- 为HttpStatusCodeResult加入customErrors
asp.net mvc的action返回值为HttpStatusCodeResult时的customErrors总是不起作用 (404和exception时的500,因为他们并不是HttpStatus ...
- TCP三次握手和Time-Wait状态
第一次握手:建立连接时.client发送syn包和一个随机序列号seq=x到server,并进入SYN_SEND状态,等待server进行确认. (syn,同 步序列编号). 第二次握手,server ...
- HDU1452Happy 2004(高次幂取模+积性函数+逆元)
题目意思:2004^x的所有正因数的和(S)对29求余:输出结果: 原题链接 题目解析:解析参照来源:点击打开链接 因子和 6的因子是1,2,3,6; 6的因子和是s(6)=1+2+3+6=12; 2 ...
- [破解]java打包Exe工具 - Jar2Exe Wizard
打包java文件为exe的方法和软件有很多,还有一些开源的软件和一些免费的软件. 我用过的所有打包exe软件中,Jar2Exe Wizard是最好用的,但是只有一个月的试用期,需要的可以从官网下载. ...
- 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)
原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...
- If-Modified-Since页面是否更新
第一次先请求某个网页,抓取到本地,假设文件名为 a.html.这时文件系统有个文件的修改时间. 第二次访问网页,如果发现本地已经有了 a.html,则向服务器发送一个 If-Modified-Sinc ...