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

            公告

            記錄我的生活和工作。。。
            <2012年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            統計

            • 隨筆 - 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當然可以了。。

             

            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                       //題目中可能的最大點數 
            int DFN[M];                           //深度優先搜索訪問次序
            int ConnectedComponetNumber=0;        //有向圖強連通分量個數
            int Belong[M];
            int Index=0;
            vector <int> Edge[M];        //鄰接表表示
            vector <int> ConnectedComponent[M];   //獲得強連通分量結果

            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)     //此圖中點的個數,注意是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;
                }
            }
            /*
            此算法正常工作的基礎是圖是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 閱讀(1230) 評論(0)  編輯 收藏 引用

            統計系統
            大美女久久久久久j久久| 99久久久精品免费观看国产| 国产亚洲成人久久| 欧美久久一级内射wwwwww.| 天天综合久久一二三区| 亚洲国产精品18久久久久久| av无码久久久久不卡免费网站| 久久免费视频网站| 麻豆av久久av盛宴av| 久久精品国产免费| 久久久噜噜噜久久中文字幕色伊伊| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久精品无码一区二区日韩AV| 国内精品久久久久影院薰衣草 | 伊人久久大香线蕉综合5g| 久久国产免费直播| 久久亚洲AV无码西西人体| 97久久精品国产精品青草| 漂亮人妻被中出中文字幕久久| 久久久久久狠狠丁香| 亚洲va久久久噜噜噜久久天堂| 久久精品国产精品亚洲下载| 97久久香蕉国产线看观看| 色偷偷偷久久伊人大杳蕉| 一本一道久久a久久精品综合 | 国产精品99久久久久久猫咪| 亚洲日韩欧美一区久久久久我| 久久久久久人妻无码| 亚洲国产成人久久综合野外| 国产精品99久久久久久猫咪 | 综合久久精品色| 久久精品夜色噜噜亚洲A∨| 日本久久久精品中文字幕| 99久久超碰中文字幕伊人| 亚洲AV日韩AV永久无码久久| 色天使久久综合网天天| 久久久久青草线蕉综合超碰| 久久这里只有精品首页| 亚洲中文字幕无码久久2017| 亚洲精品乱码久久久久久蜜桃图片| 欧美亚洲国产精品久久|