青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

ACM___________________________

______________白白の屋
posts - 182, comments - 102, trackbacks - 0, articles - 0
<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用鏈接

留言簿(24)

隨筆分類(332)

隨筆檔案(182)

FRIENDS

搜索

積分與排名

最新隨筆

最新評論

閱讀排行榜

評論排行榜

并查集 學習 詳解

Posted on 2010-08-10 11:02 MiYu 閱讀(2864) 評論(2)  編輯 收藏 引用 所屬分類: ACM ( 并查集 )

 

并查集--學習詳解

文章作者:yx_th000 文章來源:Cherish_yimi (http://www.cnblogs.com/cherish_yimi/)

l         并查集:(union-find sets)

一種簡單的用途廣泛的集合. 并查集是若干個不相交集合,能夠實現較快的合并和判斷元素所在集合的操作,應用很多,如其求無向圖的連通分量個數等。最完美的應用當屬:實現Kruskar算法求最小生成樹。

l         并查集的精髓(即它的三種操作,結合實現代碼模板進行理解):

1Make_Set(x) 把每一個元素初始化為一個集合

初始化后每一個元素的父親節(jié)點是它本身,每一個元素的祖先節(jié)點也是它本身(也可以根據情況而變)。

2Find_Set(x) 查找一個元素所在的集合

查找一個元素所在的集合,其精髓是找到這個元素所在集合的祖先這個才是并查集判斷和合并的最終依據。
判斷兩個元素是否屬于同一集合,只要看他們所在集合的祖先是否相同即可。
合并兩個集合,也是使一個集合的祖先成為另一個集合的祖先,具體見示意圖

3Union(x,y) 合并x,y所在的兩個集合

合并兩個不相交集合操作很簡單:
利用Find_Set找到其中兩個集合的祖先,將一個集合的祖先指向另一個集合的祖先。如圖

l         并查集的優(yōu)化

1Find_Set(x)時 路徑壓縮
尋找祖先時我們一般采用遞歸查找,但是當元素很多亦或是整棵樹變?yōu)橐粭l鏈時,每次Find_Set(x)都是O(n)的復雜度,有沒有辦法減小這個復雜度呢?
答案是肯定的,這就是
路徑壓縮,即當我們經過"遞推"找到祖先節(jié)點后,"回溯"的時候順便將它的子孫節(jié)點都直接指向祖先,這樣以后再次Find_Set(x)時復雜度就變成O(1)了,如下圖所示;可見,路徑壓縮方便了以后的查找。

2Union(x,y)時 按秩合并
即合并的時候將元素少的集合合并到元素多的集合中,這樣合并之后樹的高度會相對較小。

l         主要代碼實現

Code
 1 int father[MAX];   /**//* father[x]表示x的父節(jié)點*/
 2 int rank[MAX];     /**//* rank[x]表示x的秩*/
 3
 4
 5 /**//* 初始化集合*/
 6 void Make_Set(int x)
 7 {
 8     father[x] = x; //根據實際情況指定的父節(jié)點可變化
 9     rank[x] = 0;   //根據實際情況初始化秩也有所變化
10 }
11
12
13 /**//* 查找x元素所在的集合,回溯時壓縮路徑*/
14 int Find_Set(int x)
15 {
16     if (x != father[x])
17      {
18         father[x] = Find_Set(father[x]); //這個回溯時的壓縮路徑是精華
19     }
20     return father[x];
21 }
22
23
24 /**//* 
25    按秩合并x,y所在的集合
26    下面的那個if else結構不是絕對的,具體根據情況變化
27    但是,宗旨是不變的即,按秩合并,實時更新秩。
28 */
29 void Union(int x, int y)
30 {
31     x = Find_Set(x);
32     y = Find_Set(y);
33     if (x == y) return;
34     if (rank[x] > rank[y]) 
35      {
36         father[y] = x;
37     }
38     else
39      {
40         if (rank[x] == rank[y])
41          {
42             rank[y]++;
43         }
44         father[x] = y;
45     }
46 }
47

 

注:學習并查集時非常感謝Slyar提供的資料,這里注明鏈接:http://www.slyar.com/blog/
另外,我認為寫并查集時涉及到的路徑壓縮,最好用遞歸,一方面代碼的可讀性非常好,另一方面,可以更直觀的理解路徑壓縮時在回溯時完成的巧妙。

 

 

PKU POJ 1161解題報告(并查集)

 

Time Limit: 1000MS

 

Memory Limit: 20000K

Total Submissions: 5572

 

Accepted: 2660

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

100 4

2 1 2

5 10 13 11 12 14

2 0 1

2 99 2

200 2

1 5

5 1 2 3 4 5

1 0

0 0

Sample Output

4

1

1

我的思路:

典型的并查集,最初各自為集,然后每個group進行合并,等到所有的group合并完,題目也就解決了,因為在合并的時候,如果哪兩個group中有重合的元素,則那個后來的group會由于按秩合并的原則自動合并到

先有的集合當中,奧妙便在其中。下面是代碼:

Code
 1 #include<iostream>
 2 using namespace std;
 3
 4 int n, m, i, j;
 5 int father[30005], num[30005];
 6
 7 void makeSet(int n)
 8 {
 9     for(i = 0; i < n; i++)
10      {
11         father[i] = i; //使用本身做根
12         num[i] = 1;
13     }
14 }
15 int findSet(int x)
16 {
17     if(father[x] != x) //合并后的樹的根是不變的
18      {    
19         father[x] = findSet(father[x]);
20     }
21     return father[x]; 
22 }
23
24 void Union(int a, int b)
25 {
26     int x = findSet(a);
27     int y = findSet(b);
28     if(x == y)
29      {
30         return;
31     }
32     if(num[x] <= num[y])
33      {
34         father[x] = y;
35         num[y] += num[x];
36     }
37     else 
38      {
39         father[y] = x;
40         num[x] += num[y];
41     }
42 }
43
44 int main()
45 {
46     while(scanf("%d %d", &n, &m)!=EOF && n != 0)
47      {
48         makeSet(n);
49         for(i = 0; i < m; i++)
50          {
51             int count, first, b;
52             scanf("%d %d",&count, &first);
53             for(j = 1; j < count; j++)
54              {
55                 scanf("%d",&b);
56                 Union(first,b);
57             }
58         }
59         printf("%d\n",num[findSet(0)]);
60     }
61     return 0;
62 }
63

另外,上面并查集的根我是采用數字本身的,然后路徑壓縮尋找父親節(jié)點是采用遞歸的,下面貼出,采用-1做根和使用非遞歸實現的部分代碼:

Code
 1 void makeSet(int n)
 2 {
 3     for(i = 0; i < n; i++)
 4      {
 5         father[i] = -1;
 6         num[i] = 1;
 7     }
 8 }
 9 //非遞歸實現
10 int findSet(int x)
11 {
12     while(father[x] != -1)   //根為-1
13      {
14         x = father[x];
15     }
16     return x;
17 }
18

 

PKU POJ 2524 解題報告(并查集)


 

Ubiquitous Religions

Time Limit: 5000MS

 

Memory Limit: 65536K

Total Submissions: 9637

 

Accepted: 4463

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.


Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.


Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.


Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1

Case 2: 7

我的思路:

有了前面學習的基礎和1161的練習,這道題完全就可以水過了,設定一個計數器,每次合并時減1便可值得一提的是:樹的秩是好東西啊,既可以記錄每個集合的節(jié)點個數,又能保證按秩合并成相對高度小的樹。

代碼如下:

Code
 1 #include<iostream>
 2 using namespace std;
 3
 4 int n, m, maxNum, i;
 5 int father[50005], num[50005];
 6
 7 void makeSet(int n)
 8 {
 9     for(int j = 1; j <= n; j++)
10      {
11         father[j] = j;
12         num[j] = 1;
13     }
14 }
15 int findSet(int x)
16 {
17     if(father[x] != x) 
18      {    
19         father[x] = findSet(father[x]);
20     }
21     return father[x]; 
22 }
23
24 void Union(int a, int b)
25 {
26     int x = findSet(a);
27     int y = findSet(b);
28     if(x == y)
29      {
30         return;
31     }
32     if(num[x] <= num[y])
33      {
34         father[x] = y;
35         num[y] += num[x];
36         maxNum--;
37     }
38     else 
39      {
40         father[y] = x;
41         num[x] += num[y];
42         maxNum--;
43     }
44 }
45
46 int main()
47 {
48     int Case = 1;
49     while(scanf("%d %d", &n, &m)!=EOF && n!=0)
50      {
51         maxNum = n;
52         makeSet(n);
53         int a, b;
54         for(i = 0; i < m; i++)
55          {
56             scanf("%d %d",&a, &b);
57             Union(a,b);
58         }
59         printf("Case %d: %d\n",Case++,maxNum);
60     }
61     return 0;
62 }
63

POJ 1611 The Suspects          最基礎的并查集

 

POJ 2524 Ubiquitous Religions 最基本的并查集

POJ 1182 食物鏈       并查集的拓展

注意: 只有一組數據;

要充分利用題意所給條件:有三類動物A,B,C,這三類動物的食物鏈

構成了有趣的環(huán)形。AB BCCA。也就是說:只有三個group

POJ 2492 A Bug's Life 并查集的拓展

法一:深度優(yōu)先遍歷

每次遍歷記錄下該點是男還是女,只有:-〉女,女-〉男滿足,否則,找到同性戀,結束程序。

法二:二分圖匹配

法三:并查集的拓展:1182很像,只不過這里就有兩組,而1182是三組,1611無限制

POJ 1861 Network == zju_1542    并查集+自定義排序+貪心求"最小生成樹"

答案不唯一,不過在ZOJ上用QSORT()SORT()都能過,在POJ上只有SORT()才能過...

POJ 1703 Find them, Catch them 并查集的拓展

這個和POJ 2492 A Bug's Life很像,就是把代碼稍微修改了一下就AC了!

注意:And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. 就是說只有兩個組。

POJ 2236 Wireless Network        并查集的應用

需要注意的地方:1、并查集;2N的范圍,可以等于10013、從N+1行開始,第一個輸入的可以是字符串。

POJ 1988 Cube Stacking            并查集很好的應用

1、與 銀河英雄傳說==NOI2002 Galaxy一樣;2、增加了一個數組behind[x],記錄戰(zhàn)艦x在列中的相對位置;3、詳細解題報告見銀河英雄傳說。

 

JOJ 1905 Freckles   == POJ 2560 最小生成樹

 

法一:Prim算法;法二:并查集實現Kruskar算法求最小生成樹

Feedback

# re: 并查集 學習 詳解  回復  更多評論   

2011-03-03 19:48 by 貝殼里的海
學習了,內容非常豐富~~~~

# re: 并查集 學習 詳解  回復  更多評論   

2011-04-30 16:35 by 游客
非常有用 感謝分享
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久www成人_看片免费不卡| 亚洲大胆美女视频| 亚洲综合欧美| 欧美成人按摩| 亚洲影视综合| 欧美视频免费在线观看| 亚洲高清不卡在线| 麻豆精品在线视频| 国内精品美女在线观看| 欧美久久影院| 最新日韩在线视频| 欧美激情视频在线免费观看 欧美视频免费一| 亚洲一区二区三区在线| 欧美午夜一区二区| 亚洲免费在线观看视频| 夜夜嗨av色综合久久久综合网| 欧美成人综合| 亚洲裸体俱乐部裸体舞表演av| 美女被久久久| 欧美成人国产va精品日本一级| 欧美一区二区在线免费播放| 国产精品亚洲人在线观看| 一本色道久久综合亚洲精品婷婷| 亚洲国产精品福利| 欧美日韩1234| 亚洲在线一区二区| 午夜精品久久久久久久久久久久久 | 欧美日韩中文字幕日韩欧美| 亚洲欧洲一区二区三区在线观看 | 美日韩精品免费| 亚洲国产精品v| 欧美成年人视频| 欧美极品在线观看| 亚洲在线播放| 久久激情视频免费观看| 亚洲国产精品一区二区www在线| 欧美黄色小视频| 欧美日韩亚洲一区二区三区在线| 亚洲一区观看| 久久激情网站| 日韩亚洲欧美成人| 亚洲男人的天堂在线| 国产综合久久| 日韩午夜在线视频| 激情综合网激情| 亚洲日本va午夜在线影院| 国产精品亚洲综合久久| 麻豆91精品| 欧美亚州韩日在线看免费版国语版| 欧美在线视频播放| 欧美a级一区| 欧美专区18| 欧美精品国产| 久久久国产精彩视频美女艺术照福利| 久久午夜视频| 午夜视频一区在线观看| 美女视频网站黄色亚洲| 亚洲中无吗在线| 久久免费观看视频| 亚洲欧美日韩精品综合在线观看| 欧美一区在线看| 在线视频亚洲一区| 麻豆国产va免费精品高清在线| 亚洲一区免费视频| 欧美3dxxxxhd| 久久婷婷麻豆| 国产精品日产欧美久久久久| 欧美高清免费| 狠狠色伊人亚洲综合网站色| 亚洲视频第一页| 99精品国产高清一区二区| 久久精品国产免费| 亚洲欧美日韩在线观看a三区| 免费视频一区二区三区在线观看| 欧美亚洲综合另类| 99精品热视频| 亚洲日本va午夜在线影院| 欧美亚洲一区二区在线观看| 91久久亚洲| 久久久久久综合网天天| 欧美一乱一性一交一视频| 欧美精品亚洲精品| 欧美激情影音先锋| 影音先锋日韩资源| 欧美一级理论片| 亚洲自拍偷拍视频| 欧美伦理视频网站| 亚洲国产精品成人综合| 亚洲国产精品黑人久久久| 欧美专区在线播放| 美女国内精品自产拍在线播放| 国产欧美日韩视频在线观看| 亚洲性线免费观看视频成熟| 亚洲素人一区二区| 欧美午夜精品久久久久久孕妇| 91久久精品国产| 99视频精品全部免费在线| 欧美黄在线观看| 亚洲精品资源美女情侣酒店| 日韩午夜在线电影| 欧美日韩一区二区三区在线| 亚洲人妖在线| 亚洲专区欧美专区| 国产精品午夜av在线| 亚洲欧美日韩中文播放| 久久久噜噜噜| 亚洲电影在线看| 欧美国产免费| 一区二区三区欧美激情| 午夜久久99| 黄色亚洲大片免费在线观看| 久久在线免费观看视频| 亚洲国产精品一区制服丝袜| 亚洲毛片在线观看.| 欧美日韩午夜精品| 亚洲一区免费在线观看| 久久米奇亚洲| 日韩一级精品视频在线观看| 欧美三级在线| 欧美中文字幕在线| 91久久午夜| 午夜视频久久久久久| 狠狠干综合网| 欧美日韩国产综合视频在线观看| 在线亚洲免费| 嫩草伊人久久精品少妇av杨幂| 亚洲精品在线观| 国产精品一级在线| 麻豆精品视频| 亚洲香蕉视频| 亚洲电影下载| 亚洲欧美日韩国产成人| 亚洲黄网站黄| 国产欧美日韩免费| 欧美日韩精品欧美日韩精品一| 小黄鸭精品aⅴ导航网站入口| 亚洲国产精品99久久久久久久久| 亚洲欧美综合国产精品一区| 亚洲国产天堂久久综合网| 国产精品蜜臀在线观看| 免费观看30秒视频久久| 亚洲免费视频一区二区| 亚洲国产成人精品女人久久久| 亚洲尤物视频网| 亚洲国产欧美一区| 欧美午夜国产| 欧美人与禽猛交乱配视频| 欧美成人精品三级在线观看| 欧美特黄视频| 欧美91视频| 欧美在线观看网址综合| 99re这里只有精品6| 免费观看30秒视频久久| 午夜精品免费在线| 亚洲桃色在线一区| 日韩视频中午一区| 亚洲高清三级视频| 狠狠久久五月精品中文字幕| 国产精品九九| 欧美午夜免费影院| 欧美日韩免费在线观看| 欧美大秀在线观看| 蜜桃av一区二区| 久久乐国产精品| 久久精品国产欧美激情| 亚洲欧美文学| 午夜精品久久久久久久白皮肤| 中文日韩欧美| 亚洲视频一二| 亚洲视频免费在线| 99亚洲一区二区| 亚洲乱码国产乱码精品精可以看 | 欧美欧美在线| 欧美黄免费看| 欧美大片免费观看在线观看网站推荐| 久久激情五月丁香伊人| 午夜精品电影| 欧美一区在线视频| 久久精品国产清高在天天线| 性久久久久久久久久久久| 亚洲私人影院在线观看| 亚洲视频一区二区| 亚洲免费在线电影| 午夜精品一区二区三区四区| 亚洲一区二区三区四区中文| 国产精品99久久久久久久久| 一区二区毛片| 亚洲综合欧美| 久久精品国产久精国产一老狼| 久久国产精品72免费观看| 欧美一区二区在线免费观看| 久久久久网站| 欧美激情精品久久久久久蜜臀| 欧美丰满高潮xxxx喷水动漫| 欧美激情影院| 国产精品美女黄网| 国产亚洲人成网站在线观看| 国产一区二区三区久久久| 亚洲成人资源网| a4yy欧美一区二区三区|