题目链接:http://poj.org/problem?id=3352

给一个图,问加多少条边可以干掉所有的桥。

先找环,然后缩点。标记对应环的度,接着找桥。写几个例子就能知道要添加的边数是桥的个数/2取上整。

这题和3177不一样的地方在于,这个题考虑重边,而我的代码本身,饿哦考虑重边的。

考虑重边:

找出桥,然后缩点。计算缩点后度为1的连通块个数。

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Edge {
int v;
bool cut;
Edge() {}
Edge(int vv) : v(vv) { cut = ; }
}Edge; const int maxn = ;
const int maxm = ;
int n, m;
int dig[maxn];
int dfn[maxn], low[maxn], idx;
vector<Edge> G[maxn];
bool vis[maxn];
int st[maxn], top;
int belong[maxn], bcnt; void tarjan(int u, int p) {
int v;
low[u] = dfn[u] = ++idx;
vis[u] = ;
st[top++] = u;
Rep(i, G[u].size()) {
v = G[u][i].v;
if(v == p) continue;
if(!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]) {
G[u][i].cut = ;
Rep(j, G[v].size()) {
if(G[v][j].v== u) {
G[v][j].cut = ;
break;
}
}
}
}
else if(vis[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]) {
bcnt++;
do {
v = st[--top];
vis[v] = ;
belong[v] = bcnt;
} while(v != u);
}
} int main() {
// FRead();
int u, v;
while(~Rint(n) && ~Rint(m)) {
Rep(i, n+) G[i].cl();
Cls(vis); Cls(dig); Cls(dfn); Cls(low);
top = ; idx = ; bcnt = ;
Rep(i, m) {
Rint(u); Rint(v);
G[u].pb(Edge(v)); G[v].pb(Edge(u));
}
tarjan(, );
int ret = ;
For(u, , n+) {
Rep(i, G[u].size()) {
if(G[u][i].cut) {
dig[belong[u]]++;
}
}
}
For(i, , bcnt+) {
if(dig[i] == ) ret++;
}
printf("%d\n", (ret+)>>);
}
RT ;
}

不考虑重边:

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; const int maxn = ;
const int maxm = ;
int n, m;
int dig[maxn];
int pre[maxn], dfn[maxn], low[maxn];
vector<int> G[maxn];
bool vis[maxn]; void dfs(int u, int p) {
vis[u] = ;
pre[u] = p;
dfn[u] = low[u] = u;
Rep(i, G[u].size()) {
int v = G[u][i];
if(!vis[v]) dfs(v, u);
else if(v != p) low[u] = min(low[u], dfn[v]);
}
} void update(int u) {
int d = low[u];
while(u != d && u != ) {
low[u] = d;
u = pre[u];
}
} int main() {
// FRead();
int u, v;
while(~Rint(n) && ~Rint(m)) {
Rep(i, n+) G[i].cl();
Cls(vis); Cls(dig); Cls(dfn); Cls(low); Cls(pre);
Rep(i, m) {
Rint(u); Rint(v);
G[u].pb(v); G[v].pb(u);
}
For(i, , n+) if(!vis[i]) dfs(i, );
For(i, , n+) if(dfn[i] != low[i]) update(i);
For(u, , n+) {
Rep(i, G[u].size()) {
int v = G[u][i];
if(low[u] != low[v]) {
dig[low[u]]++; dig[low[v]]++;
}
}
}
int ret = ;
For(i, , n+) dig[i] /= ;
For(i, , n+) if(low[i] == i && dig[low[i]] == ) ret++;
printf("%d\n", ret % == ? ret / : ret / + );
}
RT ;
}

[POJ3352]Road Construction(缩点,割边,桥,环)的更多相关文章

  1. [POJ3352]Road Construction

    [POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...

  2. POJ3352 Road Construction 双连通分量+缩点

    Road Construction Description It's almost summer time, and that means that it's almost summer constr ...

  3. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  4. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

  5. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  6. POJ3352 Road Construction Tarjan+边双连通

    题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...

  7. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  8. 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)

    题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...

  9. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

随机推荐

  1. 高级php面试题及部分答案

    在网上看到一些高级php 的面试题目.. 闲来无事,搞了一些答案...可能不是很全面,留这以后备用吧. 一. 基本知识点1.1 HTTP协议中几个状态码的含义:503 500 401 403 404 ...

  2. textview点击后selector的pressed无效果

    原因: 需要配置  android:clickable="true" 这个跟开发环境有关,我之前用的android studio 就不需要这一项,默认可以点击. ********* ...

  3. Ubuntu下安装配置zsh和oh my zsh

    zsh优势:自动补全功能强大和很高的可配置性 1.查看当前系统装了哪些shell    cat /etc/shells 2.当前正在运行的是哪个版本的shell    echo $SHELL 3.安装 ...

  4. firefox常用扩展、脚本

    1.AutoPopup.uc.js:鼠标移到菜单和下拉箭头上自动弹出下拉菜单 2.moveButton.uc.js:移动或克隆按钮或菜单到火狐浏览器的任意位置 moveButton.uc.js使用说明 ...

  5. win7安装mysql

    转:http://blog.csdn.net/longyuhome/article/details/7913375 Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系型 ...

  6. 1257: [CQOI2007]余数之和sum - BZOJ

    Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, ...

  7. Eclipse 安装热部署JRebel

    开发环境 sts-3.7.2.RELEASE 安装步骤 1.打开应市场 2.搜索JRebel并进行下载 3.下载完成后点击JReble Configuation进入

  8. [nowCoder] 完全二叉树结点数

    给定一棵完全二叉树的头节点head,返回这棵树的节点个数.如果完全二叉树的节点数为N,请实现时间复杂度低于O(N)的解法. 分析:遍历的话不管是前序.中序.后序还是层次都是O(N),低于O(N)只能是 ...

  9. StringBuffer 和 StringBuilder

    如果你读过<Think in Java>,而且对里面描述HashTable和HashMap区别的那部分章节比较熟悉的话,你一定也明白了原因所在.对,就是支持线程同步保证线程安全而导致性能下 ...

  10. Web前端业界氛围极好的群——鬼懿IT

    鬼群简介 鬼懿IT主群号:,鬼懿IT-成长群:181368696 , 创建于2005年12月 ,聚集的业内人事包括:阿当,大漠,辣妈,崔凯,Rei,周裕波,司徒正美,丸子,鬼森林,寒冬,franky, ...