A1034 Head of aGang

#include
#include
#include
using namespace std;
const int maxn=2010;

map intToString;
map stringToInt;
map Gang;
int G[maxn][maxn]={0},weight[maxn]={0};
int n,k,numPerson=0;
bool vis[maxn]={false};

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;i0)
		{
			totalValue+=G[nowVisit][i];
			G[nowVisit][i]=G[i][nowVisit]=0;
			if(vis[i]==false)
			{
				DFS(i,head,numMember,totalValue);
			}
		}
	}
}

void DFSTrave()
{
	for(int i=0;i2&&totalValue>k)
			{
				Gang[intToString[head]]=numMember;
			}
		}
	}
}

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>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;
	}
	DFSTrave();
	cout<::iterator it;
	for(it=Gang.begin();it!=Gang.end();it++)
	{
		cout<first<<" "<second<

你可能感兴趣的:(PAT甲级)