【PAT 1034】 Head of a Gang 图论DFS

来源:互联网 发布:阿里云 nodejs 编辑:程序博客网 时间:2024/05/23 01:23

1034. Head of a Gang (30)

时间限制
100 ms
内存限制
32000 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 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 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
题意:

『gang』翻译过来是『一伙人』。gang 的定义是一群人,至少有 3 个人,这群人中每个人之间都通过通话相连,且整个群体的通话时长超过一个阈值。整个 gang 的团体中,拥有的电话时长最长的人就是头目了。

分析:

使用 vector<int> arr[LEN];存储该有向图,并求得每个节点的度数(入度+出度);
采用DFS搜索所有连通子图;
判断连通子图中节点个数及总度数是否满足Gang条件;
若满足则记录下度数最大的节点及节点个数;
对结果排序后输出。

代码:

#include <iostream>#include <fstream>#include <algorithm>#include <vector>#include <queue>#include <cstring>using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")ifstream fin("in.txt");#define cin finstruct HeadNum{int name;int num;HeadNum(int nam,int n){name = nam;num=n;}};const int LEN = 26*26*26+1;vector<int> arr[LEN];int sum[LEN]={0};bool isVisited[LEN]={false};vector<int> linkNode;int cmp(const HeadNum& aa,const HeadNum &bb){return aa.name < bb.name;}int hashName(const char name[]){//将字符数组hash成整型return (name[0]-'A')*26*26+(name[1]-'A')*26+name[2]-'A';}void int_to_name(int n,char *s){//整型转换回 字符数组s[3]='\0';  s[2]=n%26+'A';  s[1]=(n/26)%26+'A';  s[0]=n/(26*26)+'A';  } void dfs(int head){//深度优先搜索if(isVisited[head])return;isVisited[head] = true;int size = arr[head].size();for(int i=0;i<size;i++){dfs(arr[head][i]);}linkNode.push_back(head);return;}HeadNum* isGang(const vector<int> &v,int k){//判断是否算的上一个Gang团伙int s = v.size();if(s<3)return NULL;//人数小于3人不构成团伙int total = 0;int max = 0;int head = -1;for(int i=0;i<s;i++){total += sum[v[i]];//计算一个团伙的总时长(每段时长都被计算2次)if(max < sum[v[i]]){max = sum[v[i]];head = v[i];}}if(total/2 <= k){//这里要除以2return NULL;}else{return new HeadNum(head,s);}}int main(){int n,k;cin>>n>>k;queue<int> node;vector<HeadNum> res;int i;char name1[4],name2[4];int intName1,intName2,value;for(i=0;i<n;i++){cin>>name1>>name2;intName1 = hashName(name1);intName2 = hashName(name2);arr[intName1].push_back(intName2);cin>>value;sum[intName1] += value;sum[intName2] += value;node.push(intName1);}HeadNum* head = NULL;while(!node.empty()){linkNode.clear();//linkNode存储每次联通子图dfs(node.front());node.pop();head = isGang(linkNode,k);//判断联通子图是否满足团伙条件,若满足则返回{头目姓名,团伙人数}if(head!=NULL){res.push_back(*head);}}sort(res.begin(),res.end(),cmp);//将结果按照 head姓名进行排序cout<<res.size()<<endl;char name[4];for(i=0;i<res.size();i++){int_to_name(res[i].name,name);//整型转换回字符数组cout<<name<<' '<<res[i].num<<endl;}system("PAUSE");return 0;}


0 0
原创粉丝点击