pat1033

map容器的用法!!!以及dfs基于map的实现

http://blog.csdn.net/matrix5467/article/details/8641186

#include
#include
#include
#include
#include
using namespace std;

map > adj;//邻接表
map weight;//各点权重
map visit;//是否访问过
map ans;//最后要输出的结果
int cnt,totalweight;
string head;

void dfs(string str)
{
	visit[str]=1;
	cnt++;//该集合中结点个数
	totalweight+=weight[str];
	if(weight[str]>weight[head])
		head=str;
	else if(weight[str]==weight[head]&&str::iterator it=adj[str].begin();it!=adj[str].end();it++){
		if(visit[*it]==0){

			dfs(*it);

		}
	}
}

int main()
{
	int n,k,time;
	string name1,name2;
	//freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);

	cin>>n>>k;
	int i;
	for(i=0;i>name1>>name2>>time;
		weight[name1]+=time;
		weight[name2]+=time;
		adj[name1].push_back(name2);
		adj[name2].push_back(name1);
		visit[name1]=0;
		visit[name2]=0;
	}
	for(map::iterator it=visit.begin();it!=visit.end();it++){
		if(visit[it->first]==0){
			cnt=0;//该集合中结点个数
			totalweight=0;//该集合总权重
			head=it->first;
			dfs(it->first);
			if(cnt>2 && totalweight/2 > k)//满足条件则登记
				ans[head]=cnt;
		}
	}

	cout<::iterator iter=ans.begin();iter!=ans.end();iter++){
		cout<first<<" "<second<


你可能感兴趣的:(Leetcode)