标签:返回 person find turn tostring 邻接矩阵 ota 数加 使用
map<string, int>
,进行解决。最后就是关于统计每一个连通分量是否达标,一个是人数,一个是阀值。所以需要重新开辟一个空间来进行记录。也是使用map,用权重最大的字符串,进行对应的人数。#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
const int INF = 1000000000;
map<int, string> intToString;//编号->姓名
map<string, int> stringToInt;//姓名->编号
map<string, int> Gang;//head->人数
int G[maxn][maxn] = {0}, weight[maxn] = {0};//邻接矩阵和点权weight
int n, k, numPerson;//边数n,下限k, 总人数numPerson
bool vis[maxn] = {false};//标记是否被访问
//DFS函数访问单个连通块,nowVisit为当前访问的编号
//head为头目,numMember为成员编号,totalValue为连通块的总边权
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] == false){
DFS(i, head, numMember, totalValue);
}
}
}
}
//DFSTrace函数遍历整个图
void DFSTrace(){
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){
//head人数为numMember
Gang[intToString[head]] = numMember;
}
}
}
}
//change函数返回姓名str对应的编号
int change(string str){
if(stringToInt.find(str) != stringToInt.end()){
return stringToInt[str];
}else{
stringToInt[str] = numPerson;
intToString[numPerson] = str;
return numPerson++;
}
}
int main(){
int w;
string str1, str2;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> str1 >> str2 >> w;
int id1 = change(str1);
int id2 = change(str2);
weight[id1] += w;
weight[id2] += w;
G[id1][id2] += w;
G[id2][id1] += w;
}
DFSTrace();
cout << Gang.size() << endl;
for(auto it = Gang.begin(); it != Gang.end(); it++){
cout << it->first << " " << it->second << endl;
}
return 0;
}
标签:返回 person find turn tostring 邻接矩阵 ota 数加 使用
原文地址:https://www.cnblogs.com/tsruixi/p/12364425.html