poj 3177 边连通分量
思路:
dfs求出所有点的low值,然后对每个连通分量进行缩点,可以通过low来进行缩点。虽然在同一连通分量里可能存在不同的low值,但这并不影响缩点。将每个连通分量缩为一个点后,只要求出这个缩点后的树上的叶子节点个数就行了。结果为(leaf+1)/2。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#define Maxn 1010
#define Maxm Maxn*10
using namespace std;
int index[Maxn],degree[Maxn],dfn[Maxn],low[Maxn],e,n,lab=,num,visit[Maxn][Maxn];
void init()
{
memset(index,-,sizeof(index));
memset(degree,,sizeof(degree));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(visit,,sizeof(visit));
e=lab=num=;
}
struct Edge{
int from,to,next,v;
}edge[Maxm];
void addedge(int from, int to)
{
edge[e].v=;
edge[e].from=from;
edge[e].to=to;
edge[e].next=index[from];
index[from]=e++;
edge[e].v=;
edge[e].to=from;
edge[e].from=to;
edge[e].next=index[to];
index[to]=e++;
}
int dfs(int u)
{
dfn[u]=low[u]=++lab;
int i,j,temp;
for(i=index[u];i!=-;i=edge[i].next)
{
temp=edge[i].to;
if(edge[i].v) continue;
edge[i].v=edge[i^].v=;
if(!dfn[temp])
{
dfs(temp);
low[u]=min(low[u],low[temp]);
}
low[u]=min(low[u],dfn[temp]);
}
return ;
}
int solve()
{
int i,j,temp;
for(i=;i<=n;i++)
{
for(j=index[i];j!=-;j=edge[j].next)
{
int temp=edge[j].to;
if(low[i]!=low[temp])
{
degree[low[i]]++;
}
}
}
return ;
}
int main()
{
int m,i,j,a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(!visit[a][b])
{
addedge(a,b);
visit[a][b]=visit[b][a]=;
}
}
int ans=;
dfs();
solve();
for(i=;i<=n;i++)
if(degree[i]==)
ans++;
printf("%d\n",(ans+)/);
}
return ;
}
poj 3177 边连通分量的更多相关文章
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
- POJ 3177 Redundant Paths(边双连通分量)
[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...
随机推荐
- 提高iOS开发效率的方法和工具
http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...
- Android问题-No resource found that matches the given name (at 'theme' with value '@style/CaptureTheme').
问题现象:在看一个实例中写到的,提示如下: [PAClient Error] Error: E2312 C:\Users\zhujq-a\Desktop\Android实例之实现扫描二维码并生成二维码 ...
- CodeForces 689B Mike and Shortcuts (bfs or 最短路)
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Spring EL method invocation example
In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...
- Spring @PostConstruct and @PreDestroy example
In Spring, you can either implements InitializingBean and DisposableBean interface or specify the in ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- c++地址对齐
在一些计算机上,由于性能方面的原因,2个字节的变量,必须放在2的倍数的地址中,4个字节的,就必须放在4的倍数中的地址中,以此类推. 也就是说,如果有3个占用两个字节的变量,分别为:A,B,C,如下图: ...
- nginx缓存优先级(缓存问题者必看)
接触nginx的兄弟或多或少都有遇到缓存问题,要么是nginx为什么不缓存,要么就是nginx缓存很快就失效等等问题,在网上找了一遍nginx缓存优先级的文章,大家可以参考下. 架构图client端 ...
- php使用openssl进行Rsa长数据加密,解密保存问题
原来代码: public function encrypt($data) { if (openssl_public_encrypt(base64_encode($data), $encrypted, ...