//带点权边权的图的遍历
//code:
#include <cstdio>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 2010;
const int INF = 1000000000;
int N, K, numPerson = 0;
map<string, int> stringToInt; //姓名->编号
map<int, string> intToString; //编号->姓名
map<string, int> Gang;
int G[MAXN][MAXN], weight[MAXN];
bool vis[MAXN] = {false};
int head, numMember, totalValue; //全局
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 i) {
if(vis[i] == false) {
vis[i] = true;
numMember ++;
if(weight[i] > weight[head]) head = i;
for(int j=0; j<numPerson; j++) {
if(G[i][j] > 0) {
totalValue += G[i][j];
G[i][j] = G[j][i] = 0; 标记边的方式,即用完就去掉
dfs(j);
}
}
}
}
void dfsTrave() {
for(int i=0; i<numPerson; i++) {
if(vis[i] == false) {
head = i; //i
numMember = 0;
totalValue = 0;
dfs(i);
if(numMember > 2 && totalValue > K) Gang[intToString[head]] = numMember;
}
}
}
int main() {
scanf("%d%d",&N,&K);
string str1, str2;
int id1, id2, w;
for(int i=0; i<N; i++) {
cin >> str1 >> str2 >> w;
id1 = change(str1);
id2 = change(str2);
weight[id1] += w; //与w有联系的都是增加w
weight[id2] += w; //与w有联系的都是增加w
G[id1][id2] += w; //d1->id2的边权增加w
G[id2][id1] += w; //id2->id1的边权增加w
}
memset(vis,false,sizeof(vis));
dfsTrave();
printf("%d\n",Gang.size());
for(map<string, int>::iterator it = Gang.begin(); it != Gang.end(); it++)
cout << it->first << " " << it->second << endl; //错误:printf("%s %d\n",it->first,it->second);
return 0;
}
1034 Head of a Gang (30)
最新推荐文章于 2019-08-30 17:36:15 发布