1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream> #include <string> #include <map> using namespace std; const int maxn=2010; int n,k,personId=0,weight[maxn]={0},G[maxn][maxn]={0}; bool vis[maxn]={false}; map<string,int> stringToint; map<int,string> intTostring; map<string,int> Gang;
int change(string str){ if(stringToint.find(str)!=stringToint.end()){ return stringToint[str]; }else{ stringToint[str]=personId; intTostring[personId]=str; return personId++; } }
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<personId;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); } } } }
void DFSTravel(){ for(int i=0;i<personId;i++){ if(vis[i]==false){ int head=i,numMember=0,totalValue=0; DFS(i,head,numMember,totalValue); if(numMember>2&&totalValue>k){ Gang[intTostring[head]]=numMember; } } } }
int main(){ cin>>n>>k; string s1,s2; int w; for(int i=0;i<n;i++){ cin>>s1>>s2>>w; int id1=change(s1); int id2=change(s2); weight[id1]+=w; weight[id2]+=w; G[id1][id2]+=w; G[id2][id1]+=w; } DFSTravel(); cout<<Gang.size()<<endl; for(auto it=Gang.begin();it!=Gang.end();it++){ cout<<it->first<<" "<<it->second<<endl; }
return 0; }
|