• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 18,  comments - 5,  trackbacks - 0

            一、題目描述

            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 roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

            The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

            Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

            So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

            Input

            The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

            Output

            One line, consisting of an integer, which gives the minimum number of roads that we need to add.

            Sample Input

            Sample Input 1
            10 12
            1 2
            1 3
            1 4
            2 5
            2 6
            5 6
            3 7
            3 8
            7 8
            4 9
            4 10
            9 10
            Sample Input 2
            3 3
            1 2
            2 3
            1 3

            Sample Output

            Output for Sample Input 1
            2
            Output for Sample Input 2
            0
            


            二、分析
                  用DFS解決問題,詳細算法:割點與橋
            三、代碼

             1#include<iostream>
             2#include<list>
             3using namespace std;
             4int n, r;
             5list<int> g[1001];
             6int num, lab[1001], low[1001];
             7list<pair<intint> > edge;
             8int degree[1001];
             9int parent[1001], rank[1001];
            10void init_set()
            11{
            12    for(int i=1; i<1001; i++)
            13    {
            14        parent[i] = i;
            15        rank[i] = 1;
            16    }

            17}

            18int find(int k)
            19{
            20    if(parent[k] == k) 
            21        return k;
            22    else
            23        return parent[k] = find(parent[k]);
            24}

            25void union_set(int u, int v)
            26{
            27    int pu = find(u), pv = find(v);
            28    if(rank[pu] <= rank[pv])
            29    {
            30        parent[pu] = pv;
            31        rank[pv] += pu;
            32    }

            33    else
            34    {
            35        parent[pv] = pu;
            36        rank[pu] += pv;
            37    }

            38}

            39void dfs(int u, int p)
            40{
            41    lab[u] = low[u] = num++;
            42    list<int>::iterator it;
            43    for(it = g[u].begin(); it != g[u].end(); it++)
            44    {
            45        int v = *it;
            46        if(lab[v] == 0)
            47        {
            48            dfs(v, u);
            49            low[u] = min(low[u], low[v]);
            50            if(low[v] > lab[u])
            51                edge.push_back(make_pair(u, v));
            52            else
            53                union_set(u, v); //u與v能進行縮點
            54        }

            55        else if(v != p)
            56            low[u] = min(low[u], lab[v]);
            57    }

            58}

            59int main()
            60{
            61    scanf("%d%d"&n, &r);
            62    for(int i=1; i<=n; i++)
            63        g[i].clear();
            64    for(int i=1; i<=r; i++)
            65    {
            66        int v1, v2;
            67        scanf("%d%d"&v1, &v2);
            68        g[v1].push_back(v2);
            69        g[v2].push_back(v1);
            70    }

            71    memset(lab, 0sizeof lab);
            72    memset(low, 0x7fsizeof low);
            73    num = 1;
            74    init_set();
            75    dfs(10);
            76    memset(degree, 0sizeof degree);
            77    list<pair<intint> >::iterator it;
            78    int res = 0;
            79    for(it = edge.begin(); it != edge.end(); it++)
            80    {
            81        int u = it->first, v = it->second;
            82        degree[find(u)]++;
            83        if(degree[find(u)] == 1)
            84            res++;
            85        else if(degree[find(u)] == 2)
            86            res--;
            87        degree[find(v)]++;
            88        if(degree[find(v)] == 1)
            89            res++;
            90        else if(degree[find(v)] == 2)
            91            res--;
            92    }

            93    printf("%d\n", (res+1/ 2);
            94}
            posted on 2009-07-05 16:08 Icyflame 閱讀(1421) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告
            精品多毛少妇人妻AV免费久久| 久久国产午夜精品一区二区三区| 亚洲国产香蕉人人爽成AV片久久 | 久久九九兔免费精品6| 99精品国产免费久久久久久下载| 97精品伊人久久久大香线蕉| 国产精品久久一区二区三区| 色播久久人人爽人人爽人人片aV| 国内精品久久久久影院日本| 久久亚洲色一区二区三区| 久久w5ww成w人免费| 亚洲国产精品成人AV无码久久综合影院 | 女人香蕉久久**毛片精品| 久久婷婷色综合一区二区| 久久精品国产91久久综合麻豆自制 | 国产成年无码久久久免费| 国产精品欧美久久久久无广告| 无码国产69精品久久久久网站| 久久精品国产清自在天天线| 伊人色综合久久天天人手人婷| 久久国产视频99电影| 国产精品成人99久久久久 | 无码乱码观看精品久久| 久久99国产精品久久99果冻传媒| 18禁黄久久久AAA片| 久久久久久久综合日本| 狠狠久久综合伊人不卡| 久久国产精品-国产精品| 国产精品久久久久国产A级| 亚洲欧美成人综合久久久 | 亚洲精品美女久久777777| 欧美国产精品久久高清| 久久精品夜色噜噜亚洲A∨| 一本一道久久精品综合| 国产精品久久久久影院嫩草| 国产午夜精品久久久久免费视| 久久精品国产亚洲av麻豆小说| 久久精品水蜜桃av综合天堂| 国产亚洲精品美女久久久| 狠狠色噜噜狠狠狠狠狠色综合久久 | 精品久久久无码人妻中文字幕|