PAT - 甲级 - 1034. Head of a Gang (30)(DFS求连通块)
2017-11-06 13:06
537 查看
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between
the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you
are supposed to find the gangs and the heads.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:
Name1 Name2 Time
where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.
Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to
the alphabetical order of the names of the heads.
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
给定条件:
1.N条通话记录
2.称之为“团伙”的通话总和最小值K
题目要求:
1.有几个团伙
2.每个团伙的“头目”是谁
3.每个团伙的人数是多少
解答:
1.dfs求图的连通分量,连通分量数即团伙数目
2.每个连通分量中通话时长最多的就是头目
3.每个连通分量的节点数即是团伙人数
限制条件:
1.连通分量各节点的通话总时长超过k,并且节点数超过2,才算是团伙
需要注意:
1.给定的N是“边”的数量而不是“点”的数量
2.在计算通话总和的时候要考虑到“环”的情况
the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you
are supposed to find the gangs and the heads.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:
Name1 Name2 Time
where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.
Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to
the alphabetical order of the names of the heads.
Sample Input 1:
8 59 AAA BBB 10 BBB AAA 20 AAA CCC 40 DDD EEE 5 EEE DDD 70 FFF GGG 30 GGG HHH 20 HHH FFF 10
Sample Output 1:
2 AAA 3 GGG 3
Sample Input 2:
8 70 AAA BBB 10 BBB AAA 20 AAA CCC 40 DDD EEE 5 EEE DDD 70 FFF GGG 30 GGG HHH 20 HHH FFF 10
Sample Output 2:
0
给定条件:
1.N条通话记录
2.称之为“团伙”的通话总和最小值K
题目要求:
1.有几个团伙
2.每个团伙的“头目”是谁
3.每个团伙的人数是多少
解答:
1.dfs求图的连通分量,连通分量数即团伙数目
2.每个连通分量中通话时长最多的就是头目
3.每个连通分量的节点数即是团伙人数
限制条件:
1.连通分量各节点的通话总时长超过k,并且节点数超过2,才算是团伙
需要注意:
1.给定的N是“边”的数量而不是“点”的数量
2.在计算通话总和的时候要考虑到“环”的情况
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; int vis[2010], w[2010]; int e[2010][2010]; map<string, int> strToint; map<int, string> intTostr; map<string, int> ans; int id = 1; int getInt(string str){ if(strToint[str] == 0){ strToint[str] = id; return id++; }else{ return strToint[str]; } } void dfs(int &head, int i, int &totnum, int &totwei){ vis[i] = 1; totnum += 1; if(w[i] > w[head]){ head = i; } for(int j = 1; j < id; j++){ if(e[i][j]){ totwei += e[i][j]; e[i][j] = e[j][i] = 0; if(!vis[j]){ dfs(head, j, totnum, totwei); } } } } int N, K, t; string s1, s2; int main(){ freopen("input.txt", "r", stdin); while(scanf("%d%d", &N,&K) != EOF){ // init fill(vis, vis+2010, 0); fill(e[0], e[0]+2010, 0); fill(w, w+2010, 0); // input for(int i = 0; i < N; i++){ cin>>s1>>s2>>t; int a = getInt(s1); int b = getInt(s2); intTostr[a] = s1; intTostr[b] = s2; w[a] += t; w[b] += t; e[a][b] += t; e[b][a] += t; } // for(int i = 1; i < id; i++){ if(!vis[i]){ int totnum = 0, totwei = 0, head = i; dfs(head, i, totnum, totwei); if(totnum > 2 && totwei > K){ ans[intTostr[head]] = totnum; } } } cout<<ans.size()<<endl; for(map<string, int>::iterator it = ans.begin(); it != ans.end(); it++){ cout<<it->first<<" "<<it->second<<endl; } } return 0; }
相关文章推荐
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- 【PAT甲级】1034. Head of a Gang (30)
- PAT 甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT甲级刷题
- pat 甲级 1034. Head of a Gang (30)
- PAT甲级1034. Head of a Gang (30)
- pat甲级1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) DFS,删边
- PAT甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)-PAT甲级真题
- 浙大2012上机 PAT1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) 并查集
- PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- PAT_A 1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)