做法:

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

之后建树,然后求出这个树中度为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. G - DNA sequence HDU - 1560

    题目链接: https://vjudge.net/contest/254151#problem/G AC代码: #include<iostream> #include<cstring ...

  2. 通过图片获取gps地理位置

    别人说通过一张照片就可以定位你的位置,看来个视频,仔细研究了一下自己的照片没想到真的可以做到,想想真的有点可怕. 如何通过一张照片去定位这张照片的经纬度下面我以我手机中的照片为例. 我们通过pytho ...

  3. mongo批量操作存在更新否则插入

    def save_data(ok_ps): ns = [] for ok in ok_ps: ok['last_use_time'] = 0 ok['protocol'] = 0 # 协议类型 0:h ...

  4. phantomjs waitFor

    function waitFor(testFx, onReady, timeOutMillis) { var maxtimeOutMillis = timeOutMillis ? timeOutMil ...

  5. oracle日期、转换函数

    转换函数 日期类型转换成字符类型 select to_char(sysdate) s1, --14-3月 -16        to_char(sysdate, 'yyyy-mm-dd') s2, - ...

  6. springcloud使用Hystrix实现微服务的容错处理

    使用Hystrix实现微服务的容错处理 容错机制 如果服务提供者相应非常缓慢,那么消费者对提供者的请求就会被强制等待,知道提供者相应超时.在高负载场景下,如果不作任何处理,此类问题可能会导致服务消费者 ...

  7. TcxGrid 去除<No data to display>

  8. VS Code 折腾记 - (5) Angular 2+ && Typescript 2 + 必备插件推荐

    前言 说起来我会用VSCode,有很大一方面是因为工作需求[以前主力工具是Atom],刚好公司的前端技术栈是NG2+TS2;对于喜欢折腾的我,裸奔的VSCODE是不可以接受的.so-. eg: vsc ...

  9. CF 554A 字符串水题

    给出一个字符串,问再加入一个字母,最多能形成多少种字符串 inputaoutput51inputhioutput76 # include <iostream> # include < ...

  10. vs2010下sort比较函数链接错误问题

    环境:win7 + vs2010 + C++ 实现vector的sort算法,在类的头文件中写入比较函数时会出现链接错误: error LNK2005: "bool __cdecl comp ...