题目链接: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. 为aps.net core项目加上全局异常捕捉和记录

    在asp.net core中的方案在这里:http://stackoverflow.com/questions/30385246/can-asp-net-5-app-useerrorhandler-a ...

  2. C++中的lambda表达式

    1.基本形式: [捕获列表](参数列表){函数体};     其中捕获列表和函数体不能省略但是捕获列表可以为空,也就是说最简单的lambda表达式是:  []{}; 2.lambda表达式又叫匿名函数 ...

  3. notifyDataSetChanged listview内容没更新的问题

    如红色部分所示,需在Adapter添加setData方法,当 listData中数据更改后,调用setData,为Adapter设置新的数据,此时调用notifyDataSetChanged() 就可 ...

  4. 后缀树(Suffix Tree)

          问题描述:               后缀树(Suffix Tree)   参考资料: http://www.cppblog.com/yuyang7/archive/2009/03/29 ...

  5. 控制DIV属性——实现盒子长、宽、背景等变化

    写在最前面:Demo的源起来自于http://js.fgm.cc/learn/,但是实现部分都是经过自己思考和优化的,有时会借助别人的图片,然而“窃喜”.如无特殊说明,demo都是经过ie6.ie7等 ...

  6. prim求MST

    PRIM==>>MST模板 #include <iostream> using namespace std; #define typec int #define V 3 con ...

  7. AIZU 0005

    GCD and LCM Time Limit : 1 sec, Memory Limit : 65536 KB Japanese version is here GCD and LCM Write a ...

  8. ***php解析html类库simple_html_dom

    下载地址:https://github.com/samacs/simple_html_dom 一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好 ...

  9. 【转】Java读取文件方法大全

    本文转自:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html#undefined 目录: 按字节读取文件内容 按字符读取文 ...

  10. 【Linux高频命令专题(7)】rm

    简述 rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. 命令格式 rm [选项 ...