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

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;

  1 #include <iostream>
  2
  3 #include <string>
  4
  5 #include <vector>
  6
  7 #include <map>
  8
  9 using namespace std;
 10
 11
 12
 13 struct Gang
 14
 15 {
 16
 17    int num,sum;
 18
 19 };
 20
 21
 22
 23 string ss1[1001];
 24
 25 string ss2[1001];
 26
 27
 28
 29    map<string,vector<string>  > mm;
 30
 31        map<string,int> visit;
 32
 33        map<string,int> node;
 34
 35          map<string,Gang> result;
 36
 37
 38
 39 void DFS(string s,int &sum,string &max,int &num)
 40
 41 {
 42
 43    if(node[s]>node[max]) max=s;
 44
 45     num++;
 46
 47       sum=sum+node[s];
 48
 49    visit[s]=1;
 50
 51
 52
 53    for(int i=0;i<mm[s].size();i++)
 54
 55       {
 56
 57             if(visit[mm[s][i]]==0)
 58
 59                   DFS(mm[s][i],sum,max,num);
 60
 61       }
 62
 63
 64
 65 }
 66
 67
 68
 69
 70
 71 int main()
 72
 73 {
 74
 75
 76
 77      int  n,k,t;
 78
 79       string s1,s2;
 80
 81       while(cin>>n)
 82
 83       {
 84
 85          cin>>k;
 86
 87
 88
 89          mm.clear();
 90
 91          visit.clear();
 92
 93          node.clear();
 94
 95          result.clear();
 96
 97
 98
 99
100
101
102
103          int i;
104
105
106
107          for(i=0;i<n;i++)
108
109          {
110
111             cin>>s1>>s2>>t;
112
113               ss1[i]=s1;
114
115               ss2[i]=s2;
116
117               visit[s1]=0;
118
119               visit[s2]=0;
120
121               node[s1]+=t;
122
123               node[s2]+=t;
124
125                mm[s1].push_back(s2);
126
127                mm[s2].push_back(s1);
128
129          }
130
131
132
133
134
135          map<string,int>::iterator it;
136
137          int num;
138
139          int sum;
140
141          int count=0;
142
143          string max;
144
145
146
147          for(it=node.begin();it!=node.end();it++)
148
149          {
150
151                if(visit[it->first]==0)
152
153                {
154
155
156
157                      sum=0;
158
159                      num=0;
160
161                      max=it->first;
162
163                      DFS(it->first,sum,max,num);
164
165                      if(sum/2>k&&num>2)
166
167                      {
168
169                  count++;
170
171                        result[max].num=num;
172
173                        result[max].sum=sum;
174
175                      }
176
177
178
179                }
180
181          }
182
183
184
185          cout<<count<<endl;
186
187          map<string,Gang>::iterator it2;
188
189          for(it2=result.begin();it2!=result.end();it2++)
190
191          {
192
193                cout<<it2->first<<" "<<(it2->second).num<<endl;
194
195          }
196
197       }
198
199   return 0;
200
201 }

时间: 2024-08-07 04:28:35

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

确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 40   Accepted Submission(s) : 31 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的

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

Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 113  Solved: 24 SubmitStatusWeb Board Description 进去宝藏后, 小火山发现宝藏有N个房间,且这n个房间通过N-1道门联通. 每一个房间都有一个价值为Ai的宝藏, 但是每一个房间也都存在一个机关.如果小火山取走了这个房间的宝藏,那么这个房间通往其他房间的门就永远打不开了,也就是说后面的宝藏小火山是得不到了(进入这个房间的门是不会关闭的,小火山还是可以回去的

小火山的宝藏收益 多校训练2(小火山专场) poj(邻接表+DFS)

http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1907 Description 进去宝藏后, 小火山发现宝藏有N个房间,且这n个房间通过N-1道门联通. 每一个房间都有一个价值为Ai的宝藏, 但是每一个房间也都存在一个机关.如果小火山取走了这个房间的宝藏,那么这个房间通往其他房间的门就永远打不开了,也就是说后面的宝藏小火山是得不到了(进入这个房间的门是不会关闭的,小火山还是可以回去的):如果小火山不取这个宝藏,而是去打开通往另一房间的门,那么这个

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

//采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include<cstdio>#include<algorithm>using namespace std;const int maxn=1001;int g[maxn][maxn];int n,tmp;bool vis[maxn];void dfs(int v){ vis[v]=true; for(int

HDU 4707--Pet【DFS &amp;&amp; 邻接表】

Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1737    Accepted Submission(s): 835 Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He sear

zstu.4191: 无向图找环(dfs树 + 邻接表)

4191: 无向图找环 Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 117  Solved: 34 Description 给你一副无向图,每条边有边权,保证图联通,现在让你判断这个图是否有异或值大于零的环存在. Input 多组测试数据,每组先输入两个数n m,表示图的点跟边的数量. 然后是m行,每行三个数a b c.代表一条边的起点,终点,边权. 1 <= n<= 100000, 1 <= m <= 200000. 1 <

邻接表实现Dijkstra算法以及DFS与BFS算法

//============================================================================ // Name : ListDijkstra.cpp // Author : fffff // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //==========================

数据结构学习笔记05图 (邻接矩阵 邻接表--&gt;BFS DFS)

数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边<v, w> 表示从v指向w的边(单行线) 不考虑重边和自回路 无向图:边是无向边(v, w) 有向图:边是有向边<v, w> 连通:如果从V到W存在一条(无向)路径,则称V和W是连通的 连通图(Connected Graph):如果对于图的任一两个顶点v.w∈V,v和w都是连通的,则称

数据结构(11) -- 邻接表存储图的DFS和BFS

/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS /////////////////////////////////////////////////////////////// #include <iostream> #include <stdlib.h> #include <queue> using namespace std; //图的邻接表表示法