做法:

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

之后建树,然后求出这个树中度为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. python垃圾回收之分代回收

    可参考vamei的博客和https://www.jianshu.com/p/1e375fb40506

  2. Java内存模型-final域的内存语义

    一 引言 说到final你肯定知道它是Java中的关键字,那么它所在Java中的作用你知道吗?不知道的话,请前往这篇了解下https://www.cnblogs.com/yuanfy008/p/802 ...

  3. centos7执行 wget命令: command not found的两种解决方法

    1.rpm 安装 下载wget的RPM包: http://mirrors.163.com/centos/6.8/os/x86_64/Packages/wget-1.12-8.el6.x86_64.rp ...

  4. Go 2 Draft Designs

    Go 2 Draft Designs 28 August 2018 Yesterday, at our annual Go contributor summit, attendees got a sn ...

  5. 钉钉机器人-实现监控通知功能-python

    1. 首先得创建有 一个 钉钉群.(因为只能发群通知) 2. 添加机器人,得到一个url: 3. 开始写Python脚本: # -*- coding: utf-8 -*- ""&q ...

  6. zoj 3819(2014牡丹江现场赛 A题 )

    题意:给出A班和B班的学生成绩,如果bob(A班的)在B班的话,两个班级的平均分都会涨.求bob成绩可能的最大,最小值. A班成绩平均值(不含BOB)>A班成绩平均值(含BOB) &&a ...

  7. The last access date is not changed even after reading the file on Windows 7

    https://superuser.com/questions/251263/the-last-access-date-is-not-changed-even-after-reading-the-fi ...

  8. vue后台管理框架

    vue后台管理框架 系列教程<一步步带你做vue后台管理框架>第一课 github地址:vue-framework-wz 线上体验地址:立即体验 在如今的科技公司中有很多前端的需求都是要写 ...

  9. 【struts2基础】配置详解

    一.struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做Action ...

  10. 日志收集框架flume的安装及简单使用

    flume介绍 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到的数据输出到HDFS.hbase.h ...