- 编程题
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
题目大意:
给出所有电话记录的对象和通话时长,求团伙的个数,并按照团伙头目名字字母序输出每个团伙的头目和成员数量。
解题思路1:
用并查集。如果属于同个团伙,两个对象合并。所有通话记录都处理完成后,先按照团伙标号排序,再按照通话时长排序,得到通话时长最长的人为该团伙头目。最后输出。
源代码1:
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
struct member
{
string s;
int t;
int flag = 0;
};
int n, k;
int i, j;
int sum;
map<string, int>mymap;
vector<member>m;
member mm;
bool cmp1(member m1, member m2)
{
if (m1.flag != m2.flag)return m1.flag < m2.flag;
else return m1.t < m2.t;
}
bool cmp2(member m1, member m2)
{
// if (m1.flag != m2.flag)return m1.flag > m2.flag;
// else return m1.s < m2.s;
return m1.s < m2.s;
}
void merge(int n1, int n2);
int getf(int n);
int main()
{
int cnt = 1;
string s1, s2;
int t1;
cin >> n >> k;
getchar();
for (i = 1; i <= n; i++)
{
cin >> s1 >> s2 >> t1;
if (mymap[s1] == 0)
{
mymap[s1] = cnt;
mm.s = s1;
mm.t = t1;
mm.flag = cnt++;
m.push_back(mm);
}
else
m[mymap[s1] - 1].t += t1;
if (mymap[s2] == 0)
{
mymap[s2] = cnt;
mm.s = s2;
mm.t = t1;
mm.flag = cnt++;
m.push_back(mm);
}
else
m[mymap[s2] - 1].t += t1;
merge(m[mymap[s1] - 1].flag, m[mymap[s2] - 1].flag);
// cout << s1 << " " << s2 << " " << t1 << endl;
}
for (i = 1; i < cnt; i++)
getf(i);
sum = 0;
sort(m.begin(), m.end(), cmp1);
int count = 1;
sum = m[0].t;
vector<member> ans;
for (i = 1; i < m.size(); i++)
{
if (m[i].flag != m[i - 1].flag)
{
if (count > 2 && sum > 2*k)
{
mm.s = m[i - 1].s;
mm.t = sum;
mm.flag = count;
ans.push_back(mm);
}
count = 1;
sum = m[i].t;
}
else
{
count++;
sum += m[i].t;
if (i == m.size() - 1 && count > 2 && sum > 2*k)
{
mm.s = m[i].s;
mm.t = sum;
mm.flag = count;
ans.push_back(mm);
}
}
}
sort(ans.begin(), ans.end(), cmp2);
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++)
cout << ans[i].s << " " << ans[i].flag << endl;
// printf("%s %d\n", ans[i].s, ans[i].flag);
system("pause");
return 0;
}
void merge(int n1, int n2)
{
int x, y;
x = getf(n1);
y = getf(n2);
if (x != y)
{
m[y - 1].flag = x;
}
return;
}
int getf(int n)
{
if (m[n - 1].flag == n)
return n;
else
{
m[n - 1].flag = getf(m[n - 1].flag);
return m[n - 1].flag;
}
}
解题思路2:
用DFS。将与该对象有关的其他对象使用set存储到对象的结构中。用深搜记录团伙数量和每个团伙的头目和团伙成员数量。最后输出。
注意事项:
1、做深搜的时候,竟然忘记在DFS函数中递归调用DFS函数,导致有一个测试点没过(也没想到只有一个测试点没过。。。)。
2、因为给的是通话数量,不是对象总数,所以最坏情况是每个通话都是两个不同的人,那么一共最多有2000个人。如果不注意,会导致段错误。
源代码2:
#include<iostream>
#include<algorithm>
#include<map>
#include<stdlib.h>
#include<vector>
#include<string>
#include<set>
using namespace std;
struct node
{
string s;
int time;
set<int>g;
};
vector<node>v;
map<string, int>mmap;
int book[2002] = { 0 };
struct gangs
{
int head, num, total;
};
vector<gangs>ans;
bool cmp(gangs x, gangs y)
{
return v[x.head].s < v[y.head].s;
}
void dfs(int i, int gcnt, int gang)
{
for (auto it = v[i].g.begin(); it != v[i].g.end(); it++)
{
if (book[*it] == 0)
{
ans[gcnt].num++;
ans[gcnt].total += v[*it].time;
if (v[*it].time > v[gang].time)ans[gcnt].head = *it;
book[*it] = 1;
dfs(*it,gcnt,ans[gcnt].head);
}
}
return;
}
int main()
{
int n, i, j, k, cnt=0, gcnt=0;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++)
{
string a, b;
int temp;
// scanf("%s %s %d", &a, &b, &temp);
cin >> a >> b >> temp;
if (mmap[a] == 0)
{
v.push_back({ a,temp });
mmap[a] = ++cnt;
}
else v[mmap[a] - 1].time += temp;
if (mmap[b] == 0)
{
v.push_back({ b,temp });
mmap[b] = ++cnt;
}
else v[mmap[b] - 1].time += temp;
v[mmap[a] - 1].g.insert(mmap[b] - 1);
v[mmap[b] - 1].g.insert(mmap[a] - 1);
}
for (i = 0; i < cnt; i++)
{
if (book[i] == 0)
{
book[i] = 1;
ans.push_back({ i,1,v[i].time });
dfs(i, gcnt, i);
if (ans[gcnt].num < 3 || ans[gcnt].total <= 2 * k)ans.erase(ans.end() - 1);
else gcnt++;
}
}
printf("%d\n", ans.size());
if (ans.size() != 0)
{
sort(ans.begin(), ans.end(), cmp);
for (i = 0; i < ans.size(); i++)//printf("%s %d\n", &v[ans[i].head].s, ans[i].num);
cout << v[ans[i].head].s << " " << ans[i].num << endl;
}
system("pause");
return 0;
}
注意事项:两种方法的代码都有一些繁杂冗余的地方,可以继续优化。