日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

畅通工程,How Many Tables ACM第九天-图论

發布時間:2024/9/5 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 畅通工程,How Many Tables ACM第九天-图论 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面兩題的方法是一樣的都是并查集的應用; 并查集的重點就是找到祖先節點的過程。 重點在這里:int find(int x){ return p[x] == x ? x : p[x] = find( p[x]); } 不斷地遞歸,遞歸到找到自己是自己的祖先為止。并且把它取接到那個祖先節點上;這樣可以節省空間和時間。

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15414????Accepted Submission(s): 7560


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.

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
Author Ignatius.L
Source 杭電ACM省賽集訓隊選拔賽之熱身賽
Recommend Eddy


#include <cstdio> #define maxn 1005 using namespace std; int p[maxn]; int a[maxn]; int b[maxn]; int find(int x){return p[x] == x? x : p[x] = find(p[x]); } int main(){int t;scanf("%d",&t);while(t--){int n,m;scanf("%d %d",&n,&m);for(int i = 1;i <= m;i++){scanf("%d %d",&a[i],&b[i]);}int ans = 0;for(int i = 1;i <= n;i++){p[i] = i;}for(int i = 0;i <= m;i++){int x = find(a[i]);int y = find(b[i]);if(x != y){ans++;p[x] = y;}}int answer = n - ans;printf("%d\n",answer);}return 0; }
【比賽提醒】BestCoder 你報名了嗎?(點擊報名)?
【科普】什么是BestCoder?如何參加?

暢通工程

Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32971????Accepted Submission(s): 17391


Problem Description 某省調查城鎮交通狀況,得到現有城鎮道路統計表,表中列出了每條道路直接連通的城鎮。省政府“暢通工程”的目標是使全省任何兩個城鎮間都可以實現交通(但不一定有直接的道路相連,只要互相間接通過道路可達即可)。問最少還需要建設多少條道路??

Input 測試輸入包含若干測試用例。每個測試用例的第1行給出兩個正整數,分別是城鎮數目N ( < 1000 )和道路數目M;隨后的M行對應M條道路,每行給出一對正整數,分別是該條道路直接連通的兩個城鎮的編號。為簡單起見,城鎮從1到N編號。?
注意:兩個城市之間可以有多條道路相通,也就是說
3 3
1 2
1 2
2 1
這種輸入也是合法的
當N為0時,輸入結束,該用例不被處理。?

Output 對每個測試用例,在1行里輸出最少還需要建設的道路數目。?

Sample Input 4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
Sample Output 1 0 2 998 HintHint Huge input, scanf is recommended.
Source 浙大計算機研究生復試上機考試-2005年
Recommend JGShining
#include <cstdio> #include <cstring>using namespace std; #define maxn 1005 int p[maxn]; int a[maxn]; int b[maxn]; int find(int x){return p[x] == x? x : p[x] = find(p[x]); } int main(){int n,m;while(scanf("%d %d",&n,&m) && n){memset(p,0,sizeof(p));for(int i = 1;i <= m;i++){scanf("%d %d",&a[i],&b[i]);}int ans = 0;for(int i = 1;i <= n;i++){p[i] = i;}for(int i = 0;i <= m;i++){int x = find(a[i]);int y = find(b[i]);if(x != y){ans++;p[x] = y;}}int answer = n - 1 - ans;printf("%d\n",answer);}return 0; }

轉載于:https://www.cnblogs.com/lccurious/p/5079882.html

總結

以上是生活随笔為你收集整理的畅通工程,How Many Tables ACM第九天-图论的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。