zoukankan      html  css  js  c++  java
  • poj 3352(桥+缩点)

    先求出桥,然后去除桥后得到边双联通分量, 将边联通分量缩成一个点, 然后就出所有度为1的点(叶子节点)个数n,答案就是n/2+n%2. 

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 5050
    #define M 10010
    #define INF 0x3fffffff
    
    struct node
    {
        int from,to,next;
    }edge[2*M];
    
    int n,m;
    int cnt,pre[N];
    bool mark[2*M];
    bool save[2*M];
    int low[N];
    int link[N];
    int d[N];
    void add_edge(int u,int v)
    {
        edge[cnt].to=v;
        edge[cnt].from=u;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    void dfs(int s,int num)
    {
        low[s] = num;
        int mi=num;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[p]==1) continue;
            if(low[v]==-1)
            {
                mark[p]=1;
                mark[p^1]=1;
                dfs(v,num+1);
                if(low[v]>num) // 说明edge[p]是桥
                {
                    save[p]=1;
                    save[p^1]=1;
                }
            }
            mi=min(mi,low[v]);
        }
        low[s]=mi;
    }
    
    void dfs1(int s,int num)
    {
        link[s]=num;
        mark[s]=1;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[v]==1||save[p]==1) continue;
            dfs1(v,num);
        }
    }
    
    int main()
    {
        cnt=0;
        memset(pre,-1,sizeof(pre));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,y);
            add_edge(y,x);
        }
        memset(low,-1,sizeof(low));
        memset(mark,0,sizeof(mark));
        memset(save,0,sizeof(save));
        dfs(1,0);
        memset(mark,0,sizeof(mark));
        int id=0;
        for(int i=1;i<=n;i++)
        {
            if(mark[i]==0)
            {
                dfs1(i,++id);
            }
        }
        memset(d,0,sizeof(d));
        if(id==1) 
        {
            printf("0\n");
        }
        else
        {
            for(int i=0;i<cnt;i+=2)
            {
                int x=edge[i].from;
                int y=edge[i].to;
                if(link[x] != link[y])
                {
                    d[ link[x] ]++;
                    d[ link[y] ]++;
                }
            }
            int ans=0;
            for(int i=1;i<=id;i++)
            {
                if(d[i]==1)
                    ans++;
            }
            printf("%d\n",ans/2+ans%2);
        }
        return 0;
    }
    
     

    Redundant Paths
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7054   Accepted: 3075

    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

    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.

    Source

  • 相关阅读:
    lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
    lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
    lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
    lintcode:递归打印数字
    lintcode :Segmemt Tree Build II
    lintcode 中等题:find the missing number 寻找缺失的数
    lintcode 中等题: Implement Trie
    lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
    lintcode 中等题:Palindrome Linked List 回文链表
    [LeetCode] Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3088978.html
Copyright © 2011-2022 走看看