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

            POJ 3352 Road Construction 雙連通分量+縮點(diǎn)

            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
            

            Source


               

                題目大意:給定一個(gè)雙向連通的公路網(wǎng),當(dāng)某些公路路段檢修的時(shí)候可能會(huì)由于該段公路不通,可能會(huì)使某些旅游點(diǎn)之間無(wú)法通行,求至少新建多少條公路,使得任意對(duì)一段公路進(jìn)行檢修的時(shí)候,所有的旅游景點(diǎn)之間仍然暢通;

                分析:檢修某一路段導(dǎo)致公路網(wǎng)不暢通的原因必然是該段公路在圖中是橋(割邊),因此完全暢通的方法就是,加最若干條邊,使圖中不存在橋。先找出圖中所有的雙連通分量,將雙連通分量進(jìn)行縮點(diǎn),得到一個(gè)樹(shù)形圖,求出這個(gè)樹(shù)形圖中度為1的點(diǎn)的個(gè)數(shù),新加邊的條數(shù)即是(度為1的點(diǎn)數(shù)目+1)/2,考慮到題目只要求求度為1的點(diǎn)數(shù)目,因此可以部分縮點(diǎn),利用并查集,保存每個(gè)割邊的頂點(diǎn),統(tǒng)計(jì)每個(gè)頂點(diǎn)在并查集中的代表元的度數(shù)即可。
                Sample 1中存在4個(gè)雙連通分量:{1},{2,5,6},{3,7,8},{4,9,10},進(jìn)行縮點(diǎn)之后,求得一個(gè)4個(gè)節(jié)點(diǎn)的樹(shù)形圖,其中一個(gè)點(diǎn)的度數(shù)為3,其余3個(gè)點(diǎn)的度數(shù)為1,得到需要加的邊的數(shù)目為(3+1)/2=2。

            #include <iostream>
            #include 
            <vector>
            using namespace std;

            const int MAXN = 5001;
            vector
            < vector<int> > adj;
            int cnt,low[MAXN],pre[MAXN],visit[MAXN],degree[MAXN];

            void dfs(int u,int v){
                visit[u]
            =1;
                pre[u]
            =cnt++,low[u]=pre[u];
                
            int i,len=adj[u].size();
                
            for(i=0;i<len;i++){
                    
            if(adj[u][i]==v) continue;
                    
            if(!visit[adj[u][i]]) dfs(adj[u][i],u);
                    
            if(low[adj[u][i]]<low[u]) low[u]=low[adj[u][i]];
                }

                visit[u]
            =2;
            }

            int main(){
                
            int i,j,u,v,n,m,len,ans;
                scanf(
            "%d %d",&n,&m);
                adj.assign(n
            +1,vector<int>());
                
            while(m--){
                    scanf(
            "%d %d",&u,&v);
                    adj[u].push_back(v),adj[v].push_back(u);
                }

                memset(visit,
            0,sizeof(visit));
                cnt
            =0,dfs(1,1);
                memset(degree,
            0,sizeof(degree));
                
            for(i=1;i<=n;i++){
                    len
            =adj[i].size();
                    
            for(j=0;j<len;j++)
                        
            if(low[i]!=low[adj[i][j]])
                            degree[low[i]]
            ++;
                }

                
            for(ans=i=0;i<=n;i++)
                    
            if(degree[i]==1) ans++;
                printf(
            "%d\n",(ans+1)/2);
                
            return 0;
            }

            posted on 2009-05-29 18:53 極限定律 閱讀(1574) 評(píng)論(3)  編輯 收藏 引用 所屬分類(lèi): ACM/ICPC

            評(píng)論

            # re: POJ 3352 Road Construction 雙連通分量+縮點(diǎn) 2009-08-03 10:47 路人

            11 14
            1 2
            1 3
            1 4
            2 5
            6 11
            2 6
            5 6
            5 11
            3 7
            3 8
            7 8
            4 9
            4 10
            9 10

            你的程序,這組數(shù)據(jù)時(shí)錯(cuò)誤的。。  回復(fù)  更多評(píng)論   

            # re: POJ 3352 Road Construction 雙連通分量+縮點(diǎn) 2009-08-04 09:46 mythit

            @路人
            多謝指正  回復(fù)  更多評(píng)論   

            # re: POJ 3352 Road Construction 雙連通分量+縮點(diǎn) 2011-07-25 19:14 過(guò)客

            void dfs(int u,int v){
            visit[u]=1;
            pre[u]=cnt++,low[u]=pre[u];
            int i,len=adj[u].size();
            for(i=0;i<len;i++){
            if(adj[u][i]==v) continue;
            if(!visit[adj[u][i]]) dfs(adj[u][i],u);
            if(low[adj[u][i]]<low[u]) low[u]=low[adj[u][i]];
            }
            visit[u]=2;
            }
            最后一句 visit[u]=2;有什么用啊,求解釋  回復(fù)  更多評(píng)論   

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類(lèi)

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久综合亚洲色一区二区三区| 久久99精品国产自在现线小黄鸭| 狠狠精品久久久无码中文字幕| 久久水蜜桃亚洲av无码精品麻豆 | 久久久久久精品免费免费自慰| 久久国产精品无码HDAV| 精品综合久久久久久88小说| 伊人久久大香线蕉av不卡| 久久996热精品xxxx| 999久久久无码国产精品| 久久天天躁狠狠躁夜夜不卡| 99久久国产综合精品网成人影院 | 精品久久久久久无码国产| 久久九九精品99国产精品| 香蕉久久影院| 久久精品免费一区二区三区| 久久丫精品国产亚洲av不卡| 久久午夜免费视频| 久久久久香蕉视频| 91麻豆精品国产91久久久久久 | 久久受www免费人成_看片中文| 久久久久久久尹人综合网亚洲| 亚洲第一极品精品无码久久| 2019久久久高清456| 青青草原综合久久大伊人导航| 久久国产美女免费观看精品| 久久精品一区二区| 精品久久久久久| 久久国产免费观看精品| 国产精品18久久久久久vr| 久久ZYZ资源站无码中文动漫| 亚洲AV无码久久寂寞少妇| 99精品国产99久久久久久97 | 久久久久久国产精品美女| 久久久久女人精品毛片| 亚洲精品乱码久久久久久| 狠狠色狠狠色综合久久| 久久综合综合久久综合| 久久国产精品77777| 国产亚洲色婷婷久久99精品| 999久久久无码国产精品|