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

            O(1) 的小樂

            Job Hunting

            公告

            記錄我的生活和工作。。。
            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統(tǒng)計

            • 隨筆 - 182
            • 文章 - 1
            • 評論 - 41
            • 引用 - 0

            留言簿(10)

            隨筆分類(70)

            隨筆檔案(182)

            文章檔案(1)

            如影隨形

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            Connected Component 無向圖連通分量

            In graph theory, a connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices. For example, the graph shown in the illustration on the right has three connected components. A graph that is itself connected has exactly one connected component, consisting of the whole graph.

             

            A graph with three connected components.

             

            顯然DFS就足夠判斷了。。BFS當(dāng)然可以了。。

             

            Code:

            #include "cstdlib"
            #include "cctype"
            #include "cstring"
            #include "cstdio"
            #include "cmath"
            #include "algorithm"
            #include "vector"
            #include "string"
            #include "iostream"
            #include "sstream"
            #include "set"
            #include "queue"
            #include "stack"
            #include "fstream"
            #include "strstream"
            using namespace std;

            #define  M 5000                       //題目中可能的最大點數(shù) 
            int DFN[M];                           //深度優(yōu)先搜索訪問次序
            int ConnectedComponetNumber=0;        //有向圖強連通分量個數(shù)
            int Belong[M];
            int Index=0;
            vector <int> Edge[M];        //鄰接表表示
            vector <int> ConnectedComponent[M];   //獲得強連通分量結(jié)果

            void DFS(int i)
            {
                DFN[i]=Index++;
                Belong[i]=ConnectedComponetNumber;
                ConnectedComponent[ConnectedComponetNumber].push_back(i);
                for (int e=0;e<Edge[i].size();e++)
                {
                    int j=Edge[i][e];
                    if (DFN[j]==-1)
                        DFS(j);
                }
            }

            void solve(int N)     //此圖中點的個數(shù),注意是0-indexed!
            {
                memset(DFN,-1,sizeof(DFN));
                memset(Belong,0,sizeof(Belong));
                for(int i=0;i<N;i++)
                    if(DFN[i]==-1)
                        ConnectedComponetNumber++,DFS(i);
            }
            void reshape(int N)
            {
                cout<<ConnectedComponetNumber<<endl;
                for(int i=0;i<N;i++)
                    cout<<Belong[i]<<" ";
                cout<<endl;
                for(int i=0;i<N;i++)
                    cout<<DFN[i]<<" ";
                cout<<endl;
                for(int i=1;i<=ConnectedComponetNumber;i++)
                {
                    for(int j=0;j<ConnectedComponent[i].size();j++)
                        cout<<ConnectedComponent[i][j]<<" ";
                    cout<<endl;
                }
            }
            /*
            此算法正常工作的基礎(chǔ)是圖是0-indexed的。
            */
            int main()
            {
                Edge[0].push_back(1);
                Edge[1].push_back(0),Edge[1].push_back(2);
                Edge[2].push_back(1);
                int N=6;
                solve(N);
                reshape(N);
                return 0;
            }

            posted on 2010-09-28 10:11 Sosi 閱讀(1227) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            統(tǒng)計系統(tǒng)
            色综合久久久久久久久五月| 国产精品久久久久久搜索| 久久91精品综合国产首页| 久久亚洲国产成人精品无码区| 漂亮人妻被中出中文字幕久久| 囯产精品久久久久久久久蜜桃| 少妇精品久久久一区二区三区| 99久久国产综合精品麻豆| 久久久久国色AV免费看图片| 久久婷婷色综合一区二区| 97久久超碰国产精品2021| 国内精品久久久久影院老司| 国产成人无码久久久精品一| 中文字幕精品久久久久人妻| 国内精品久久久久影院一蜜桃| 久久婷婷人人澡人人| 久久91精品国产91久久户| 中文字幕乱码人妻无码久久| 久久久精品波多野结衣| 91精品国产综合久久精品| 伊人久久大香线蕉综合影院首页| 久久久久综合国产欧美一区二区| 国产精品久久久久天天影视| 亚洲va久久久噜噜噜久久男同| 91秦先生久久久久久久| 精品乱码久久久久久久| 中文字幕久久波多野结衣av| 久久综合久久伊人| 大美女久久久久久j久久| 精品熟女少妇av免费久久| 久久久www免费人成精品| 中文成人无码精品久久久不卡 | 久久久久久久波多野结衣高潮 | 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 亚洲国产成人精品无码久久久久久综合 | 久久久久免费看成人影片| 久久精品极品盛宴观看| 久久男人AV资源网站| 久久精品亚洲乱码伦伦中文 | 亚洲国产成人精品女人久久久 | 午夜精品久久久久久毛片|