做法:

把所有的边双联通分量缩成一个点。

之后建树,然后求出这个树中度为1的点。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<stdlib.h>
#define INF_MAX 0x7fffffff
#define INF 999999
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define ll __int64
#define maxn 10001
#define maxm 100001
struct node
{
int u;
int v;
int w;
bool friend operator < (node a, node b){
return a.w < b.w;
}
}edge[maxn];
ll gcd(ll n,ll m){if(n<m) swap(n,m);return n%m==0?m:gcd(m,n%m);}
ll lcm(ll n,ll m){if(n<m) swap(n,m);return n/gcd(n,m)*m;}
vector<int>vec[maxn];
vector<int>vect[maxn];
stack<int>st;
int n,m,times,nums;
int dnf[maxn];
int low[maxn];
int instack[maxn],num[maxn];
int du[maxn],du2[maxn],vis[maxn];
void init()
{
int i;
times=1;
nums=1;
for(i=0;i<=n;i++)
{
dnf[i]=low[i]=instack[i]=0;
num[i]=0;
vis[i]=0;
vec[i].clear();
vect[i].clear();
du[i]=du2[i]=0;
}
}
void tarjan(int x,int pre)
{
int i;
low[x]=dnf[x]=times++;
instack[x]=1;
st.push(x);
int len=vec[x].size();
for(i=0;i<len;i++)
{
int y=vec[x][i];
if(y==pre)continue;
if(!dnf[y])
{
tarjan(y,x);
low[x]=min(low[x],low[y]);
}
else if(instack[y])
{
low[x]=min(low[x],dnf[y]);
}
}
if(low[x]==dnf[x])
{
int y=-1;
while(y!=x)
{
y=st.top();
st.pop();
vis[y]=nums;
num[nums]++;
instack[y]=0;
}
nums++;
}
}
void jt()
{
int i,j;
i=1;
for(i=1;i<=n;i++)
{
int len=vec[i].size();
for(j=0;j<len;j++)
{
int x=vis[i];
int y=vis[vec[i][j]];
if(x==y)continue;
du[x]++;
du[y]++;
}
}
}
int main()
{
int i,a,b;
while(~scanf("%d%d",&n,&m))
{
init();
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
for(i=1;i<=n;i++)
if(!dnf[i])tarjan(i,-1); jt();
int t=0;
for(i=1;i<nums;i++)
{
if(du[i]==2)
{
t++;
}
}
printf("%d\n",(t+1)/2);
}
return 0;
}


												

poj-3352-Road Construction-缩点的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  3. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  4. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

  5. POJ 3352 Road Construction(边—双连通分量)

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

  6. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  7. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  8. POJ 3352 Road Construction 双联通分量 难度:1

    http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...

  9. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

  10. POJ - 3352 Road Construction(边双连通分支)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板)  与这道题一模一样.代码就改了下范围,其他都没动 ...

随机推荐

  1. lucene简介——(一)

    0.概念性东西 1.数据分类

  2. python3之模块urllib

    urllib是python内置的HTTP请求库,无需安装即可使用,它包含了4个模块: request:它是最基本的http请求模块,用来模拟发送请求 error:异常处理模块,如果出现错误可以捕获这些 ...

  3. OE中的bitbake使用

    OpenEmbedded是一些脚本(shell和python脚本)和数据构成的自动构建系统.     脚本实现构建过程,包括下载(fetch).解包(unpack).打补丁(patch).config ...

  4. TAU调研咨询

    厦门宇能科技有限公司 GPRS-RTU/DTU.3/4G路由器.无线远程抄表.管网监控 咨询电话:0592-5710250 2017-07-04 9:36:16 您好,欢迎光临.请问有什么可以帮到您? ...

  5. redis 配置文件翻译

    2014年6月24日 17:29:11 include  如果有其它配置文件,可以使用 include 指令 ####通用配置 daemonize  默认的redis不会以守护进程运行,需要这样的话可 ...

  6. mybatis-config.xml 模板

    ssm模板 <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration  ...

  7. Network Principle Course Summary 001

    1.物理层 物理层 协议:RJ45.CLOCK.IEEE802.3 (中继器,集线器) 作用:通过媒介传输比特,确定机械及电气规范(比特Bit) 1.1 通信基础 数据 (data) —— 运送消息的 ...

  8. python生成器、装饰器、正则

    包子来了[4],被[mayun]吃了! 包子来了[4],被[mahuateng]吃了! 做了两个包子 包子来了[5],被[mayun]吃了! 包子来了[5],被[mahuateng]吃了! 做了两个包 ...

  9. Spring对象依赖关系处理

    Spring中给对象属性赋值 1.通过set方法给属性注入值 2.p名称空间 3.自动装配 4.注解 编写MVCModel调用userAction MVCModel public class MVCM ...

  10. vue1.0

    vue1.0学习总结   前言 使用vue已经有三.四个月了,但是只是学着使用了一些基本方法.因为现在的前端框架越来越多(Angular,React...),但是我相信万变不离其宗,很多用法框架之间还 ...