C語言題目:有多少張桌子--并查集
【Problem Description】問題描述
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.
今天是伊格納修斯的生日。他邀請了很多朋友?,F(xiàn)在是吃晚飯的時間了。伊格納修斯想知道他至少需要多少張桌子。你必須注意,并不是所有的朋友都互相認識,而且所有的朋友都不想和陌生人呆在一起。
這個問題的一個重要規(guī)則是,如果我告訴你A認識B,B認識C,這意味著A,B,C相互認識,所以他們可以呆在一張桌子上。
例如:如果我告訴你A知道B,B知道C,D知道E,那么A,B,C可以呆在一張桌子上,而D,E必須呆在另一張桌子上。所以伊格納修斯至少需要兩張桌子。
【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.
輸入以整數(shù)T(1<=T<=25)開始,表示測試用例的數(shù)量。然后,T測試用例隨之出現(xiàn)。每個測試用例從兩個整數(shù)N和M開始(1<=N,M<=1000)。N表示朋友的數(shù)量,朋友標記為從1到N。然后M行跟隨。每行由兩個整數(shù)A和B(A=B) 意思是朋友A和朋友B彼此認識。兩個案例之間將有一個空白行。
【Output】輸出
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
對于每個測試用例,只需輸出Ignatius至少需要多少個表。不要打印任何空白。
【Sample Input】樣本輸入
2
5 3
1 2
2 3
4 5
5 1
2 5
【Sample Output】樣本輸出
2
4
【Code】代碼
#include <iostream> using namespace std; const int maxn=1005; int pre[maxn]; int find(int x) { if(x!=pre[x]) pre[x]=find(pre[x]); return pre[x]; } void merge(int x,int y) { if(find(x)!=find(y)) pre[find(x)]=find(y); } int main() { int T,N,M; int p,q; scanf("%d",&T); while(T--) { int ans=0; scanf("%d%d",&N,&M); for(int i=1; i<=N; i++) pre[i]=i; for(int i=1; i<=M; i++) { scanf("%d%d",&p,&q); merge(p,q); } for(int i=1; i<=N; i++) { if(find(i)==i) ans++; } printf("%d\n",ans); } return 0; }
/*
in:
2
5 3
1 2
2 3
4 5
5 1
2 5
out:
2
4
*/
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
詳解C++的JSON靜態(tài)鏈接庫JsonCpp的使用方法
這篇文章主要介紹了C++的JSON靜態(tài)鏈接庫JsonCpp的使用方法,演示了使用JsonCpp生成和解析JSON的方法,以及C++通過JSON方式的socket通信示例,需要的朋友可以參考下2016-03-03C語言匯編分析傳遞結(jié)構(gòu)體指針比傳遞結(jié)構(gòu)體變量高效的深層原因
本文章使用的工具是vs2010,本篇文章主要講解結(jié)構(gòu)體指針作為參數(shù)傳遞與結(jié)構(gòu)體變量作為參數(shù)傳遞的對比,不談值傳遞與址傳遞的概念2022-10-10關(guān)于函數(shù)傳參問題(指針傳參,值傳參,引用傳參)
這篇文章主要介紹了關(guān)于函數(shù)傳參問題(指針傳參,值傳參,引用傳參),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01VC實現(xiàn)動態(tài)菜單的創(chuàng)建方法
這篇文章主要介紹了VC實現(xiàn)動態(tài)菜單的創(chuàng)建方法,需要的朋友可以參考下2014-07-07C++中的std::initializer_list使用解讀
這篇文章主要介紹了C++中的std::initializer_list使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07C# CLR 中學習 C++關(guān)鍵詞extern使用詳解
這篇文章主要為大家介紹了C# CLR 中學習 C++ 之extern使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09