1034 Head of a Gang (30 分)

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 Aand 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.

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

分析: 变量有点多。。得有耐心写

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-21-21.05.03
 * Description : A1034
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 },weight[maxn]={};
 bool vis[maxn]={false};
 map<string,int> stringToInt;
 map<int,string> intToString;
 map<string,int> Gang; //Gang的人数
 ,numPerson=;
 int n,th;
 int change(string str){
     if(stringToInt.find(str)!=stringToInt.end()){
         return stringToInt[str];
     }
     else{
         stringToInt[str]=numPerson;
         intToString[numPerson]=str;
         return numPerson++;
     }
 }

 void DFS(int index,int& head,int& numMember,int& totalValue){
     numMember++;
     vis[index]=true;
     if(weight[index]>weight[head]){
         head=index;
     }
     ;i<numPerson;i++){
         ){
             totalValue+=G[index][i];
             G[index][i]=G[i][index]=;  //遍历过后把该边删除
             if(vis[i]==false) DFS(i,head,numMember,totalValue);
         }
     }
 }

 void DFSTravel(){
     ;i<numPerson;i++){
         if(vis[i]==false){
             ,totalValue=;
             DFS(i, head, numMember, totalValue);
             && totalValue > th){
                 Gang[intToString[head]]=numMember;
             }
         }
     }
 }

 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int w;
     string str1,str2;
     scanf("%d%d",&n,&th);
     ;i<n;i++){
         cin>>str1>>str2>>w;
         int id1=change(str1);
         int id2=change(str2);
         weight[id1]+=w;
         weight[id2]+=w;
         G[id1][id2]+=w;
         G[id2][id1]+=w;
     }
     DFSTravel();
     cout<<Gang.size()<<endl;
     for(auto it =Gang.begin();it!=Gang.end();it++){
         cout<<it->first<<" "<<it->second<<endl;
     }
     ;
 }
 
 

最新文章

  1. JavaScript写一个小乌龟推箱子游戏
  2. Android项目的目录结构
  3. Stakeholder Risk Management
  4. div中字垂直居中对齐
  5. iOS中的加密方式 与 文件解压缩
  6. 成功在BAE上部署ghost 5.0
  7. 归并排序算法(C#实现)
  8. Java基础知识强化之网络编程笔记09:TCP之客户端键盘录入服务器写到文本文件中
  9. PHP单例模式编写
  10. VIM批量文件查找和替换
  11. springmvc 添加@ResponseBody
  12. Python之匿名函数
  13. jacoco+maven 初次使用覆盖率工具
  14. git安装和GitHub使用
  15. Spring @Bean注解 (基于java的容器注解)
  16. mysql的基础知识
  17. DTW的原理及matlab实现(转载+整理)
  18. zstu 4247-萌新的旅行
  19. 今日总结(linux和plsql)
  20. Winform 学生管理系统增删改查

热门文章

  1. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree
  2. 201621123010《Java程序设计》第12周学习总结
  3. L238
  4. python使用opencv驱动摄像头
  5. SWIFT推送之本地推送(UILocalNotification)
  6. 测试JS方法运行时间
  7. 第32课 初探C++标准库
  8. C高级第一次PTA作业(2)
  9. 【error】scripts/basic/fixdep: Syntax error: &quot;(&quot; unexpected
  10. 51Nod 1002:数塔取数问题(DP)