1034. Head of a Gang (30) (并查集 或者 DFS)

来源:互联网 发布:java基础教程pdf下载 编辑:程序博客网 时间:2024/05/03 03:25

1034. Head of a Gang (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0

题意: 给定n组关系,每组关系给出两个人的姓名以及他们的通话时间,问有多少个"gang", 所谓的"gang"是指相互联系的超过2个人,并且他们总的通话时间超过给定的k值,并给出他们的领头人.

题解: 这题我是用的DFS做的,首先建图,然后根据每个点dfs一遍,看其是否符合上述条件,如果符合,加入答案里边,最后不要忘了按照名字排序.

或者是并查集也可以做.


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <vector>#include <map>using namespace std;const int N = 30 * 30 * 30;int tm[N], cnt[N];vector<int> V[N];bool vis[N];void dfs(int& id, int& num, int& total_time, int cur) {    if (tm[id] < tm[cur]) id = cur;    vis[cur] = true;    num++;    total_time += tm[cur];    for (int i = 0; i < V[cur].size(); ++i)        if (vis[V[cur][i]] == false)            dfs(id, num, total_time, V[cur][i]);}bool cmp(const pair<int, int>& p1, const pair<int, int>& p2) {    return p1.first < p2.first;}int get_id(const string & str) {    return (str[0] - 'A') * 26 * 26 + (str[1] - 'A') * 26 + (str[2] - 'A');}void show_string(const int id) {    printf("%c%c%c ", 'A' + id / 26 / 26, 'A' + (id / 26) % 26, 'A' + (id % 26));}int main() {    int n, k;    cin >> n >> k;    int total = 0;    while (n--) {        string name1, name2;        int time;        cin >> name1 >> name2 >> time;        int id1 = get_id(name1);        int id2 = get_id(name2);        V[id1].push_back(id2);        V[id2].push_back(id1);        tm[id1] += time, tm[id2] += time;        cnt[id1]++, cnt[id2]++;    }    memset(vis, false, sizeof(vis));    vector<pair<int, int> > ans;    for (int i = 0; i < 26 * 26 * 26; ++i) {        if (vis[i] == false) {            int id = 26 * 26 * 26, num = 0, total_time = 0;            dfs(id, num, total_time, i);            if (num > 2 && total_time > k * 2)                ans.push_back(make_pair(id, num));        }    }    cout << ans.size() << endl;    sort(ans.begin(), ans.end());    for (int i = 0; i < ans.size(); ++i) {        show_string(ans[i].first);        cout << ans[i].second << endl;    }    return 0;}


0 0
原创粉丝点击