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

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

            2.判斷是否存在鏈
            首先統計各個點的度,然后對于度為1的點,將其相連的邊刪掉,再統計新圖的度,這時新圖應該剩下一條鏈,
            也就是說,新圖的不存在大于2個度為1的點,而且這個點在舊圖的度是大于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 閱讀(5999) 評論(0)  編輯 收藏 引用 所屬分類: 算法題解
            久久久久久九九99精品| 人人狠狠综合久久亚洲| 国产婷婷成人久久Av免费高清| 精品久久久久久久久午夜福利| 精品国产91久久久久久久a| 久久夜色精品国产| 国产精品久久久久国产A级| 久久免费视频6| 精品蜜臀久久久久99网站| 久久99精品国产麻豆婷婷| 亚洲熟妇无码另类久久久| 精品无码久久久久久久动漫| 久久久久人妻一区精品色| 久久国产精品偷99| 国产精品青草久久久久婷婷 | 久久久免费精品re6| 精品久久久久久久中文字幕| 亚洲精品国产美女久久久| 久久e热在这里只有国产中文精品99| 色欲久久久天天天综合网| 亚洲精品无码久久不卡| 日本三级久久网| 久久久久AV综合网成人| 欧美伊人久久大香线蕉综合| 久久WWW免费人成—看片| 久久国产精品99久久久久久老狼| 一本一本久久a久久综合精品蜜桃| 久久精品国产色蜜蜜麻豆| 国产高清国内精品福利99久久| 久久亚洲国产精品一区二区| 久久精品无码专区免费青青| 久久久久久曰本AV免费免费| 久久精品免费全国观看国产| 三级韩国一区久久二区综合| 久久久久亚洲AV成人网人人软件 | 久久天天躁狠狠躁夜夜躁2O2O| 国产精品成人久久久| 2021久久精品免费观看| 思思久久精品在热线热| 久久久久亚洲AV成人网人人网站 | 久久精品国产一区二区三区|