POJ3177Redundant Paths (双联通分量)
来源:互联网 发布:张国荣同款衣服淘宝 编辑:程序博客网 时间:2024/05/22 13:08
Description
Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 71 22 33 42 54 55 65 7
Sample Output
2
Hint
One visualization of the paths is:
1 2 3 +---+---+ | | | | 6 +---+---+ 4 / 5 / / 7 +Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3 +---+---+ : | | : | | 6 +---+---+ 4 / 5 : / : / : 7 + - - - -Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
大佬的题解:http://www.cnblogs.com/shenben/p/5839786.html
题目大意:
有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场。奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以选择至少两条独立的路。现在F个牧场的任何两个牧场之间已经至少有一条路了,奶牛们需要至少有两条。
给定现有的R条直接连接两个牧场的路,F-1<=R<=10000,计算至少需要新修多少条直接连接两个牧场的路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指没有公共边的路,但可以经过同一个中间顶点
题解:
tarjan求割边(桥)的模板
一个有桥的连通图,如何把它通过加边变成边双连通图?
方法为首先求出所有的桥,然后删除这些桥边,剩下的每个连通块都是一个双连通子图。把每个双连通子图收缩为一个顶点,再把桥边加回来,最后的这个图一定是一棵树,边连通度为1。
其实,本题数据保证是一个连通图,那么桥=缩点数-1;
统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。
知识点:
对于一棵树,需要加上(leaf+1)/2条边,才能使这棵树变成双联通。left为叶节点个数。
AC代码+详细注释:
#include<iostream>#include<cstring>#include<cstdio>#include<stack>#define N 5005using namespace std;struct Edge{ int v, nxt;}edge[N<<1];bool mp[N][N], mark[N];int n, m, head[N], low[N], dfn[N], id[N], in[N], tol, tim, scc;//tim为时间戳、scc为双联通分量的个数//id为所属的双联通分量、in为双联通分量的度数stack<int> s;void add(int u, int v){ edge[++tol].v = v; edge[tol].nxt = head[u]; head[u] = tol;}bool judge(int x, int y){ if((x&1) && y == x + 1) return true; if(!(x&1) && y == x - 1) return true; return false;}void tarjan(int v, int fa){ low[v] = dfn[v] = ++tim; s.push(v); mark[v] = 1; for(int i = head[v]; i; i = edge[i].nxt)//i为边的编号 { if(judge(i, fa)) continue;//是不是与父节点相连的边 int w = edge[i].v; if(!dfn[w]) { tarjan(w, i); low[v] = min(low[v], low[w]); } else if(mark[w])//if(mark[w]可以没有 low[v] = min(low[v], dfn[w]); } int u; if(low[v] == dfn[v]) { scc++; do { u = s.top(); s.pop(); id[u] = scc;//为该双联通分量内的点标上所属双联通分量的标号 mark[u] = 0;//可以注释掉 }while(u != v); }}int main(){ while(scanf("%d%d", &n ,&m) != EOF) { tim = scc = tol = 0; while(!s.empty()) s.pop(); memset(mark, false, sizeof(mark)); memset(mp, false, sizeof(mp)); memset(dfn, 0, sizeof(dfn)); memset(head, 0, sizeof(head)); for(int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); if(!mp[x][y]) { add(x, y);add(y, x); mp[x][y] = mp[y][x] = 1; } } for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i, -1); for(int i = 1; i <= n; i++) for(int j = head[i]; j; j = edge[j].nxt) if(id[i] != id[edge[j].v]) in[id[i]]++; int ans = 0; for(int i = 1; i <= scc; i++)//计算每个双联通分量缩点后的度数 if(in[i] == 1) ans++; printf("%d\n", (ans+1) / 2); } return 0;}
- POJ3177Redundant Paths (双联通分量)
- poj 3177 Redundant Paths (边双联通分量+缩点)
- POJ3177 Redundant Paths【边双联通分量】【Tarjan】
- POJ 3177 Redundant Paths 边双联通分量 缩点
- poj3177—Redundant Paths(缩点+边双联通分量)
- 浅谈强联通分量,双联通分量
- poj3177Redundant Paths tarjan缩点
- 双联通分量
- 边双联通分量
- 边双联通分量
- 点双联通分量
- 边双联通分量
- 双联通分量 HDU4738
- Redundant Paths+POJ+边双联通分量(有重边-奇偶判断法)
- POJ 3352 Road Construction&& POJ 3177 Redundant Paths 双联通分量
- BZOJ 1718 [Usaco2006 Jan] Redundant Paths 分离的路径 双联通分量
- POJ3177 Redundant Paths —— 边双联通分量 + 缩点
- UVALive5796点双联通分量or边双联通分量
- iOS
- 安卓开发:数字文字选择器
- 欢迎使用CSDN-markdown编辑器
- 遍历HashMap的四种方式
- 【LeetCode】49. Group Anagrams
- POJ3177Redundant Paths (双联通分量)
- Android 软键盘的处理
- PHP 打开文件浏览器并传递文件
- Spring的核心类有哪些
- 经典视觉SLAM框架
- python vim配置
- 使用YOLOv2训练行人检测模型
- linux下的文件的管道的最基本使用(代码)
- javascript元素相关属性和方法