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. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

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

  2. 分道扬镳 /// 邻接表 DFS 剪枝 oj1332

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

  3. zzuli 1907: 小火山的宝藏收益 邻接表+DFS

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

  4. HDU2586 How far away ? 邻接表+DFS

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

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

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

  6. NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)

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

  7. 魔法宝石(邻接表+dfs更新)

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

  8. PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)

    //采用不同的图存储结构结构邻接矩阵.邻接表分别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. ConcurrentHashMap 源码解析 -- Java 容器

    ConcurrentHashMap的整个结构是一个Segment数组,每个数组由单独的一个锁组成,Segment继承了ReentrantLock. 然后每个Segment中的结构又是类似于HashTa ...

  2. cocos2d-x之jni使用(对接Android各种sdk)

    游戏弄完了,要发布到各个平台,ios.Android是肯定少不了的,那么本文就来讲讲Android平台对接代理商付费sdk.各渠道.五大运营商.广告.分享.数据统计等等少不了的jni调用,接sdk真是 ...

  3. PHP读书笔记(6)- 数组

    数组定义 数组就是一个键值对组成的语言结构,键类似于酒店的房间号,值类似于酒店房间里存储的东西.PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型. 定义数组 ...

  4. c# 友元程序集

    在团队开发中,如果一个程序集中要调用另外一个程序集,但是要被调用的那个程序集又不想用public来公开自己的类, 那么怎么办,就是用最后一种internal来用来做类的可见性了. 下面来看一个简单例子 ...

  5. Toad for Oracle 12.1下载地址

    32 位版: http://us-downloads.quest.com/Repository/support.quest.com/Toad for Oracle/12.1/Software/Toad ...

  6. 命令行创建Windows窗体应用程序

    csc:(C Sharp Compiler) 类似于 javac (java Compiler) 命令行的编译工具 位置:C:\Windows\Microsoft.NET\Framework\v4.0 ...

  7. Spring(3.2.3) - Beans(4): p-namespace & c-namespace

    p 命名空间 p 命名空间允许你使用 bean 元素的属性而不是 <property/>子元素来描述 Bean 实例的属性值.从 Spring2.0 开始,Spring 支持基于 XML ...

  8. Git - Eclipse 提交工程至 GitHub

    1. 在 GitHub 新建一个工程 hello-world,repository 地址是 https://github.com/username/hello-world.git 2. 在 Eclip ...

  9. Xcode6:The file couldn’t be opened because you don’t have permission to view it

    最近为了兼容iOS8升级到Xcode6.0编译之前的工程,结果App无法在真机上运行.报错如下: The file “xxxx.app” couldn’t be opened because you ...

  10. chattr实现文件不可删除

    用自己的话解释清楚这件事儿~ 目前问题: Android手机,在/system/app 目录下的apk,使用chmod 修改权限失败,rm命令也删除不掉. 现象: rm failed for wand ...