【poj3177】 Redundant Paths
来源:互联网 发布:宁波软件培训 编辑:程序博客网 时间:2024/06/08 05:22
http://poj.org/problem?id=3177 (题目链接)
题意:给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥。
Solution
我们可以发现,用最少的边使得图中没有桥,那么就是将图缩点得到树,求使每个叶子节点相连所需要的最少边数,即 (叶子节点个数+1)/2 。
Tarjan求出图中的桥,以及并查集记录下每个节点属于哪个双连通分量,只与一座桥相连的点即为叶子节点,统计答案即可。
听说会有重边,不过对我的程序好像并没有什么影响。
代码:
// poj3177#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<set>#define MOD 1000000007#define inf 2147483640#define LL long long#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);using namespace std;inline LL getint() { LL x=0,f=1;char ch=getchar(); while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=10010;struct edge {int to,next;}e[maxn<<2];int f[maxn],dfn[maxn],vis[maxn],low[maxn],head[maxn],bridge[maxn][2],cnts[maxn];int cnt,ind,s,n,m;void insert(int u,int v) { e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt; e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;}int find(int x) { return x==f[x] ? x : f[x]=find(f[x]);}void Tarjan(int u,int fa) { dfn[u]=low[u]=++ind; vis[u]=1; for (int i=head[u];i;i=e[i].next) if (e[i].to!=fa) { if (!vis[e[i].to]) { Tarjan(e[i].to,u); low[u]=min(low[u],low[e[i].to]); if (dfn[u]<low[e[i].to]) { bridge[++s][0]=u; bridge[s][1]=e[i].to; } else { int r1=find(u),r2=find(e[i].to); f[r1]=r2; } } else low[u]=min(low[u],dfn[e[i].to]); }} int main() { while (scanf("%d%d",&n,&m)!=EOF) { cnt=ind=s=0; for (int i=1;i<=n;i++) head[i]=dfn[i]=low[i]=vis[i]=cnts[i]=0; for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); insert(x,y); } for (int i=1;i<=n;i++) f[i]=i; Tarjan(1,-1); for (int i=1;i<=s;i++) { cnts[find(bridge[i][0])]++; cnts[find(bridge[i][1])]++; } int ans=0; for (int i=1;i<=n;i++) if (cnts[i]==1) ans++; printf("%d\n",(ans+1)/2); } return 0;}
0 0
- poj3177 - Redundant Paths
- POJ3177--Redundant Paths
- 【POJ3177】Redundant Paths
- poj3177 Redundant Paths
- poj3177 Redundant Paths
- POJ3177 Redundant Paths
- POJ3177 Redundant Paths
- 【poj3177】 Redundant Paths
- poj3177 Redundant Paths
- Redundant Paths(POJ3177)
- 【poj3177】【双连通分量】Redundant Paths
- poj3177 Redundant Paths 边双连通分量
- POJ3177 Redundant Paths (双联通缩点)
- poj3177 Redundant Paths && poj3352 Road Construction
- poj3177 Redundant Paths 双连通分量
- poj3177 Redundant Paths 双连通分量
- poj3177 Redundant Paths(边双联通)
- [POJ3177]Redundant Paths(tarjan求边双)
- 设计模式:装饰模式(Decorator)
- CCS项目结构:各种文件作用
- 异常类
- 欢迎使用CSDN-markdown编辑器
- MapReduce初级案例
- 【poj3177】 Redundant Paths
- 设计模式:外观模式(Facade)
- QTP自动化框架练习一(flight4a)
- 我的Gulp入门合集
- 思科路由器 一些配置
- 设计模式:享元模式(Flyweight)
- 【NOI2012】美食节
- NYOJ 2 括号配对问题【栈的运用】
- Node.js+Socket.IO搭建WebSocket实时应用