• poj 3177 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 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;
    #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
    ",(leaf+1)/2);
        }
        return 0;
    }

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

     
    至少做到我努力了
  • 相关阅读:
    国内首篇介绍JanOS物联网操作系统的文章
    安卓MonkeyRunner源码分析之与Android设备通讯方式
    MonkeyRunner源码分析之-谁动了我的截图?
    学习Swift写iOS?那写安卓和WinPhone呢?请看一石三鸟终极解决方案
    UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
    jdbc基础 (二) 通过properties配置文件连接数据库
    对于Servlet、Servlet容器以及一个Servlet容器-Tomcat
    5种分布式共享session的方法
    企业项目构建学习(一)maven
    SQLServer 2008以上误操作数据库恢复方法——日志尾部备份
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3735505.html
Copyright © 2020-2023  润新知