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;
     }
     ;
 }
 
 

1034 Head of a Gang (30 分)的更多相关文章

  1. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  2. 1034 Head of a Gang (30分)(dfs 利用map)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  3. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

  4. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  5. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  6. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  7. 1034. Head of a Gang (30) -string离散化 -map应用 -并查集

    题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  8. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  9. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

  10. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

随机推荐

  1. IOS多线程编程:概述

    什么是多线程 多线程是一个比较轻量级的方法来实现单个应用程序内多个代码执行路径.从技术角度来看,一个线程就是一个需要管理执行代码的内核级和应用级数据结构组合.内核级结构协助调度线程事件,并抢占式调度一 ...

  2. 了解SAGA

    萨迦(尤指古代挪威或冰岛讲述冒险经历和英雄业绩的长篇故事)

  3. Linux 内核链表实现和使用(一阴一阳,太极生两仪~)

    0. 概述 学习使用一下 linux 内核链表,在实际开发中我们可以高效的使用该链表帮我们做点事, 链表是Linux 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...

  4. Intellij Idea2016.3 svn服务器拉取代码

    1.修改idea的默认配置,取消SVN设置里的两个勾 2.拉取代码 3.输入SVN仓库的地址,然后checkout 即可

  5. IOS 作业项目(1) 关灯游戏 (百行代码搞定)

    1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor

  6. Texas Instruments matrix-gui-2.0 hacking -- helper_functions.php

    <?php # PHP_SELF: 但前正在执行脚本的文件名,与document root相关 # QUERY_STRING: 查询(query)的字符串 $cachefile = " ...

  7. (MyEclipse) MyEclipse完美破解方法(图)

    http://photo.blog.sina.com.cn/list/blogpic.php?pid=53358777td408badc4071&bid=533587770101dd03&am ...

  8. Lua基本语法-lua与C#的交互(相当简单详细的例子)

    lua脚本 与 C#的交互 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Lua And C# -- ...

  9. JAVA爬取百度贴吧图片

    package com.wang.xiaowei.utils; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.code ...

  10. 《DSP using MATLAB》Problem 4.18

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...