1034 Head of a Gang (30分)

#include<string>
#include<iostream>  
#include<unordered_map>  
#include<vector> 
#include<algorithm>
using namespace std;
struct node {
	int value;
	int id;
};
int  n, k, numb = 0,numbgang=0;
vector<int> score(2000, 0);
int all[2000][2000] = { 0 };
vector<bool> visited(2000, false);
vector<node> head;
unordered_map<int, string> id2n;
unordered_map<string, int> n2id;
bool cmp(node& a, node& b) {
	return id2n[a.id] < id2n[b.id];
}
void DFS(int root, int& totalsocore) 
{
	visited[root] = true;
	if (score[root] > score[head[numbgang - 1].id]) {
		head[numbgang - 1].id = root;
	}
	for (int i = 0; i < numb; i++) {
		if (all[root][i]!=0&&visited[i] == false) {
			head[numbgang - 1].value++;
			totalsocore += score[i];
			DFS(i, totalsocore);
		}
	}
}
int main() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		string a, b;
		int time;
		cin >> a >> b >> time;
		if (!n2id.count(a)) {
			n2id[a] = numb;
			id2n[numb++] = a;

		}
		if (!n2id.count(b)) {
			n2id[b] = numb;
			id2n[numb++] = b;
		}
		score[n2id[a]] += time;
		score[n2id[b]] += time;
		all[n2id[a]][n2id[b]]+=time;
		all[n2id[b]][n2id[a]]+=time;
	}  
	for (int i = 0; i < numb; i++) {
		if (visited[i] == false) {
			numbgang++;
			int totalsocore = score[i];
			head.push_back(node{ 1,i});
			DFS(i,totalsocore);
			if ((totalsocore/2) <= k||head[numbgang-1].value<=2) {
				numbgang--;
				head.erase(head.end()- 1);
			}
		}
	}
	sort(head.begin(), head.end(), cmp);
	cout << numbgang;
	for (int i = 0; i < numbgang; i++) {
		cout << "\n" << id2n[head[i].id] << " " << head[i].value;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值