2019-01-29

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5

5 1
2 5
Sample Output
2
4
問題鏈接:https://vjudge.net/contest/279641#problem/G
問題簡述:把相互認(rèn)識的人分成一堆券敌,求有多少堆
問題分析:找到每個人最初認(rèn)識的人
程序說明:并查集
AC通過的C++程序如下:

include<iostream>

using namespace std;
int find(int x[],int i)
{
if (x[i] != i)
return find(x,x[i]);
return x[i];
}
void set(int x[], int y,int z)
{
int a = find(x,y), b = find(x,z);
if (a != b)
x[b] = a;
else return;
}
int main()
{
int t, n, m, f[1005], a, b, s;
cin >> t;
while (t--)
{
s = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
f[i] = i;
}
for (int i = 1; i <= m; i++)
{
cin >> a >> b;
set(f,a, b);
}
for (int i = 1; i <= n; i++)
{
find(f,i);
if (find(f,i) == i)
s++;
}
cout << s<< endl;
}
return 0;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市柳洋,隨后出現(xiàn)的幾起案子待诅,更是在濱河造成了極大的恐慌,老刑警劉巖熊镣,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件卑雁,死亡現(xiàn)場離奇詭異,居然都是意外死亡绪囱,警方通過查閱死者的電腦和手機(jī)测蹲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鬼吵,“玉大人扣甲,你說我怎么就攤上這事〕菀危” “怎么了琉挖?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長涣脚。 經(jīng)常有香客問我示辈,道長,這世上最難降的妖魔是什么遣蚀? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任矾麻,我火速辦了婚禮,結(jié)果婚禮上妙同,老公的妹妹穿的比我還像新娘射富。我一直安慰自己,他們只是感情好粥帚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布胰耗。 她就那樣靜靜地躺著,像睡著了一般芒涡。 火紅的嫁衣襯著肌膚如雪柴灯。 梳的紋絲不亂的頭發(fā)上卖漫,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機(jī)與錄音赠群,去河邊找鬼羊始。 笑死,一個胖子當(dāng)著我的面吹牛查描,可吹牛的內(nèi)容都是我干的突委。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼冬三,長吁一口氣:“原來是場噩夢啊……” “哼匀油!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起勾笆,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敌蚜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后窝爪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體弛车,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年蒲每,在試婚紗的時候發(fā)現(xiàn)自己被綠了纷跛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡啃勉,死狀恐怖忽舟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情淮阐,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布刁品,位于F島的核電站泣特,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏挑随。R本人自食惡果不足惜状您,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望兜挨。 院中可真熱鬧膏孟,春花似錦、人聲如沸拌汇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽噪舀。三九已至魁淳,卻和暖如春飘诗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背界逛。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工昆稿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人息拜。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓溉潭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親少欺。 傳聞我的和親對象是個殘疾皇子喳瓣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,511評論 0 23
  • 自從認(rèn)識了安化黑茶,我就天天喝茶狈茉,忽然有一天我的身體慢慢的就有所改變夫椭,不像以前那樣老是感冒了。
    袁雪利閱讀 183評論 0 0
  • 每天看二寶氯庆,就像在打仗蹭秋,身心疲憊,我也想陪大寶寫作業(yè)堤撵,也想陪她讀書仁讨,陪她一起寫日記,沒看過娃的人不知道实昨,有心無力洞豁,...
    李凝萱媽媽閱讀 87評論 0 0
  • 三年級第四單元習(xí)作:
    睿子格格閱讀 316評論 0 0