zoukankan      html  css  js  c++  java
  • PAT 1034. Head of a Gang

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

    分析
    我用的是邻接表法建图map<string,vector<string>>,然后用dfs来遍历连通分量;注意最后求每个连通分量weight总和时要除以2;

     1 #include<iostream>
     2 #include<map>
     3 #include<vector>
     4 #include<algorithm>
     5 using namespace std;
     6 int num=0,sum;
     7 string head;
     8 map<string,int> result;
     9 map<string,vector<string>> G;
    10 map<string,int> weight;
    11 map<string,int> visited;
    12 void dfs(string s){
    13     visited[s]=0;
    14     num++;
    15     sum+=weight[s];
    16     if(weight[s]>weight[head]) head=s;
    17     for(int i=0;i<G[s].size();i++)
    18         if(visited[G[s][i]]==1)
    19            dfs(G[s][i]);
    20 }
    21 int main(){
    22     int n,k,weigh,tag=0,temp=0;
    23     string s1,s2;
    24     cin>>n>>k;
    25     for(int i=0;i<n;i++){
    26         cin>>s1>>s2>>weigh;
    27         weight[s1]+=weigh; weight[s2]+=weigh;
    28         G[s1].push_back(s2);
    29         G[s2].push_back(s1);
    30         visited[s1]=visited[s2]=1;
    31     }
    32     auto it=visited.begin();
    33     for(;it!=visited.end();it++){
    34         if(it->second==1){
    35             num=sum=0; 
    36             head=it->first;
    37             dfs(it->first);
    38         }
    39         if(num>2&&sum/2>k)
    40             result[head]=num;
    41     }
    42     cout<<result.size()<<endl;
    43     for(auto itt=result.begin();itt!=result.end();itt++)   
    44         cout<<itt->first<<" "<<itt->second<<endl;
    45     return 0;
    46 } 
  • 相关阅读:
    从关系型数据库到非关系型数据库
    2016某知名互联网公司PHP面试题及答案
    企业网站核心关键词如何去选择
    写Seo网站标题应该注意什么
    什么样的外链才是优质外链
    什么是网站物理链接结构
    需要分析竞争对手的网站哪些SEO数据
    做外链的时候应该需要注意什么
    描述标签对关键词排名有影响吗
    网站外链对排名的影响有哪些
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8300900.html
Copyright © 2011-2022 走看看