【PAT】【Advanced Level】1034. Head of a Gang (30)
2017-07-23 15:28
399 查看
1034. Head of a Gang (30)
时间限制100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:
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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1034
思路:
DFS
CODE:
#include<iostream> #include<cstring> #include<string> #include<algorithm> #include<map> #define N 2001 using namespace std; int n,mc; typedef struct S { string name; int time; }; typedef struct S1 { string name; int pn; }; S node ; S1 dg ; bool able ; bool fl ; int num=1; int mt=0; string boss; int rs=0; int maxt=-1; map<string,int> ma; void dfs(int now) { //cout<<node[now].name<<endl; if (node[now].time>maxt) { maxt=node[now].time; boss=node[now].name; } mt+=node[now].time; rs++; for (int i=1;i<num;i++) { if (fl[i]==1) continue; if (able[now][i]==0) continue; fl[i]=1; dfs(i); } } bool cmp(S1 a, S1 b) { return a.name<b.name; } int main() { memset(able,0,sizeof(able)); memset(fl,0,sizeof(fl)); cin>>n>>mc; for (int i=1;i<=n;i++) { string a,b; int c; cin>>a>>b>>c; if (ma[a]==0) { ma[a]=num; node[num].name=a; node[num].time=c; num++; } else node[ma[a]].time+=c; if (ma[b]==0) { ma[b]=num; node[num].name=b; node[num].time=c; num++; } else node[ma[b]].time+=c; able[ma[a]][ma[b]]=able[ma[b]][ma[a]]=1; //cout<<ma[a]<<" "<<ma[b]<<endl; } int sum=0; for (int i=1;i<num;i++) { if (fl[i]==0) { rs=0; mt=0; maxt=-1; fl[i]=1; dfs(i); mt/=2; //cout<<rs<<" "<<mt<<endl; if (rs>2&&mt>mc) { dg[sum].name=boss; dg[sum].pn=rs; sum++; } //cout<<endl; } } sort(dg,dg+sum,cmp); cout<<sum<<endl; for (int i=0;i<sum;i++) cout<<dg[i].name<<" "<<dg[i].pn<<endl; }
相关文章推荐
- PAT (Advanced Level) 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1034. Head of a Gang (30) 黑帮头目,并查集
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- PAT (Advanced Level) Practise 1034 Head of a Gang (30)
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30) 解题报告
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30)
- PAT 1034. Head of a Gang (30) DFS,删边
- PAT Advanced Level 1018. Public Bike Management (30)(Java and C++)
- 1018. Public Bike Management (30) @ PAT (Advanced Level) Practise
- PAT-PAT (Advanced Level) Practise 1018. Public Bike Management (30) 【四星级】
- PAT (Advanced Level) Practise 1135 Is It A Red-Black Tree (30)
- PAT (Advanced Level) Practise 1107 Social Clusters (30)
- 浙大2012上机 PAT1034. Head of a Gang (30)
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
- 1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)
- 1049. Counting Ones (30)【计算1 的个数】——PAT (Advanced Level) Practise