• <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 閱讀(5984) 評論(0)  編輯 收藏 引用 所屬分類: 算法題解
            一本久久a久久精品亚洲| 99久久免费国产特黄| 狠狠精品久久久无码中文字幕| 国産精品久久久久久久| 午夜精品久久久内射近拍高清| 久久久久久久波多野结衣高潮| 91精品国产91久久久久福利| 久久精品免费网站网| 久久亚洲春色中文字幕久久久| 国产精品热久久无码av| 欧美大香线蕉线伊人久久| 精品人妻伦九区久久AAA片69| 一本色道久久综合亚洲精品| 99久久国产综合精品成人影院 | 77777亚洲午夜久久多喷| 久久精品成人免费网站| 精品久久久无码21p发布| 国产91久久综合| 91精品国产9l久久久久| 久久久久久精品免费免费自慰 | 一本久久综合亚洲鲁鲁五月天| 国产亚洲精久久久久久无码 | 99久久精品免费观看国产| 久久久久免费精品国产| 久久天天躁狠狠躁夜夜av浪潮| 国产精品久久久久久影院| 色狠狠久久AV五月综合| 精品伊人久久久| 亚洲欧美久久久久9999| 精品乱码久久久久久夜夜嗨| 99久久婷婷国产综合精品草原| 久久精品国产亚洲av麻豆色欲| 久久精品一本到99热免费| 久久夜色精品国产噜噜亚洲a| 久久精品国产99久久丝袜| 久久国产视频网| 久久久综合香蕉尹人综合网| 久久久久国产精品三级网| 欧美精品丝袜久久久中文字幕 | 国产99久久精品一区二区| 99久久er这里只有精品18|