题目链接: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. 深入理解ThreadLocal(一)

    Android里,在不同的线程(假设子线程已经创建了Looper)中创建Handler时,并不需要显式指定Looper,系统能自动找到该线程自己的Looper.不同线程的Looper相互独立,之所以能 ...

  2. 分类图 Class Diagram

    类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图. 类图是系统中静态视图的一部分,静态视图可以包括许多的类图. 静态视图用于为软件系统进行结构建模,它构造系统的词汇和关系,而 ...

  3. php缓存

    APC缓存退出舞台,APCU诞生,OPCACHE升级! 对于php5.5以后的新版本开发,使用apc习惯的开发者可能会发现php.5.5以后找不到了这个组件的更新,尤其是中文phper都找不到合适的资 ...

  4. hadoop历史服务器配置问题

    作者:sdjnzqr 出处:http://www.cnblogs.com/sdjnzqr/ 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文连接 ...

  5. sampler2d

    Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{   Texture = <texture_ ...

  6. 解决eclipse打开报错:failed to create the java virtual ma

    在Eclipse安装目录下找到:eclipse.ini 将如下参数改为: --launcher.XXMaxPermSize 128M ------------------------------- 说 ...

  7. 使用EF code first和asp.net mvc4遇到的问题总结

    最近使用EF code first和asp.net mvc4做项目,遇到些问题,记录一下. 一.EF code first 生成外键列问题. 一般情况下,都是先写一个int型外键id属性,然后写一个外 ...

  8. PHP ServerPush

    原文:http://yorsal.com/archives/302 随着人们对Web即时应用需求的不断上升,Server Push(推送)技术在聊天.消息提醒尤其是社交网络等方面开始兴起,成为实时应用 ...

  9. ZOJ1232 Adventure of Super Mario spfa上的dp

    很早之前听说有一种dp是在图上的dp,然后是在跑SPFA的时候进行dp,所以特地找了一题关于在SPFA的时候dp的. 题意:1~a是村庄 a+1~a+b是城堡,存在m条无向边.求由a+b->1的 ...

  10. who is in front of me 解题报告

    题目描述:N(1<=N<=50005)个学生站成一个纵队,每个人只能看到前面身高比他高(严格大于)的人 求所有人中能看到的最大人数 分析:对于某个人A,设前面第一个身高比他高的人是B.如果 ...