zoukankan      html  css  js  c++  java
  • PAT1034;Head of a Gang

    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

    思路

    图的连通性和dfs问题。
    1.map + vector构造一个图,另外用一个map统计每个人的weight,一个map储存满足条件的黑帮,一个map负责标记一个节点是否访问过。
    2.dfs时根据每个人的weight不断更新黑帮老大head,并统计相关联的节点数countNode,sum为该黑帮内所有人weight之和。
    注:sum/2其实就是黑帮的总通话时长。
    3.将满足条件(sum/2)> K 和黑帮人数在2人以上(countNode > 2)的黑帮数据储存到cluster中。
    4.cluster为空输出0,否则遍历输出。

    代码
    #include<map>
    #include<vector>
    #include<iostream>
    using namespace std;
    
    map<string,int> weight;
    map<string,int> cluster;
    map<string,vector<string>> graph;
    map<string,bool> visits;
    
    
    void dfs(const string& a,string& head,int& countNode,int& sum)
    {
        int curmax = weight[head];
        if(weight[a] > curmax)
            head = a;
        countNode++;
        sum += weight[a];
        visits[a] = true;
        for(int i = 0; i < graph[a].size(); i++)
        {
            if(!visits[graph[a][i]])
                dfs(graph[a][i],head,countNode,sum);
        }
    }
    
    int main()
    {
        int N,K;
        while(cin >> N >> K)
        {
            for(int i = 0; i < N; i++)
            {
                string a,b;
                int w;
                cin >> a >> b >> w;
                weight[a] += w;
                weight[b] += w;
                graph[b].push_back(a);
                graph[a].push_back(b);
                visits[a] = visits[b] = false;
            }
            int cnt = 0;
            for(auto it = graph.begin(); it != graph.end(); it++)
            {
                if(visits[it->first])
                    continue;
                cnt++;
                string head = it->first;
                int countNode = 0,sum = 0;
                dfs(it->first,head,countNode,sum);
                if((sum/2) > K && countNode > 2)
                    cluster[head] = countNode;
                else
                    cnt--;
            }
            if(cluster.empty())
            {
                cout << 0 << endl;
                continue;
            }
            cout << cnt << endl;
            for(auto it = cluster.begin(); it != cluster.end(); it++)
            {
                cout << it->first << " " << it->second << endl;
            }
        }
    }
    

      

  • 相关阅读:
    angularjs学习笔记—事件指令
    JS编写点击页面弹出被点击的标签名
    对数据进行排序
    springBoot集成seata
    maven打包时根据不同的环境生成不同的jar包名称
    单列模式-双重锁校验解析
    hashmap原理简述
    Linux搭建disconf(二)
    Linux搭建dubbo-admin 分布式服务监控中心
    Linux安装zookeeper
  • 原文地址:https://www.cnblogs.com/0kk470/p/8125183.html
Copyright © 2011-2022 走看看