HDU1213&HDU1232-求连通分支数
来源:互联网 发布:淘宝装修工具2.1 编辑:程序博客网 时间:2024/06/05 22:53
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30283 Accepted Submission(s): 14985
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省赛集训队选拔赛之热身赛
题目大意: 求连通分支数
解题思路: dfs
#include<iostream>#include<cstring>#include<cmath>#include<ctime>#include<cstdlib>#include<algorithm>#include<iomanip>#include<fstream>#include<vector>#include<map>using namespace std;const int MAXN=1e4+10;const int INF=0x3f3f3f3f;bool vis[MAXN];//bool M[MAXN][MAXN];int cc[MAXN];int current_cc;int vn,en;struct edge{ int to,cost; edge(int _to,int _cost):to(_to),cost(_cost){}};vector<edge> G[MAXN];void dfs(int u){ vis[u]=true; cc[u]=current_cc; //cout<<u<<" "; //previsit(u); int d=G[u].size(); for(int i=0;i<d;i++) { int v=G[u][i].to; if(!vis[v]) dfs(v); } //postvisit(u);}void find_cc(){ current_cc=0; memset(vis,false,sizeof(vis)) ; for(int i=1;i<=vn;i++) { if(!vis[i]) { current_cc++; dfs(i); } }}int main(){ ios::sync_with_stdio(false); int u,v; int T; cin>>T; //cout<<"请输入点数和边数:"<<endl; while(T--) { cin>>vn; //if(vn==0) break; cin>>en; memset(vis,false,sizeof(vis[0])*(vn+5)); //memset(M,false,sizeof(M)); for(int i=0;i<=vn;i++) G[i].clear(); srand((unsigned)time(NULL)); //cout<<"请输入边:"<<endl; for(int i=0;i<en;i++) { /*do { u=rand()%vn; v=rand()%vn; }while(u==v||M[u][v]||M[v][u]); cout<<u<<" "<<v<<endl;*/ cin>>u>>v; //M[u][v]=true;M[v][u]=true; G[u].push_back(edge(v,0)); G[v].push_back(edge(u,0)); } /*cout<<endl; cout<<" "; for(int j=0;j<vn;j++) { cout<<j<<" "; } cout<<endl; for(int i=0;i<vn;i++) { cout<<i<<": "; for(int j=0;j<vn;j++) { if(M[i][j]) cout<<M[i][j]<<" "; else cout<<" "; } cout<<endl; } cout<<endl; for(int i=0;i<vn;i++) { if(vis[i]) continue; else { dfs(i); } } cout<<endl; memset(vis,false,sizeof(vis[0])*(vn+5));*/ find_cc(); //cout<<"连通分量:"; cout<<current_cc<<endl; } return 0;}
畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53473 Accepted Submission(s): 28504
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年
题目大意: 求连通分支数-1
解题思路: dfs
#include<iostream>#include<cstring>#include<cmath>#include<ctime>#include<cstdlib>#include<algorithm>#include<iomanip>#include<fstream>#include<vector>#include<map>using namespace std;const int MAXN=1e4+10;const int INF=0x3f3f3f3f;bool vis[MAXN];//bool M[MAXN][MAXN];int cc[MAXN];int current_cc;int vn,en;struct edge{ int to,cost; edge(int _to,int _cost):to(_to),cost(_cost){}};vector<edge> G[MAXN];void dfs(int u){ vis[u]=true; cc[u]=current_cc; //cout<<u<<" "; //previsit(u); int d=G[u].size(); for(int i=0;i<d;i++) { int v=G[u][i].to; if(!vis[v]) dfs(v); } //postvisit(u);}void find_cc(){ current_cc=0; memset(vis,false,sizeof(vis)) ; for(int i=1;i<=vn;i++) { if(!vis[i]) { current_cc++; dfs(i); } }}int main(){ ios::sync_with_stdio(false); int u,v; //cout<<"请输入点数和边数:"<<endl; while(cin>>vn) { if(vn==0) break; cin>>en; memset(vis,false,sizeof(vis[0])*(vn+5)); //memset(M,false,sizeof(M)); for(int i=0;i<=vn;i++) G[i].clear(); srand((unsigned)time(NULL)); //cout<<"请输入边:"<<endl; for(int i=0;i<en;i++) { /*do { u=rand()%vn; v=rand()%vn; }while(u==v||M[u][v]||M[v][u]); cout<<u<<" "<<v<<endl;*/ cin>>u>>v; //M[u][v]=true;M[v][u]=true; G[u].push_back(edge(v,0)); G[v].push_back(edge(u,0)); } /*cout<<endl; cout<<" "; for(int j=0;j<vn;j++) { cout<<j<<" "; } cout<<endl; for(int i=0;i<vn;i++) { cout<<i<<": "; for(int j=0;j<vn;j++) { if(M[i][j]) cout<<M[i][j]<<" "; else cout<<" "; } cout<<endl; } cout<<endl; for(int i=0;i<vn;i++) { if(vis[i]) continue; else { dfs(i); } } cout<<endl; memset(vis,false,sizeof(vis[0])*(vn+5));*/ find_cc(); //cout<<"连通分量:"; cout<<current_cc-1<<endl; } return 0;}
- HDU1213&HDU1232-求连通分支数
- hdu1213+hdu1232
- hdu 1232 畅通工程 求连通分支数
- poj 1562 ,hdu 1241 Oil Deposits dfs,bfs 求连通分支数
- hdu1232&& hdu1213(简单并查集)
- 图 求强连通分支子图
- 求强连通分支 tarjan算法
- hdu 1213 求连通分支个数
- poj1523[割顶&连通分支数]
- POJ1523.SPF——无向图的割点,并求连通分支数(tarjan算法)
- 连通分支
- 如何编程求有向图的单向连通分支
- 图论,求有向图的强连通分支
- 图--求强连通分支 算法导论p338
- POJ2117_Electricity_求删除割点后最大连通分支的个数_tarjan
- 23.基于 邻接表 dfs 的 求连通分支
- 深度优先搜索,DFS,UVA572,连通分支数
- HDU1232 畅通工程 并查集求连通分量
- XX特惠系统 压测接口汇总
- Jexus 托管方式运行 Asp.net core
- 树形DP入门
- 内部类
- 2、SpringCloud 第一课 Eureka 注册中心
- HDU1213&HDU1232-求连通分支数
- oracle命名空间
- Android Studio项目引入外部库注意事项(PullToRefresh)
- IOS本地通知
- 不休闲的学习之Git的安装和使用
- Intent的简介以及属性的详解
- eclipse配置go语言环境
- eclipse操作技巧
- Flex 布局教程:语法篇