PAT甲级1034 并查集

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 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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include
using namespace std;
struct node{
    int id=-1;
    int time=0,max0=999999;
    int renshu = 0;
    
};
vector father(1000000,-1);
vector timef(1000000,0);
vector ans(1000000);

int findfather(int x){
    if(x!=father[x]){
        father[x] = findfather(father[x]);
    }
    return father[x];
}
void _union(int x,int y) father[findfather(x)] = findfather(y);;

int main(){
    int n,k;
    cin>>n>>k;
    string a1,a2;
    int b1,b2,time;
    for(int i=0;i>a1>>a2>>time;
        string c1,c2;
        for(int j=0;j<3;j++) c1+=to_string(a1[j]-'0');
        for(int j=0;j<3;j++) c2+=to_string(a2[j]-'0');
        b1=stoi(c1);
        b2=stoi(c2);
        if(father[b1]==-1)father[b1] = b1;
        if(father[b2]==-1)father[b2] = b2;
        timef[b1]+=time;
        timef[b2]+=time;
        _union(b1,b2);
    }
    //17a 18b 19c 20d 21e 22f 23g 24h;
    for(int i=100000;i<1000000;i++){
        if(father[i]!=-1){
            int x = findfather(i);
            if(ans[x].id==-1){
                ans[x].id=1;
                ans[x].max0 = i;
            }
            ans[x].time+=timef[i]/2;
            ans[x].renshu++;
            if(timef[i]>timef[ans[x].max0]) ans[x].max0=i;
        } 
    }
    
    vector v;
    
    for(int i=100000;i<1000000;i++) if(ans[i].id!=-1&&ans[i].renshu>2&&ans[i].time>k) v.push_back(ans[i]);
    sort(v.begin(),v.end(),[](node &a1,node &a2){
        return a1.max0

你可能感兴趣的:(PAT练习,算法)