Head of a Gang (map+邻接表+DFS)

时间:2022-09-11 23:37:38

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

首先 要建一个 以string的 下标的 连接表,需要用map

map<string,vector<string>  > mm;

表内直接存放 string 地址就行了,权值另外保存

map<string,int> node;

再 DFS  求出极大连通图的个数,及各各极大连通图的节点数,权值之和,权值最大的节点地址

坑点:

1、“A "Gang" is a cluster of more than 2 persons ”  所以节点数要大于2

2、因为每次通话每个人都权值都加了,其实总通话时间=权值之和/2;

 #include <iostream>

 #include <string>

 #include <vector>

 #include <map>

 using namespace std;

 struct Gang

 {

    int num,sum;

 };

 string ss1[];

 string ss2[];

    map<string,vector<string>  > mm;

        map<string,int> visit;

        map<string,int> node;

          map<string,Gang> result;

 void DFS(string s,int &sum,string &max,int &num)

 {

    if(node[s]>node[max]) max=s;

     num++;

       sum=sum+node[s];

    visit[s]=;

    for(int i=;i<mm[s].size();i++)

       {

             if(visit[mm[s][i]]==)

                   DFS(mm[s][i],sum,max,num);

       }

 }

 int main()

 {

      int  n,k,t;

       string s1,s2;

       while(cin>>n)

       {

          cin>>k;

          mm.clear();

          visit.clear();

          node.clear();

          result.clear();

          int i;

          for(i=;i<n;i++)

          {

             cin>>s1>>s2>>t;

               ss1[i]=s1;

               ss2[i]=s2;

               visit[s1]=;

               visit[s2]=;

               node[s1]+=t;

               node[s2]+=t;

                mm[s1].push_back(s2);

                mm[s2].push_back(s1);

          }

          map<string,int>::iterator it;

          int num;

          int sum;

          int count=;

          string max;

          for(it=node.begin();it!=node.end();it++)

          {

                if(visit[it->first]==)

                {

                      sum=;

                      num=;

                      max=it->first;

                      DFS(it->first,sum,max,num);

                      if(sum/>k&&num>)

                      {

                  count++;

                        result[max].num=num;

                        result[max].sum=sum;

                      }

                }

          }

          cout<<count<<endl;

          map<string,Gang>::iterator it2;

          for(it2=result.begin();it2!=result.end();it2++)

          {

                cout<<it2->first<<" "<<(it2->second).num<<endl;

          }        

       }

   return ;

 }

Head of a Gang (map+邻接表+DFS)的更多相关文章

  1. 确定比赛名次&lpar;map&plus;邻接表 邻接表 拓扑结构 队列&plus;邻接表&rpar;

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  2. 分道扬镳 &sol;&sol;&sol; 邻接表 DFS 剪枝 oj1332

    题目大意: 编号为1…N 的N个城市之间以单向路连接,每一条道路有两个参数:路的长度和通过这条路需付的费用. Bob和Alice生活在城市1,但是当Bob发现了Alice玩扑克时欺骗他之后,他决定与她 ...

  3. zzuli 1907&colon; 小火山的宝藏收益 邻接表&plus;DFS

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 113  Solved: 24 SubmitStatusWeb Board Description    ...

  4. HDU2586 How far away ? 邻接表&plus;DFS

    题目大意:n个房子,m次询问.接下来给出n-1行数据,每行数据有u,v,w三个数,代表u到v的距离为w(双向),值得注意的是所修建的道路不会经过一座房子超过一次.m次询问,每次询问给出u,v求u,v之 ...

  5. 数据结构作业——图的存储及遍历(邻接矩阵、邻接表&plus;DFS递归、非递归&plus;BFS)

    邻接矩阵存图 /* * @Author: WZY * @School: HPU * @Date: 2018-11-02 18:35:27 * @Last Modified by: WZY * @Las ...

  6. NBOJv2——Problem 1037&colon; Wormhole(map邻接表&plus;优先队列SPFA)

    Problem 1037: Wormhole Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format: ...

  7. 魔法宝石(邻接表&plus;dfs更新)

    魔法宝石 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submissi ...

  8. PAT1013&period; Battle Over Cities&lpar;邻接矩阵、邻接表分别dfs&rpar;

    //采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...

  9. All Roads Lead to Rome(30)(MAP【int,string】,邻接表,DFS,模拟,SPFA)(PAT甲级)

    #include<bits/stdc++.h>using namespace std;map<string,int>city;map<int,string>rcit ...

随机推荐

  1. Vuforia点击屏幕自动对焦,过滤UGUI的按钮

    //点击屏幕自对对焦 #if UNITY_EDITOR )) #elif UNITY_ANDROID || UNITY_IPHONE && Input.GetTouch().phase ...

  2. MVC4&period;0 WebApi如何自定义返回数据类型

    1.客户端可以通过HTTP Accept消息头来通知服务器客户端想要什么样的MIME类型数据,例如:application/json则代表告诉服务器想要的是Json数据 2.服务器端撇开客户端的请求类 ...

  3. java web&period;xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

  4. Apache CXF实现Web Service(5)—— GZIP使用

    Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...

  5. C&num;根据汉字生成拼音首字母全称

    static void Main(string[] args) { string s = GetChineseSpell("周杰伦"); Console.WriteLine(s.T ...

  6. Android 纯代码加入点击效果

    项目中非常多的Button, 同一时候配置非常多button切图,Selector是不是非常烦, 使用以下这个类,就能够直接为Button添加点击效果. 不用多个图片,不用Selector. 使用方法 ...

  7. &lbrack;转载&rsqb; zookeeper 分布式锁服务

    转载自http://www.cnblogs.com/shanyou/archive/2012/09/22/2697818.html 分布式锁服务在大家的项目中或许用的不多,因为大家都把排他放在数据库那 ...

  8. 前端模块化:RequireJS&lpar;转&rpar;

    前言 前端模块化能解决什么问题? 模块的版本管理 提高可维护性 -- 通过模块化,可以让每个文件职责单一,非常有利于代码的维护 按需加载 -- 提高显示效率 更好的依赖处理 -- 传统的开发模式,如果 ...

  9. LR与SVM的异同

    原文:http://blog.sina.com.cn/s/blog_818f5fde0102vvpy.html 在大大小小的面试过程中,多次被问及这个问题:“请说一下逻辑回归(LR)和支持向量机(SV ...

  10. centos 7&period;2 安装gitlab汉化

    ####################你如果搜到我的这个博客,你的系统得是centos 7的   80端口没有占用.  QQ:1394466404   这个博客维护1年 #### 多地方第一个是百度 ...