刘书上例题

#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的更多相关文章

  1. UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)

    尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...

  2. POJ 1364 King (UVA 515) 差分约束

    http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  5. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  6. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  7. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  8. 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 ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. mongodb多条件查询总结

    根据两字段乘积过滤查询分页数据 db.cron.aggregate([{$project:{_id:,AppID:,result:{$add:["$endlottery",&quo ...

  2. dede网站目录权限设置

    如果你的网站数据十分重要(那种两天就能弄好的垃圾站就算了),建议按本文所说的安全步骤进行严格的设置.1.目录权限 我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的 ...

  3. (转)Spring如何装配各种集合类型的属性

    http://blog.csdn.net/yerenyuan_pku/article/details/52858499 在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合 ...

  4. TCP和流

    http://www.cnblogs.com/lwzz/archive/2011/07/03/2096963.html TCP是一种流协议(stream protocol).这就意味着数据是以字节流的 ...

  5. iview modal 弹框 模板

    iview modal 弹框 模板 <!-- * @description 上传图片 * @fileName camera.vue * @author 彭成刚 * @date // :: * @ ...

  6. 模板引擎freemarker的使用(二)

    freemarker默认配置使用时,如果传到前端的值为null或者不存在,后台会报错. 处理方法: <bean id="freemarkerConfig" class=&qu ...

  7. Linux常用命令大全2

    Linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.驱动.键盘.鼠标,还是用户等都是文件,Linux命令是它正常运行的核心.接下来,就来看看xp系统下载编辑 ...

  8. sublime中使用markdown并实时编辑

    1.需求 想在sublime中编辑.md文件 2.步骤 找到菜单栏: 快捷键,shift + command + p,选择 Package Control:Install Package, 没有找到P ...

  9. Hibernate-03

    目的:表操作(表维护) 一.一对一(略过) 二.一对 1.建表原则:在多的一方创建外键指向一的一方的外键 2.建表:实体中添加 商品实体表: private Set<User> user ...

  10. 第一讲:vcs simulation basic

    要求: 1.complie a verilog/systemverilog design using vcs 2.simulate a verilog/systemverilog design vcs ...