UVA 10583 - Ubiquitous Religions

Ubiquitous Religions

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

題目大意

當今世界有很多不同的宗教,很難通曉他們。你有興趣找出在你的大學(xué)里有多少種不同的宗教信仰。
你知道在你的大學(xué)里有n個學(xué)生(0 < n <= 50000) 燥滑。你無法詢問每個學(xué)生的宗教信仰俯抖。此外,許多學(xué)生不想說出他們的信仰臂容。避免這些問題的一個方法是問m(0 <= m <= n(n - 1)/ 2)對學(xué)生, 問他們是否信仰相同的宗教( 例如他們可能知道他們兩個是否去了相同的教堂) 嗅剖。在這個數(shù)據(jù)中,你可能不知道每個人信仰的宗教忆蚀,但你可以知道校園里最多可能有多少個不同的宗教矾利。假定每個學(xué)生最多信仰一個宗教。

Input

第一行第一個數(shù)字代表宗教總數(shù)馋袜,第二個為組數(shù)

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 50000;
int fa[N], deep[N];

void init()
{
    memset(fa, -1, sizeof(fa));
    memset(deep, 0, sizeof(deep));
}

int find(int x)
{
    if (fa[x] == -1) return x;
    return fa[x] = find(fa[x]);
}

void unite(int x, int y)
{
    x = find(x);y = find(y);
    if (x == y) return;
    if (deep[x]<deep[y])
        fa[x] = y;
    else
    {
        fa[y] = x;
        if (deep[x] = deep[y])
            deep[x]++;
    }
}

bool same(int x, int y)
{
    return find(x) == find(y);
}

int main()
{
    int n, m;//n個宗教 m組
    int x, y;//學(xué)生x 學(xué)生y
    int fx, fy;//學(xué)生x y的父節(jié)點
    int c = 1;//case 計數(shù)
    while (scanf_s("%d%d", &n, &m) && n != 0 && m != 0)
    {
        init();//初始化
        while (m--) {
            scanf_s("%d%d", &x, &y);
            fx = find(x), fy = find(y);//設(shè)置x y的父節(jié)點
            if (fx!=fy) //如果父節(jié)點不相等 則合并
            {
                unite(fx, fy);
                n--; //宗教數(shù)-1
            }

        }
        printf("Case %d: %d\n", c++, n);
    }
    return 0;
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末男旗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子欣鳖,更是在濱河造成了極大的恐慌察皇,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泽台,死亡現(xiàn)場離奇詭異什荣,居然都是意外死亡,警方通過查閱死者的電腦和手機师痕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門溃睹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人胰坟,你說我怎么就攤上這事因篇。” “怎么了笔横?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵竞滓,是天一觀的道長。 經(jīng)常有香客問我吹缔,道長商佑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任厢塘,我火速辦了婚禮茶没,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晚碾。我一直安慰自己抓半,他們只是感情好,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布格嘁。 她就那樣靜靜地躺著笛求,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上探入,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天狡孔,我揣著相機與錄音,去河邊找鬼蜂嗽。 笑死苗膝,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的植旧。 我是一名探鬼主播荚醒,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼隆嗅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起侯繁,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤胖喳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后贮竟,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丽焊,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年咕别,在試婚紗的時候發(fā)現(xiàn)自己被綠了技健。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡惰拱,死狀恐怖雌贱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情偿短,我是刑警寧澤欣孤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站昔逗,受9級特大地震影響降传,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勾怒,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一婆排、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧笔链,春花似錦段只、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春鹦赎,著一層夾襖步出監(jiān)牢的瞬間谍椅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工古话, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留雏吭,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓陪踩,卻偏偏與公主長得像杖们,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子肩狂,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評論 0 23
  • 昨天俺沒課摘完,老劉同志說要和俺出游,說是出游傻谁,實際上就是他騎上電驢子(他戲稱電瓶車為電驢子)帶著俺在家周邊轉(zhuǎn)轉(zhuǎn)孝治,這是...
    鐵嫵閱讀 344評論 25 16
  • 中午吃飯的時候和兒子聊天,兒子讓我用一個詞形容一下當前的天氣审磁,望著室外炙熱的陽光以及41°的高溫谈飒,我裝作滿腹經(jīng)綸的...
    孫sunny閱讀 970評論 0 0
  • 參加一個新的團體,由于剛成立态蒂,什么都在初期尚未成型杭措,因一些特殊原因加入,希望自己能有所收獲钾恢。 萬事開頭難手素,不放棄,...
    蘭藍天閱讀 144評論 0 0
  • 喜歡很多時候自己一個人慢慢的走赘那,想很多東西刑桑。很喜歡回頭看看,老是有念舊的人在那孤獨惆悵募舟。 每天笑的很合群祠斧,就算多難...
    海帶潔閱讀 328評論 1 2