poj3177 && poj3352 边双连通分量缩点

Redundant Paths

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12676   Accepted: 5368

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

题意:

给你一张无向图,判断至少要加多少边,才能使任意2个点间至少有2条相互独立(无公共边)的道路;

思路:

在同一个双连通分量中的点可以等价于一个点,原图就成了一棵树,那么问题就转化为树中加多少条边可以成为双连通图。答案 = (树中度为1的边个数 + 1) / 2;

/*
 * Author:  sweat123
 * Created Time:  2016/6/21 20:07:00
 * File Name: main.cpp
 */
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 5010;
struct node{
    int to;
    int next;
}edge[MAXN<<2];
int pre[MAXN],vis[MAXN],pa[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int px[MAXN],py[MAXN];
int pcnt;
void add(int x,int y){
    edge[ind].to = y;
    edge[ind].next = pre[x];
    pre[x] = ind ++;
}
int find(int x){
    if(pa[x] != x)pa[x] = find(pa[x]);
    return pa[x];
}
void dfs(int rt,int k,int fa){
    dfn[rt] = low[rt] = k;
    for(int i = pre[rt]; i != -1; i = edge[i].next){
        int t = edge[i].to;
        if(!dfn[t] && t != fa){
            dfs(t,k+1,rt);
            low[rt] = min(low[rt],low[t]);
            if(low[t] > dfn[rt]){
                px[pcnt] = rt,py[pcnt++] = t;
            } else {
                int fx = find(t);
                int fy = find(rt);
                if(pa[fx] != pa[fy]){
                    pa[fx] = fy;
                }
            }
        } else if(t != fa){//bridge is differenet from point
            low[rt] = min(low[rt],dfn[t]);
        }
    }
}
int d[MAXN],f[MAXN];
int main(){
    while(~scanf("%d%d",&n,&m)){
        ind = 0;
        pcnt = 0;
        memset(pre,-1,sizeof(pre));
        for(int i = 1; i <= m; i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y),add(y,x);
        }
        for(int i = 1; i <= n; i++){
            pa[i] = i;
        }
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        dfs(1,1,-1);
        //for(int i = 1; i <= n; i++){
            //cout<<dfn[i]<<‘ ‘<<low[i]<<endl;
        //}
        //cout<<endl;
        memset(d,0,sizeof(d));
        memset(f,-1,sizeof(f));
        int pnum = 0;
        for(int i = 1; i <= n; i++){
            int fx = find(i);
            if(f[fx] == -1)f[fx] = ++pnum;
            f[i] = f[fx];
        }
        for(int i = 0; i < pcnt; i++){
            d[f[px[i]]] ++,d[f[py[i]]] ++;
        }
        int ans = 0;
        for(int i = 1; i <= pnum; i++){
            if(d[i] == 1)ans ++;
        }
        printf("%d\n",(ans + 1) / 2);
    }
    return 0;
}
时间: 2024-12-28 00:27:02

poj3177 && poj3352 边双连通分量缩点的相关文章

poj3177(边双连通分量+缩点)

传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点. 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,因此同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图的桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加

POJ 3177 Redundant Paths 边双连通分量+缩点

题目链接: poj3177 题意: 给出一张连通图,为了让任意两点都有两条通路(不能重边,可以重点),至少需要加多少条边 题解思路: 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,所以同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加边数=(树中度为1的节点数+1)/2 具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上

POJ3177 Redundant Paths 双连通分量

Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

【HDOJ4612】【双连通分量缩点+找树的直径】

http://acm.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 8309    Accepted Submission(s): 1905 Problem Description N planets are connected by M bidi

POJ3177 Redundant Paths (双联通缩点)

求对于给定一个连通图,加多少条边可以变成边双连通图. 一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树.需要加的边为(leaf+1)/2 (leaf为叶子结点个数). 对于此题,有重边但重边不加入计算. 重边的话,要么在开始去掉,要么用桥来计算入度. 因为桥不属于任何一个边双连通分支,其余的边和每个顶点都属于且只属于一个边双连通分支.对于重边而言,只有一对边被标记为桥,而对于所有重边来言,belong[u]和belong[v]都是不一样的,那么如果用belong[u]!

HDU 4612 Warm up(双连通分量缩点+求树的直径)

思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v,一定是直径的一个端点(证明从略),第二次以点v为开头进行bfs,求出的最后一个点,就是直径的另一个端点,记录深度就是我们要求的长度.我这里是使用的bfs+dfs,是一样的,少开一个deep数组,节省一下空间吧…… 其实我一开始是不会求的,我以为随便一个叶子节点就可以做端点,交上去WA,当时还好奇感觉

POJ 3177 Redundant Paths(边双连通分量)

[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化为给出一棵树,加边使得其成为边双连通分量的最小边数, 只要从叶节点连一条边到任意节点,那么就可以使得这个叶节点加入到双连通分量中, 那么优先叶节点和叶节点连接,所以其答案为(叶节点+1)/2 [代码] #include <cstdio> #include <algorithm> #in

【POJ 3177】Redundant Paths(双连通分量)

求出每个双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map> using namespace std; const int N = 5010; const int M = 10010; struct Edge { int