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


                題意大意:一群牛將被在一個特定路徑構(gòu)成的農(nóng)場上遷移,每兩塊農(nóng)場之間都至少有一條通道,這些牛要求每兩塊路徑至少要有兩條通道,求最少需要修建多少條路才能滿足要求。
                這題的解法與http://www.shnenglu.com/mythit/archive/2009/05/29/86082.html完全一樣,只是題目中說了圖中有可能存在平行邊,這里必須判斷一下。我還是很偷懶的用了STL里的vector模擬鄰接矩陣,并且開了個5001*5001的bool數(shù)組判斷平行邊。結(jié)果導(dǎo)致代碼的效率和空間消耗都很大,110MS和將近24M的內(nèi)存空間。如果自己建圖的話,效率能提高很多。

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

            const int MAXN = 5001;
            vector
            < vector<int> > adj;
            bool hash[MAXN][MAXN];
            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;
                
            while(scanf("%d %d",&n,&m)!=EOF){
                    adj.assign(n
            +1,vector<int>());
                    memset(hash,
            false,sizeof(hash));
                    
            while(m--){
                        scanf(
            "%d %d",&u,&v);
                        
            if(!hash[u][v]){
                            hash[u][v]
            =true;
                            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-30 01:18 極限定律 閱讀(1533) 評論(4)  編輯 收藏 引用 所屬分類: ACM/ICPC

            評論

            # re: POJ 3177 Redundant Paths 雙連通分量+縮點 2009-08-14 09:53 zeus

            省去hash可以這樣判重空間小很多 時間沒多多少 依然0ms
            bool isok( int u, int v )//判重
            {
            for ( int i= 0; i< g[u].size(); ++i )
            if ( g[u][i]== v ) return false;

            return true;
            }  回復(fù)  更多評論   

            # re: POJ 3177 Redundant Paths 雙連通分量+縮點 2009-08-14 20:55 極限定律

            我也想這樣做的,不過怕時間效率變低,就偷懶直接HASH了@zeus
              回復(fù)  更多評論   

            # re: POJ 3177 Redundant Paths 雙連通分量+縮點 2011-04-28 09:30 Icyeye

            拜讀了哈,幫助很大,謝啦^-^
            但是有一點,那個visit[u]=2不知道有什么用,但注釋掉后能快三分之二左右的時間~~  回復(fù)  更多評論   

            # re: POJ 3177 Redundant Paths 雙連通分量+縮點[未登錄] 2012-07-31 20:48 bigrabbit

            樓主,我發(fā)現(xiàn)個問題。這組數(shù)據(jù)對于下面的數(shù)據(jù)
            5 6
            1 2
            1 3
            2 3
            3 4
            3 5
            4 5
            輸出的low數(shù)組是 0 0 0 1 1
            是不對的,應(yīng)該是0 0 0 0 0,你建圖的方式很奇怪,我也看不懂你到底是怎么建圖的。可以解釋下嗎?我直接用vector<int> edg[]搞的,刪除重邊。  回復(fù)  更多評論   

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

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            少妇人妻综合久久中文字幕| 久久精品国产精品亚洲精品| 97精品国产97久久久久久免费| 国产精品无码久久久久| 久久精品国产99久久香蕉| 伊人久久综合无码成人网| 久久er99热精品一区二区| 国产一级持黄大片99久久 | 色婷婷综合久久久久中文字幕| 狠狠色丁香久久婷婷综合蜜芽五月| 久久影院综合精品| 国产免费久久久久久无码| 伊人久久综合精品无码AV专区| 国产A级毛片久久久精品毛片| 久久久久久国产精品美女| 色噜噜狠狠先锋影音久久| 午夜精品久久久久久久| 久久www免费人成精品香蕉| 久久丫精品国产亚洲av不卡| 亚洲精品tv久久久久| 四虎国产永久免费久久| 国产成人精品白浆久久69| 综合网日日天干夜夜久久| 久久亚洲国产成人精品无码区| 99久久国产综合精品麻豆| 久久精品日日躁夜夜躁欧美| 久久AⅤ人妻少妇嫩草影院| 7777久久亚洲中文字幕| 亚洲精品乱码久久久久久蜜桃不卡| 亚洲精品国产自在久久| 久久亚洲国产成人影院网站| 国产精品丝袜久久久久久不卡| 99久久免费国产特黄| 久久美女网站免费| 久久免费精品视频| 国产精品欧美亚洲韩国日本久久 | 久久综合视频网站| 久久精品国产福利国产琪琪| 国产精品成人99久久久久| 青青热久久综合网伊人| 久久996热精品xxxx|