码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3177 Redundant Paths

时间:2014-05-19 12:43:06      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   c   

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 descri_ption 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

Hint

Explanation of the sample: 

One visualization of the paths is: 
1   2   3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
1   2   3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
 
Every pair of fields is, in fact, connected by two routes. 

It‘s possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
 
今天刚学了,双连通,学长就来那么一个题,感觉好不适应,哭战两个小时,终于搞定,其实AC了,我都有点不敢相信啊
思路是这样的,先求出桥,把桥去掉,得到一个深林,在缩边,再把桥放进去,这个时候得到一个边度为1的连通图,求这个树的度为一的店的个数,答案就是这个数加1,再整除2;
bubuko.com,布布扣
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f
using namespace std;

int degree[5000+10];

struct CUT_E
{
    static const int maxn=5000+10;
    int low[maxn],pre[maxn],dfs_clock,n,m,sumcut;
    int cut_edge[maxn][maxn];
    vector<int>group[maxn];

    void init()
    {
        for (int i=0;i<=n;i++)
        {
            group[i].clear();
            for (int j=0;j<=n;j++)
            cut_edge[i][j]=0;
        }
        sumcut=0; dfs_clock=0;
    }

    void addedge(int u,int v)
    {
        group[u].push_back(v);
        group[v].push_back(u);
    }

    int dfs(int u,int fa)
    {
        int lowu=pre[u]=++dfs_clock;
        for (int i=0;i<group[u].size();i++)
        {
            int v=group[u][i];
            if (!pre[v])
            {
                int lowv=dfs(v,u);
                lowu=min(lowu,lowv);
                if (lowv>pre[u]) {cut_edge[u][v]=1;cut_edge[v][u]=1;}
            }
            else if (pre[v]<pre[u] && v!=fa) lowu=min(lowu,pre[v]);
        }
        low[u]=lowu;
        return lowu;
    }

    int get_sum()
    {
        int ans=dfs(-1,1);
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        if (cut_edge[i][j]) sumcut++;
        return sumcut;
    }
};
CUT_E stable;

void update()
{
    for (int i=0;i<=stable.n;i++) stable.group[i].clear();
    for (int i=1;i<=stable.n;i++)
    for (int j=1;j<=stable.n;j++)
    if (stable.cut_edge[i][j]==2) {
        stable.group[i].push_back(j);
        stable.group[j].push_back(i);
    }
}

bool vis[5000+10];
int p[5000+10];
void DFS(int u,int fa)
{
    p[u]=fa;
    for (int i=0;i<stable.group[u].size();i++)
    {
        int v=stable.group[u][i];
        if(!vis[v])
        {
            vis[v]=true;
            DFS(v,fa);
        }
    }
}


void solve()
{
    for (int i=1;i<=stable.n;i++)
    for (int j=1;j<=stable.n;j++)
    if (stable.cut_edge[i][j]==1)
    {
        int x=p[i];
        int y=p[j];
        degree[x]++;
        degree[y]++;
    }
}

int find_leaf()
{
    int ans=0;
    for (int i=1;i<=stable.n;i++)
    if (degree[i]==2) ans++;
    return ans;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int f,r,x,y;
    while (scanf("%d%d",&f,&r)!=EOF)
    {
        stable.n=f; stable.m=r;
        stable.init();
        for (int i=1;i<=r;i++)
        {
            scanf("%d%d",&x,&y);
            stable.addedge(x,y);
            stable.cut_edge[x][y]=2;
            stable.cut_edge[y][x]=2;
        }
        int temp=stable.dfs(1,-1);//求割边
        update();//去掉割边,更新图
        memset(vis,0,sizeof(vis));vis[1]=true;
        for (int i=1;i<=f;i++)//找出每一个连通快,缩点
        if (!vis[i] || i==1)DFS(i,i);
        memset(degree,0,sizeof(degree));
        solve();
        int leaf=find_leaf();//找度为一的节点
        printf("%d\n",(leaf+1)/2);
    }
    return 0;
}
bubuko.com,布布扣

由于我用矩阵来更新度,故度为2的点才是

 

poj 3177 Redundant Paths,布布扣,bubuko.com

poj 3177 Redundant Paths

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/chensunrise/p/3735505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!