• <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 - 33,  comments - 33,  trackbacks - 0

            題意:判斷一個(gè)給定的圖,沒有環(huán),而且存在一個(gè)鏈,圖上的所有點(diǎn)或者在這條鏈上或者在其的鄰居
            題解:
            1.判斷環(huán):
            對(duì)于無向圖:如果 點(diǎn) < 邊 + 1,則存在環(huán);
            然后使用并查集進(jìn)一步判斷環(huán)的存在

            2.判斷是否存在鏈
            首先統(tǒng)計(jì)各個(gè)點(diǎn)的度,然后對(duì)于度為1的點(diǎn),將其相連的邊刪掉,再統(tǒng)計(jì)新圖的度,這時(shí)新圖應(yīng)該剩下一條鏈,
            也就是說,新圖的不存在大于2個(gè)度為1的點(diǎn),而且這個(gè)點(diǎn)在舊圖的度是大于1的。

            代碼:

            #include <stdio.h>
            #include 
            <string.h>
            #include 
            <vector>
            #include 
            <queue>

            using namespace std;

            const int N = 105;

            vector
            <int> graphs[N];
            int deg[N];
            int degOld[N];
            int n,e;
            int cnt;

            class UnionSet
            {
            private:
                
            int parent[N];
                
            int rank[N];
                
            int size;
            public:
                UnionSet()
                
            {
                    size 
            = 0;
                }


                UnionSet(
            int _size)
                
            {
                    init(_size);
                }

                
            ~UnionSet()
                
            {

                }


                
            void init(int _size)
                
            {
                    size 
            = _size;
                    
            for (int i = 0; i < size; ++i)
                    
            {
                        parent[i] 
            = -1;
                        rank[i] 
            = 1;
                    }

                }


                
            void adjust(int _root,int _x)
                
            {
                    
            int i = _x;
                    
            int j ;
                    
            while(parent[i] >= 0)
                    
            {
                        j 
            = parent[i];
                        parent[i] 
            = _root;
                        i 
            = j;
                    }

                }


                
            int getRoot(int _x)
                
            {
                    
            int r = _x;
                    
            while(parent[r] >= 0)
                    
            {
                        r
            = parent[r];
                    }


                    
            //adjust
                    adjust(r,_x);
                    
            return r;

                }


                
            void join(int _r1,int _r2)
                
            {
                    
            if (_r1 == _r2)
                    
            {
                        
            return ;
                    }

                    
            int root1 = getRoot(_r1);
                    
            int root2 = getRoot(_r2);
                    
            if (root1 == root2)
                    
            {
                        
            return ;
                    }

                    
            if (rank[root1] > rank[root2])
                    
            {
                        parent[root2] 
            = root1;
                        rank[root1] 
            += rank[root2];
                    }

                    
            else
                    
            {
                        parent[root1] 
            = root2;
                        rank[root2] 
            += rank[root1];
                    }


                }


                
            int getRank(int _x)
                
            {
                    
            return rank[_x];
                }


            }
            ;

            static UnionSet uSet;



            void Test()
            {
                memset(deg,
            0,sizeof(deg));
                
            for (int i = 0; i < N; ++i)
                
            {
                    graphs[i].clear();
                }

                
            int a,b;
                uSet.init(n
            +1);
                
            for (int i = 0; i < e; ++i)
                
            {
                    scanf(
            "%d %d",&a,&b);
                    deg[a]
            ++;
                    deg[b]
            ++;
                    graphs[a].push_back(b);
                    graphs[b].push_back(a);
                    uSet.join(a,b);
                }

                
            if (n < e + 1)
                
            {
                    printf(
            "Graph %d is not a caterpillar.\n", cnt);
                    
            return ;
                }

                
            for (int i = 2; i <= n; ++i)
                
            {
                    
            if (uSet.getRoot(1!= uSet.getRoot(i))
                    
            {
                        printf(
            "Graph %d is not a caterpillar.\n", cnt);
                        
            return ;
                    }

                }


                
            for (int i = 1; i <= n; ++i)
                
            {
                    degOld[i] 
            = deg[i];
                }


                
            for (int i = 1; i <= n; ++i)
                
            {
                    
            if (degOld[i] == 1)
                    
            {
                        
            for (int j = 0; j < graphs[i].size(); ++j)
                        
            {
                            deg[graphs[i][j]]
            --;
                        }

                    }

                }


                
            int tmp = 0;
                
            for (int i = 1; i <= n; ++i)
                
            {
                    
            if(degOld[i] > 1 && deg[i] == 1)
                        
            ++tmp;
                }

                
            if(tmp > 2)
                    printf(
            "Graph %d is not a caterpillar.\n", cnt);
                
            else
                    printf(
            "Graph %d is a caterpillar.\n", cnt);

            }


            int main()
            {
                
            //freopen("data.txt","r",stdin);
                cnt = 0;
                
            while(scanf("%d",&n) != EOF)
                
            {
                    
            if(n == 0)
                        
            break;
                    scanf(
            "%d",&e);
                    
            ++cnt;

                    Test();
                }

                
            return 0;
            }


             

            posted on 2011-11-17 10:50 bennycen 閱讀(5984) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 算法題解
            国产精品99久久久精品无码| 香蕉久久夜色精品国产2020| 合区精品久久久中文字幕一区| 狠狠色丁香久久综合五月| 国产综合成人久久大片91| 日韩欧美亚洲国产精品字幕久久久| 久久久久亚洲AV无码专区首JN | 99久久国产亚洲综合精品| 亚洲国产精品久久电影欧美| 72种姿势欧美久久久久大黄蕉 | 久久国产高清一区二区三区| 国产精品成人久久久| 麻豆精品久久久一区二区| 国产午夜久久影院| 久久亚洲春色中文字幕久久久| 久久噜噜久久久精品66| 久久久久久极精品久久久| 超级碰久久免费公开视频| 精品综合久久久久久88小说| 国产成人精品久久免费动漫| 亚洲中文字幕久久精品无码喷水 | 国产V亚洲V天堂无码久久久 | 尹人香蕉久久99天天拍| 欧美久久久久久午夜精品| 精品无码久久久久久久久久| 国产欧美久久久精品影院| 久久精品国产精品亚洲精品| 日本精品久久久久中文字幕8| 综合人妻久久一区二区精品| 久久电影网2021| 久久人妻无码中文字幕| 国产999精品久久久久久| 色综合久久综精品| 久久国产高潮流白浆免费观看| 无码乱码观看精品久久| 久久亚洲精品国产亚洲老地址| 久久不射电影网| 亚洲va国产va天堂va久久| 成人国内精品久久久久影院VR| 国产精品gz久久久| 99久久99久久久精品齐齐|