#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<map>
using namespace std;
const int maxn=2010;
int G[maxn][maxn]={0};
int weight[maxn]={0};
map<int,string> intostr;
map<string,int> strtoin;
map<string,int> gang;
int nummember=0;
int k;
int change(string str)
{
if(strtoin.find(str)==strtoin.end())
{
strtoin[str]=nummember;
intostr[nummember]=str;
return nummember++;
}
else
{
return strtoin[str];
}
}
bool visit[maxn]={false};
void dfs(int index,int &head,int &num,int &totalvalue)
{
if(visit[index]==false)
visit[index]=true;
num++;
if(weight[index]>weight[head])
head=index;
for(int i=0;i<nummember;i++)
{
if(G[index][i]>0)//图遍历顶点和边要分开讨论
{
totalvalue+=G[index][i];
G[index][i]=G[i][index]=0;
if(visit[i]==false)
dfs(i,head,num,totalvalue);
}
}
}
void dfstra()
{
for(int i=0;i<nummember;i++)
{
if(visit[i]==false)
{
int head=i;
int num=0;
int totalvalue=0;
dfs(i,head,num,totalvalue);
if(num>2&&totalvalue>k)
gang[(intostr[head])]=num;//map内部自动排序,按照字母表顺序;
}
}
}
int main()
{
int n;
scanf("%d%d",&n,&k);
string str1;
string str2;
int w;
for(int i=0;i<n;i++)
{
// scanf("%s %s %d",str1,str2,&w);
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;
}
dfstra();
printf("%d\n",gang.size());
map<string,int>::iterator it;
for(it=gang.begin();it!=gang.end();it++)
{
/// printf("%s %d\n",it->first.c_str(),it->second);
cout<<it->first<<" "<<it->second<<endl;
}
system("pause");
return 0;
}PAT 1034
最新推荐文章于 2022-02-04 15:45:07 发布
1366

被折叠的 条评论
为什么被折叠?



