1034 Head of a Gang (30 分)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.

一、用图的dfs找出连通分量数,在这个过程中找到符合条件的gang以及其head

#include<iostream>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 2100
struct node{
    string name;
    int weight;	//通话时长
    int isHead=0;
    int totalp=0;//这个gang的总人数
}nodes[maxn];
int graph[maxn][maxn];
int mark[maxn];
map<string,int>StrToInt;//name与在graph中的下标相互转换
map<int,string>IntToStr;
int n,k;
int counts=0;
int curpos=0;
bool cmp(node n1,node n2){
    return n1.name<n2.name;
}
void dfs(int start,int &head,int &totalw){
    mark[start]=1;
    counts+=1;
    head=(nodes[start].weight>nodes[head].weight)?start:head;
    for(int i=0;i<curpos;i++){
        if(graph[start][i]!=0){
            totalw+=graph[start][i];
            graph[start][i]=graph[i][start]=0;
            if(mark[i]==0){
                dfs(i,head,totalw);
            }
        }
    }
}
int main(){
    cin>>n>>k;
    for (int i = 0; i < n; ++i) {
        mark[i]=0;
        memset(graph[i],0, sizeof(graph[i]));
    }
    for (int i = 0; i < n; ++i) {
        string n1,n2;
        int toll;
        cin>>n1>>n2>>toll;
        if(StrToInt.find(n1)!=StrToInt.end()){
            nodes[StrToInt[n1]].weight+=toll;
        }
        else{
            StrToInt[n1]=curpos;
            nodes[curpos].weight=toll;
            nodes[StrToInt[n1]].name=n1;
            curpos++;
        }
        if(StrToInt.find(n2)!=StrToInt.end()){
            nodes[StrToInt[n2]].weight+=toll;
        }
        else{
            StrToInt[n2]=curpos;
            nodes[curpos].weight=toll;
            nodes[StrToInt[n2]].name=n2;
            curpos++;
        }
        graph[StrToInt[n1]][StrToInt[n2]]+=toll;
        graph[StrToInt[n2]][StrToInt[n1]]+=toll;
    }
    for(int i=0;i<curpos;i++){
        int pre=counts;
        int head=i;
        int totalw=0;
        if(mark[i]==0){
            dfs(i,head,totalw);
            nodes[head].totalp=counts-pre;
            nodes[head].isHead=(nodes[head].totalp>2&&totalw>k)?1:0;
        }
    }
    sort(nodes,nodes+curpos,cmp);
    int gangs=0;
    for (int i = 0; i < curpos; ++i) {
        if(nodes[i].isHead==1)gangs++;
    }
    cout<<gangs<<endl;
    for (int i = 0; i < curpos; ++i) {
        if(nodes[i].isHead)cout<<nodes[i].name<<" "<<nodes[i].totalp<<endl;
    }
    return 0;
}

二、用并查集做,需要注意的是,要在全部输入完成并计算出每个人的总的通话时长后(以便于确定谁是head),才可以建立并查集,否则一边输入一边构建可能很麻烦(没试过)。所以需要用一个东西把每一对记录存储起来。

#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
struct peo {
	string father;
	int cnt, total;//cnt是gang的人数,total是gang内所有人的通话时间之和
};
unordered_map<string, peo>rec;
vector<string>temp;//记录输入
unordered_map<string, int>mins;//计算每个人的总通话时长
vector<peo>ans;//用于将符合条件的gang的head排序
int n, k;
string findF(string str) { return rec[str].father == str ? str : findF(rec[str].father); }
void unionF(string s1, string s2) {
	string f1 = findF(s1);
	string f2 = findF(s2);
	if (f1 != f2) {
		string s;
		if ((mins[f1] > mins[f2])||(mins[f1] == mins[f2] && f1 < f2)) {
			rec[f2].father = f1;
			s = f1;
		}
		else if ((mins[f1] < mins[f2])||(mins[f1] == mins[f2] && f1 > f2)) {
			rec[f1].father = f2;
			s = f2;
		}
		rec[s].cnt = rec[f1].cnt + rec[f2].cnt;
		rec[s].total = rec[f1].total + rec[f2].total;
	}
}
bool cmp(peo p1, peo p2) { return p1.father < p2.father; }

int main() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		string s1, s2;
		int m;
		cin >> s1 >> s2 >> m;
		temp.push_back(s1);
		temp.push_back(s2);
		if (mins.count(s1) == 0)mins[s1] = m;
		else
			mins[s1] += m;
		if (mins.count(s2) == 0)mins[s2] = m;
		else
			mins[s2] += m;
	}
	for (int i = 0; i < temp.size(); i += 2) {//每两个为一组记录
		if (rec.count(temp[i]) == 0) {
			rec[temp[i]] = { temp[i],1,mins[temp[i]] };
		}
		if (rec.count(temp[i + 1]) == 0) {
			rec[temp[i + 1]] = { temp[i + 1],1,mins[temp[i + 1]] };
		}
		unionF(temp[i], temp[i + 1]);
	}
	for (auto it = rec.begin(); it != rec.end(); it++) {
		if ((it->second.father == it->first) && (it->second.cnt > 2) && (it->second.total / 2 > k)) {//每一通电话双方的通话时间都计入了,相当于多加了一次,类似于无向图的度等于边数x2
			ans.push_back(it->second);
		}
	}
	sort(ans.begin(), ans.end(), cmp);
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i].father << " " << ans[i].cnt << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值