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 threthold 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.html
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:node
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.c++
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.算法
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
2 AAA 3 GGG 3
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
0
這題算是有點算法思想,
用到了並查集
而後用map的話就是一個圖的問題了。
最後輸出排個序就行
1 #include <bits/stdc++.h> 2 #define N 3000 3 using namespace std; 4 int n, m; 5 struct Node{ 6 string a, b; 7 int val; 8 }node[N]; 9 struct Edge{ 10 string s; 11 int k; 12 friend bool operator < (const Edge &a, const Edge &b){ 13 return a.s < b.s; 14 } 15 }; 16 vector<Edge> vt; 17 map<string,int> mp; 18 map<int,string> mp1; 19 vector<int> v[N]; 20 int vals[N],fa[N],vis[N],vis1[N]; 21 int id = 1, tmp_val = 0, tmp_id = -1; 22 23 int find(int x){ 24 return fa[x] == x?x:fa[x] = find(fa[x]); 25 } 26 int value = 0, cnt = 0; 27 void dfs(int x){ 28 vis[x] = 1; 29 value += vals[x]; 30 cnt++; 31 for(int i = 0; i < v[x].size(); i++){ 32 if(vis[v[x][i]]) 33 continue; 34 dfs(v[x][i]); 35 } 36 } 37 38 void dfs1(int x){ 39 vis1[x] = 1; 40 if(tmp_val < vals[x]){ 41 tmp_val = vals[x]; 42 tmp_id = x; 43 } 44 for(int i = 0; i < v[x].size(); i++){ 45 if(vis1[v[x][i]]) 46 continue; 47 dfs1(v[x][i]); 48 } 49 } 50 51 int main(){ 52 cin >> n >> m; 53 for(int i = 0; i < N; i++) 54 fa[i] = i; 55 for(int i = 0; i < n; i++){ 56 cin >> node[i].a >> node[i].b >> node[i].val; 57 if(mp[node[i].a]){ 58 vals[mp[node[i].a]] += node[i].val; 59 }else{ 60 mp1[id] = node[i].a; 61 mp[node[i].a] = id++; 62 vals[mp[node[i].a]] += node[i].val; 63 } 64 if(mp[node[i].b]){ 65 vals[mp[node[i].b]] += node[i].val; 66 }else{ 67 mp1[id] = node[i].b; 68 mp[node[i].b] = id++; 69 vals[mp[node[i].b]] += node[i].val; 70 } 71 v[mp[node[i].b]].push_back(mp[node[i].a]); 72 v[mp[node[i].a]].push_back(mp[node[i].b]); 73 int aa = find(mp[node[i].a]); 74 int bb = find(mp[node[i].b]); 75 if(aa != bb){ 76 fa[aa] = bb; 77 } 78 } 79 int sum = 0; 80 for(int i = 1; i < id; i++){ 81 if(fa[i] == i){ 82 value = 0; 83 cnt = 0; 84 dfs(i); 85 if(cnt > 2 && value/2 > m){ 86 sum ++; 87 tmp_val = 0; 88 dfs1(i); 89 Edge edge; 90 edge.s = mp1[tmp_id]; 91 edge.k = cnt; 92 vt.push_back(edge); 93 } 94 } 95 } 96 cout<<sum<<endl; 97 sort(vt.begin(),vt.end()); 98 for(int i = 0; i < vt.size(); i++){ 99 cout <<vt[i].s<<" "<<vt[i].k<<endl; 100 } 101 return 0; 102 }