PAT A1034 Head of a Gang
作者:互联网
- 通话记录有1000条,人数可能为2000
- 给的人名是string,用两个map记录数字转换
- 每遍历过一条边后就把这条边的权值设为0,防止出现回路遍历死循环
#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 2010;
map<string, int> stringToint;//姓名-编号
map<int, string> intTostring;//编号-姓名
map<string, int> ans;
int G[maxn][maxn] = { 0 }, weight[maxn] = { 0 };
int n,k,numPerson=0;
bool vis[maxn] = { false };
int change(string str)
{
if (stringToint.find(str) != stringToint.end()) {
return stringToint[str];
}
else {
stringToint[str] = numPerson;
intTostring[numPerson] = str;
return numPerson++;
}
}
void DFS(int nowVisit, int& head, int& numMember, int& totalValue) {
numMember++;
vis[nowVisit] = true;
if (weight[nowVisit] > weight[head]) {
head = nowVisit;
}
for (int i = 0; i < numPerson; i++) {
if (G[nowVisit][i] != 0) {
totalValue += G[nowVisit][i];
G[nowVisit][i] = G[i][nowVisit] = 0;//删除边,防止回头
if (!vis[i]) {
DFS(i, head, numMember, totalValue);
}
}
}
}
void DFSTrave() {
for (int i = 0; i < numPerson; i++) {
if (vis[i] == false) {
int head = i, numMember = 0,totalValue=0;
DFS(i, head, numMember, totalValue);
if (numMember > 2 && totalValue > k) {
ans[intTostring[head]] = numMember;
}
}
}
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
string t1, t2;
int w;
cin >> t1 >> t2 >> w;
int id1 = change(t1);
int id2 = change(t2);
weight[id1] += w;
weight[id2] += w;
G[id1][id2] += w;
G[id2][id1] += w;
}
DFSTrave();
cout << ans.size() << endl;
map<string, int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++) {
cout << it->first << " " << it->second << endl;
}
return 0;
}
标签:Head,PAT,int,head,numPerson,numMember,A1034,nowVisit,include 来源: https://blog.csdn.net/Mind_The_Gap/article/details/114486490