zoukankan      html  css  js  c++  java
  • 1034 Head of a Gang (30 分)

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

    这题算是有点算法思想,
    用到了并查集
    然后用map的话就是一个图的问题了。
    最后输出排个序就行
      1 #include <bits/stdc++.h>
      2 #define N 3000
      3 using namespace std;
      4 int n, m;
      5 struct Node{
      6     string a, b;
      7     int val;
      8 }node[N];
      9 struct Edge{
     10     string s;
     11     int k;
     12     friend bool operator < (const Edge &a, const Edge &b){
     13         return a.s < b.s;
     14     }
     15 };
     16 vector<Edge> vt;
     17 map<string,int> mp;
     18 map<int,string> mp1;
     19 vector<int> v[N];
     20 int vals[N],fa[N],vis[N],vis1[N];
     21 int id = 1, tmp_val = 0, tmp_id = -1;
     22 
     23 int find(int x){
     24     return fa[x] == x?x:fa[x] = find(fa[x]);
     25 }
     26 int value = 0, cnt = 0;
     27 void dfs(int x){
     28     vis[x] = 1;
     29     value += vals[x];
     30     cnt++;
     31     for(int i = 0; i < v[x].size(); i++){
     32         if(vis[v[x][i]])
     33             continue;
     34         dfs(v[x][i]);
     35     }
     36 }
     37 
     38 void dfs1(int x){
     39     vis1[x] = 1;
     40     if(tmp_val < vals[x]){
     41         tmp_val = vals[x];
     42         tmp_id = x;
     43     }
     44     for(int i = 0; i < v[x].size(); i++){
     45         if(vis1[v[x][i]])
     46             continue;
     47         dfs1(v[x][i]);
     48     }
     49 }
     50 
     51 int main(){
     52     cin >> n >> m;
     53     for(int i = 0; i < N; i++)
     54         fa[i] = i;
     55     for(int i = 0; i < n; i++){
     56         cin >> node[i].a >> node[i].b >> node[i].val;
     57         if(mp[node[i].a]){
     58             vals[mp[node[i].a]] += node[i].val;
     59         }else{
     60             mp1[id] = node[i].a;
     61             mp[node[i].a] = id++;
     62             vals[mp[node[i].a]] += node[i].val;
     63         }
     64         if(mp[node[i].b]){
     65             vals[mp[node[i].b]] += node[i].val;
     66         }else{
     67             mp1[id] = node[i].b;
     68             mp[node[i].b] = id++;
     69             vals[mp[node[i].b]] += node[i].val;
     70         }
     71         v[mp[node[i].b]].push_back(mp[node[i].a]);
     72         v[mp[node[i].a]].push_back(mp[node[i].b]);        
     73         int aa = find(mp[node[i].a]);
     74         int bb = find(mp[node[i].b]);
     75         if(aa != bb){
     76             fa[aa] = bb;
     77         }
     78     }
     79     int sum = 0;
     80     for(int i = 1; i < id; i++){
     81         if(fa[i] == i){
     82             value = 0;
     83             cnt = 0;
     84             dfs(i);
     85             if(cnt > 2 && value/2 > m){
     86                 sum ++;
     87                 tmp_val = 0;
     88                 dfs1(i);
     89                 Edge edge;
     90                 edge.s = mp1[tmp_id];
     91                 edge.k = cnt;
     92                 vt.push_back(edge);
     93             }
     94         }
     95     }
     96     cout<<sum<<endl;
     97     sort(vt.begin(),vt.end());
     98     for(int i = 0; i < vt.size(); i++){
     99         cout <<vt[i].s<<" "<<vt[i].k<<endl;
    100     }
    101     return 0;
    102 }






  • 相关阅读:
    destoon(DT)系统中公司主页模板风格添加方法
    outlook 收Gmail邮箱邮件
    使用新网全球邮改如何对域名进行解析
    无法访问.您可能没有权限使用网络资源.局域网无法访问共享,局域网无法访问打印机的一些方法
    Microsoft Word 对象ASP教程,ASP应用
    面向对象和面向过程的区别
    图文讲解 上网本 无光驱 系统蓝屏/系统无法开机 用U盘 winpe 启动U盘 重装系统的方法(通用PE工具箱/老毛桃/大白菜WinPE)
    2.0 版本的版权底部破解
    pureftpd FTP登岸呈现530验证失败 lnmp
    word域高级应用 if 域 域邮件合并的值的更改 日期的更改
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11081964.html
Copyright © 2011-2022 走看看