题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33804

思路:和poj的一道题有点像,不过这道题图可能不连通,因此首先求边双连通分量,然后算每个连通分量的度数,显然叶子节点的度数为1,孤立点的度数为0,然后就是统计度数了,对于孤立点ans+=2,对于叶子节点,ans++。于是最后的答案就是(ans+1)/2了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
#define MAXN 1111 int n, m, cnt, _count;
stack <int >S;
vector <vector<int > >g; int low[MAXN], dfn[MAXN], color[MAXN];
int degree[MAXN];
bool mark[MAXN];
void Tarjan(int u, int father)
{
low[u] = dfn[u] = ++ cnt;
S.push(u);
mark[u] = true;
for(int i = ; i < g[u].size(); i ++ ){
int v = g[u][i];
if(v == father)continue;
if(dfn[v] == ) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if(mark[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]){
int x;
_count++;
do {
x = S.top();
S.pop();
mark[x] = false;
color[x] = _count;
}while(x != u);
}
} int main()
{
int u, v, ans;
while(~scanf("%d %d", &n, &m)){
g.clear();
g.resize(n+);
while(m --){
scanf("%d %d",&u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(dfn, , sizeof(dfn));
memset(mark, false, sizeof(mark));
cnt = _count = ;
for(int i = ; i <= n; i ++){
if(dfn[i] == )Tarjan(i, -);
}
if(_count == ){
puts("");
continue;
}
memset(degree, , sizeof(degree));
for(int i = ; i <= n; i++){
for(int j = ; j < g[i].size(); j++){
if(color[i] != color[g[i][j]])degree[color[g[i][j]]] ++;
}
}
ans = ;
for(int i = ; i <= _count; i++){
if(degree[i] == )ans += ; // 孤立点
else if(degree[i] == )ans ++; // 叶子节点
}
printf("%d\n", (ans + )/ );
}
return ;
}

uva 10972(边双连通分量)的更多相关文章

  1. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  2. UVA 10972 - RevolC FaeLoN(边-双连通分量)

    UVA 10972 - RevolC FaeLoN option=com_onlinejudge&Itemid=8&page=show_problem&category=547 ...

  3. UVA 10765 Doves and bombs(双连通分量)

    题意:在一个无向连通图上,求任意删除一个点,余下连通块的个数. 对于一个非割顶的点,删除之后,原图仍连通,即余下连通块个数为1:对于割顶,余下连通块个数>=2. 由于是用dfs查找双连通分量,树 ...

  4. UVA 10972 RevolC FaeLoN(边-双连通+缩点)

    很好的一道图论题,整整撸了一上午... 题意是给定一个无向图,要求将所有边变为有向边,求最少加入多少条有向边,使得该图强连通?这里先假设一个问题:给定一个无向子图,该子图具有怎样的性质才能使得将其无向 ...

  5. LA 3523 圆桌骑士(二分图染色+点双连通分量)

    https://vjudge.net/problem/UVALive-3523 题意: 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置 ...

  6. poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)

    #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #includ ...

  7. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  8. 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分

    E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...

  9. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 751[Submit][Statu ...

随机推荐

  1. 我用的/etc/vimrc

    " 映射非数字/字母键, 如:ctrl,shift, alt, home,end,功能键F1~F12, 要把这些键用尖括号括起来!如: map <F3> :NERDTree< ...

  2. git入门知识了解

    文章转自:http://www.cnblogs.com/cocowool/archive/2012/02/17/2356125.html 源代码管理系统(SCM)与版本控制   版本控制是一种记录若干 ...

  3. Commando War

    Commando War“Waiting for orders we held in the wood, word from the front never cameBy evening the so ...

  4. 深入理解Java中的继承

    对于面向对象的程序设计而言,每一个程序员都应该去了解Java中的封装,继承和多态,那么我今天来说的主要是以继承为核心的主题. 一.关于对继承的理解. 继承是面向对象的三大特性之一,是java中实现代码 ...

  5. [Effective JavaScript 笔记]第6章:库和API设计--个人总结

    前言 又到了一章的总结,这章里的内容.是把我从一个代码的使用者,如何换位成一个代码的编写者.如何让别人用自己的代码更容易,不用去注意太多的无用细节,不用记住冗长的函数名.在使用API时怎样避免使用者会 ...

  6. [BZOJ3670][UOJ#5][NOI2014]动物园

    [BZOJ3670][UOJ#5][NOI2014]动物园 试题描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学 ...

  7. editorial-render A

    PROBLEM LINK: PracticeContest Author: adminTester: Kevin AtienzaEditorialist: Ajay K. VermaRussian T ...

  8. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  9. 创建一个最简单的Linux随机启动服务

    转自: http://xiaoxia.org/2011/11/15/create-a-simple-linux-daemon/

  10. 初识lua

    转自:http://www.oschina.net/question/12_115993-- 两个横线是单行注释(译者注:这跟 SQL 一样) --[[ 增加两个 [ 和 ] 变成多行注释 我是多行注 ...