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

            統計

            • 隨筆 - 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)  編輯 收藏 引用

            統計系統
            99久久无码一区人妻a黑| 久久996热精品xxxx| 思思久久99热只有频精品66| 丁香久久婷婷国产午夜视频| 久久精品天天中文字幕人妻| 精品久久久久久无码专区不卡| 色欲久久久天天天综合网精品| 精品国产99久久久久久麻豆| 久久精品aⅴ无码中文字字幕不卡| 亚洲精品tv久久久久| 国产99久久久国产精品小说| 亚洲国产视频久久| 综合网日日天干夜夜久久| 久久综合狠狠综合久久综合88| 人妻少妇久久中文字幕一区二区| 国产三级久久久精品麻豆三级| 精品久久无码中文字幕| 久久99国产精一区二区三区| 国产精品亚洲综合专区片高清久久久 | 亚洲午夜久久久久久噜噜噜| 手机看片久久高清国产日韩| 久久人人爽人人爽人人片AV高清| 亚洲国产精品久久电影欧美| 久久精品成人免费看| 93精91精品国产综合久久香蕉 | 国产69精品久久久久99尤物| 国内精品久久久久久久涩爱| 久久天天躁夜夜躁狠狠| 久久福利青草精品资源站| 久久亚洲国产成人影院网站| 东方aⅴ免费观看久久av| 麻豆精品久久精品色综合| 色综合合久久天天给综看| 久久久久久亚洲Av无码精品专口| 久久影视国产亚洲| 精品一区二区久久| 久久久久久久久久久| 久久精品毛片免费观看| 伊人久久大香线蕉综合热线| 777午夜精品久久av蜜臀| 无码人妻久久一区二区三区蜜桃|