做法:

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

之后建树,然后求出这个树中度为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. import和require的区别

    node编程中最重要的思想就是模块化,import和require都是被模块化所使用. 遵循规范 require 是 AMD规范引入方式 import是es6的一个语法标准,如果要兼容浏览器的话必须转 ...

  2. 克隆虚拟机重启之后eth0不见的解决方案

    今天用虚拟机克隆多一个虚拟机的时候,发现克隆之后的新虚拟机的网卡eth0在配置之后完全是用不了的,下面说一下我的解决办法,亲测可用. 1.用ipconfig命令查看ip信息的时候会发现虚拟机没有找到e ...

  3. python垃圾回收三之标记清除

    #第一组循环引用# a = [1,2] b = [3,4] a.append(b) b.append(a) del a ## #第二组循环引用# c = [4,5] d = [5,6] c.appen ...

  4. VMware下centos桥接模式静态ip配置

    声明:本文转载自http://blog.csdn.net/ltr15036900300/article/details/48828207,非原创. 一.配置虚拟机centos网络 备份网络文件 [ro ...

  5. PowerMockRunner和ActiveObjectsJUnitRunner

    Jira的二次开发,需要作单元测试. 测试跟数据库连接的类,比如service类,需要在类上加@RunWith(ActiveObjectsJUnitRunner.class). 有时需要搭配mocki ...

  6. 测试开发之前端——No2.HTML5中的标签

    HTML5中的标签. 标签 描述 <!--...--> 定义注释. <!DOCTYPE>  定义文档类型. <a> 定义超链接. <abbr> 定义缩写 ...

  7. java JVM指令2

    https://www.cnblogs.com/dreamroute/p/5089513.html 指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推送 ...

  8. CSS font-family 字体介绍,\5b8b\4f53 表示“宋体”

    font-family采用一种"回退"的形式来保存字体,可以写若干种字体.当第一种字体浏览器不支持的时候,会找第二种字体,一次类推. font-family字体分为两类: 特殊字体 ...

  9. 关于 contentWindow, contentDocument

    没有永恒的技术只有变态的需求,没有好说的客户只有无奈的开发者, 如果iframe的出现是一个错误的话,iframe里边在来一个iframe那是错上加错,神话没有在远古的尘嚣中消失,却在怀具的今天不断上 ...

  10. 【AtCoder】ARC086

    C - Not so Diverse 题解 选出现次数K多的出来,剩下的都删除即可 代码 #include <bits/stdc++.h> #define fi first #define ...