PAT 1034

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值