uva 1364
刘书上例题
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <stack>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#define FFI freopen("in.txt", "r", stdin)
#define maxn 1010
#define INF 0x3f3f3f3f
#define inf 10000000
#define MOD 1000000007
#define ULL unsigned long long
#define LL long long
#define _setm(houge) memset(houge, INF, sizeof(houge))
#define _setf(houge) memset(houge, -1, sizeof(houge))
#define _clear(houge) memset(houge, 0, sizeof(houge))
using namespace std; struct Edge
{
int u, v;
}; int color[maxn], pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;
vector<int> G[maxn], bcc[maxn];
stack<Edge> S;
int A[maxn][maxn], odd[maxn]; bool bipartite(int u, int b) {
for(int i = 0; i < (int)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 dfs(int u, int fa) {
int lowu = pre[u] = ++ dfs_clock;
int child = 0;
for(int i = 0; i < (int)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) {
_clear(pre);
_clear(iscut);
_clear(bccno);
dfs_clock = bcc_cnt = 0;
for(int i = 0; i < n; ++ i) {
if(!pre[i]) dfs(i, -1);
}
} int main() {
int n, m;
// FFI;
while(scanf("%d%d", &n, &m) == 2 && n) {
for(int i = 0; i < n; ++ i) G[i].clear();
_clear(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);
_clear(odd);
for(int i = 1; i <= bcc_cnt; ++ i) {
_clear(color);
for(int j = 0; j < (int)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 < (int)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;
}
uva 1364的更多相关文章
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- POJ 1364 King (UVA 515) 差分约束
http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
随机推荐
- Oracle | Java日期处理
public class Test{ public static void main (String args []){ j ...
- Quartz使用一 通过getJobDataMap传递数据
Quartz定时器使用比较广泛,介绍一点简单的使用 上代码:定义一个Job,执行具体的任务 package org.tonny.quartz; import java.text.SimpleDateF ...
- Linux OpenGL 实践篇-9 模型
之前一直渲染箱子,显得有点单调.这一次我们绘制一个用艺术家事先用建模工具创建的模型. 本次实践参考:https://learnopengl-cn.github.io/03%20Model%20Load ...
- Android Studio -自定义LogCat的颜色
博文地址 http://www.cnblogs.com/Loonger/p/6285344.html 先看看效果 (设置中的显示,下图) 步骤如下 File->Settings 或Ctrl + ...
- Java集合(四)--基于JDK1.8的ArrayList源码解读
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- Vickers Vane Pump - How To Choose Vane Pump Parameter Specifications?
1 rated pressure selection. The rated pressure of the vane pump products is 7MPa, 1OMPa, 16MPa, 2lMP ...
- Oracle中的for和while循环
实例: beginfor i in 51..500 loop delete from test t where t.date=to_date('2016-07-01', 'yyyy-MM-dd') a ...
- linux秘钥分发
秘钥分发 ssh-copy-id -i /root/.ssh/id_rsa.pub "-p 9000 root@192.168.1.100" 传送文件 scp -P9000 -rp ...
- js layui 分页脚本
//分页 layui.use(['laypage'], function(){ var laypage = layui.laypage; laypage.render({ elem: 'page' , ...
- python 装饰器(二): 加参数
接上篇python 闭包&装饰器(一) 一.功能函数加参数:实现一个可以接收任意数据的加法器 源代码如下: def show_time(f): def inner(*x, **y): # 形参 ...