poj3177 Redundant Paths 双连通分量
来源:互联网 发布:王者荣耀 狄仁杰 知乎 编辑:程序博客网 时间:2024/06/01 08:31
题意/Description:
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
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:
Line 1: Two space-separated integers: F and R
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
输出/Output:
Line 1: A single integer that is the number of new paths that must be built.
题解/solution:
首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。(注意判断重边)
(不懂双连通分量见http://www.cnblogs.com/en-heng/p/4002658.html)
代码/Code:
type arr=record x,y,next:longint; end;var dfn,low,ls,ttt:array [0..5001] of longint; a:array [0..5001,0..5001] of boolean; tu:array [0..50001] of arr; ans:array [0..5001] of boolean; n,m,tot,p,cnt:longint;procedure add(o,p:longint);begin inc(tot); with tu[tot] do begin x:=o; y:=p; next:=ls[o]; ls[o]:=tot; end; inc(tot); with tu[tot] do begin x:=p; y:=o; next:=ls[p]; ls[p]:=tot; end;end;function min(o,p:longint):longint;begin if o<p then exit(o); exit(p);end;procedure tarjan(x,fa:longint);var t:longint;begin inc(p); dfn[x]:=p; low[x]:=p; t:=ls[x]; while t>0 do with tu[t] do begin if dfn[y]=0 then begin tarjan(y,x); low[x]:=min(low[x],low[y]); end else if y<>fa then low[x]:=min(low[x],dfn[y]); t:=next; end;end;procedure print;var i,sum:longint;begin for i:=1 to tot do with tu[i] do if low[x]<>low[y] then inc(ttt[low[x]]); sum:=1; for i:=1 to n do if ttt[i]=1 then inc(sum); writeln(sum div 2);end;procedure init;var i,x,y:longint;begin fillchar(a,sizeof(a),true); readln(n,m); for i:=1 to m do begin readln(x,y); if not a[x,y] then continue; a[x,y]:=false; a[y,x]:=false; add(x,y); end;end;begin init; tarjan(1,1); print;end.
- 【poj3177】【双连通分量】Redundant Paths
- poj3177 Redundant Paths 边双连通分量
- poj3177 Redundant Paths 双连通分量
- poj3177 Redundant Paths 双连通分量
- POJ3177 Redundant Paths 3352的双胞胎题 双连通分量
- poj3177 Redundant Paths 无向图 双连通分量 桥
- Redundant Paths-POJ3177(并查集+双连通分量)
- poj3177 Redundant Paths(边双连通分量+缩点)
- [POJ3177]Redundant Paths 边双连通分量 做题笔记
- poj3177——Redundant Paths(双连通分量)
- [POJ3177]Redundant Paths Tarjan求双连通分量
- poj3177——Redundant Paths(双连通分量)
- 【POJ3177】Redundant Paths-边双连通分量+缩点
- ★【双连通分量】Redundant Paths
- poj 3177 Redundant Paths(双连通分量)
- poj3177 双连通分量
- POJ 3177 Redundant Paths 边的双连通分量
- [边双连通分量] poj 3177 Redundant Paths
- Redis源码解析:26集群(二)键的分配与迁移
- hibernate关联List
- 面向对象设计原则之单一职责原则
- android_开发中那些踩过的和没踩的坑
- 《设计模式解析》读书笔记之Adapter Pattern(适配器模式)
- poj3177 Redundant Paths 双连通分量
- 1002 Problem B
- Python机器学习实战笔记之KNN算法
- 华为机试---文本嗅探
- java学习、java入门:JAVA编程题练习
- Problem D: C语言习题 不等长字符串排序
- 旅游机票类专业名词---PNR
- 《剑指offer》:[45]圆圈中最后剩下的数字(约瑟夫(Josephuse)环问题)
- Java 代理模式用法