• <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 閱讀(5977) 評論(0)  編輯 收藏 引用 所屬分類: 算法題解
            青青青青久久精品国产h| 久久久久噜噜噜亚洲熟女综合| 热久久国产精品| 国产成人久久久精品二区三区| 伊人伊成久久人综合网777| 久久人人爽人人人人爽AV | 国产成人精品综合久久久| 久久综合九色综合97_久久久| 午夜视频久久久久一区| 91精品日韩人妻无码久久不卡 | 亚洲国产精品狼友中文久久久| 无码AV中文字幕久久专区| 国产精品狼人久久久久影院| 午夜精品久久久久| 99久久精品免费观看国产| 久久无码AV一区二区三区| 久久中文字幕人妻丝袜| 婷婷久久综合九色综合98| 久久久久久久久波多野高潮| 国产精品久久久久久久久久免费| 久久丫精品国产亚洲av| 亚洲精品无码成人片久久| 99久久免费只有精品国产| 国产成人精品久久一区二区三区| 无码国内精品久久人妻| 思思久久99热只有频精品66| 精品一久久香蕉国产线看播放| 久久精品国产99久久香蕉| 97久久久久人妻精品专区| 久久久久亚洲精品无码蜜桃| 久久婷婷激情综合色综合俺也去| 欧美成a人片免费看久久| 久久精品国产亚洲αv忘忧草| 久久91这里精品国产2020| 伊人热人久久中文字幕| 久久久久久久综合日本亚洲| 久久国产亚洲高清观看| 国产成人久久精品一区二区三区| 女人香蕉久久**毛片精品| 日本免费久久久久久久网站| 欧美综合天天夜夜久久|