1034. Head of a Gang (30) PAT甲级刷题
2018-02-26 02:52
585 查看
题目描述
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.
输入描述:
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.
输出描述:
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 3GGG 3
思路:1.对名字进行编号作为图结点的序号,视为3位26进制数,由于不是顺序编号,所以还要一个bool数组记录各个结点是否出现过。
2.dfs算连通分量数,各个连通分量权值和、权值最大结点、结点数。用全局变量或引用传参。
3.题目所示图是又向的,但这里要当成无向图处理,所以最后算各个连通分量权值和时要除2。#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int n,k,tm_,tot=0,max_=0;
bool has[20000] = {0};
bool visited[20000] = {0};
struct Head{
int hd;
int mbrs;
};
vector<Head> ans;
struct member{
vector<int> next;
int time=0;
};
int nametoint(string s){
for(auto i=s.size();i<3;++i)
s = 'A'+s;
return (s[0]-'A')*26*26+(s[1]-'A')*26+s[2]-'A';
}
string inttoname(int n){
string s;
while(n!=0){
char c = n%26+'A';
s = c+s;
n/=26;
}
return s;
}
bool cmp(Head a,Head b){
return inttoname(a.hd)<inttoname(b.hd);
}
int dfs(member gang[],int v,int &head,int &n){
tot += gang[v].time;
++n;
if(max_<gang[v].time){
max_ = gang[v].time;
head = v;
}
visited[v] = 1;
for(int i=0;i<gang[v].next.size();++i){
int nxt = gang[v].next[i];
if(!visited[nxt])
dfs(gang,nxt,head,n);
}
return tot;
}
void dfstvs(member gang[]){
for(int i=0;i<20000;++i){
if(has[i]&&!visited[i]){
int head = 0,sum = 0;
int tt = dfs(gang,i,head,sum);
if(tt/2>k&&sum>2){
Head one;
one.hd = head;
one.mbrs = sum;
ans.push_back(one);
}
tot = 0;
max_ = 0;
}
}
cout<<ans.size()<<endl;
sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<ans.size();++i)
cout<<inttoname(ans[i].hd)<<' '<<ans[i].mbrs<<endl;
}
int main()
{
string a,b;
cin>>n>>k;
member gang[20000];
for(int i=0;i<n;++i){
cin>>a>>b>>tm_;
int a_int = nametoint(a);
int b_int = nametoint(b);
has[a_int] = 1;
has[b_int] = 1;
gang[a_int].next.push_back(b_int);
gang[a_int].time += tm_;
gang[b_int].next.push_back(a_int);
gang[b_int].time += tm_;
}
dfstvs(gang);
return 0;
}
相关文章推荐
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- 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甲级真题
- 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)
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)
- 【PAT】【Advanced Level】1034. Head of a Gang (30)
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- PAT甲题题解-1034. Head of a Gang (30)-并查集
- Python实现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)