DFS + map

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#define N 2010
using  namespace std;
map<string,int> string2int;
map<int,string> int2string;
map<string,int> gang;
int G[N][N]={0},w[N]={0};
bool vis[N] = {false};
int idNum = 1;
int k;
int change(string s){
    if(string2int[s] == 0){
        string2int[s] = idNum;
        int2string[idNum]=s;
        return idNum++;
    }else{
        return string2int[s];
    }
}
void DFS(int u,int &h,int &m,int &t){
    m++;
    vis[u] = true;
    if(w[u] > w[h]){
        h = u;
    }
    for(int v=1;v < idNum;v++){
        if(G[u][v] > 0){ //如果有边 
            t += G[u][v];
            G[u][v]=G[v][u] = 0;
            if(vis[v]==false){
                DFS(v,h,m,t);
            }
        }
    }
}
void DFSTrave(){
    for(int i=1; i<idNum;i++){
        if(vis[i]==false){
            int head = i,numMem=0,totalV=0;//头目,总人数,总边权
            DFS(i,head,numMem,totalV);
            if(numMem > 2 && totalV > k){
                gang[int2string[head]] = numMem;
            } 
        }
    }
}
int main(){ 
    int n,d;
    string str1,str2;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>str1>>str2>>d;
        int id1 = change(str1);
        int id2 = change(str2);
        w[id1] += d;  //点权 
        w[id2] += d;  
        G[id1][id2] += d; //边权 
        G[id2][id1] += d;   
        }
    DFSTrave(); 
    cout<<gang.size()<<endl;
    map<string,int>::iterator it;
        for(it = gang.begin();it !=gang.end();it++){
            cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}