日韩性视频-久久久蜜桃-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第九天-图论的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 伊久久| 插入综合网 | 精品一区国产 | 日本japanese极品少妇 | 国产深夜视频 | 国产人成在线观看 | 中文字幕一区二区三区在线视频 | 黄色欧美在线观看 | 色涩综合| 亚洲自拍中文字幕 | av免费看网站 | av大片在线看 | 激情五月深爱五月 | 国产午夜av| 番号动态图 | 日本偷拍一区 | 日本天堂网在线 | 波多野一区二区 | 超色视频 | 男人桶进美女尿囗 | 啪啪网站视频 | 成人毛片基地 | 三年大全国语中文版免费播放 | 婷婷综合五月 | 91捆绑91紧缚调教91 | 国产青青视频 | 波多野结衣一本 | 麻豆国产一区二区 | 中文字幕系列 | 亚洲免费成人av | 久久久久香蕉 | 欧美人体一区二区三区 | 国产中文字幕乱人伦在线观看 | 日本丰满熟妇bbxbbxhd | 蜜芽一区二区 | 午夜影视大全 | 美女野外找人搭讪啪啪 | 伊人亚洲精品 | 男人的天堂免费视频 | 日韩女同一区二区三区 | 免费av网址大全 | 久久99久久99精品免观看粉嫩 | 日韩精品免费看 | 激情自拍视频 | 打白嫩屁屁网站视频短裙 | 青青草社区视频 | 精品乱子伦 | 免费亚洲婷婷 | 可以看av的网站 | 成人亚洲综合 | 久久综合狠狠综合久久综合88 | 黄色91免费版| 中文免费在线观看 | 尤物av无码色av无码 | 亚洲熟妇无码久久精品 | 在线视频一区二区三区 | 神马影院一区二区 | av网站免费在线观看 | 在线不卡日本 | 天天色天| 美腿丝袜一区二区三区 | 欧美久久成人 | 91观看视频 | 米奇久久 | 无码国产色欲xxxxx视频 | 女生张开腿让男生插 | 18+视频在线观看 | 一卡二卡精品 | 成人在线a | 日韩精品一区二 | 亚洲图片自拍偷拍 | 18成人免费观看网站 | 茄子av | 久久久久久艹 | 人人射人人插 | 欧美毛片免费看 | 国产三级av在线播放 | 免费黄色在线网站 | 环太平洋3:泰坦崛起 | 亚洲精品在线看 | 青青草国产成人av片免费 | 国产成人精品一区二三区四区五区 | 国产精品色在线网站 | av动漫免费观看 | 福利视频在线导航 | 国产视频二| 深夜福利一区二区 | 麻豆黄色片 | 亚洲av综合色区无码二区爱av | 午夜一二区 | 青青国产在线观看 | 视频二区中文字幕 | 夜夜操网址 | 国产免费黄色大片 | 欧美日韩18| 9色视频 | 久久久精品在线 | 麻豆传媒映画官网 | 啪啪网页|