hhhqzh


私信TA

用户名:hhhqzh

访问量:1857

签 名:

等  级
排  名 2573
经  验 2150
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 scnu
专  业

  自我简介:

解题思路:
           看了下别人的大致思路,知道可以用拓扑排序,即每一次输出入度为0的。用到了map,map的删除是在是坑。
注意事项:

参考代码:

#include <iostream>
#include <string>
#include <map>
using namespace std;

map<string,int> si;//string为人名,int为该人的入度数即前面女人的个数

int main()
{	
	int T,n,i;
	cin>>T;
	while(T!=0){
		si.clear();
		cin>>n;
		string **str = new string*[n];
		for(i=0;i<n;i++)
			str[i] = new string[2];
		for(i=0;i<n;i++){
			cin>>str[i][0]>>str[i][1];
			if(!si.count(str[i][0])){//判断map中是否有该人
				si[str[i][0]]=0;
			}
			if(!si.count(str[i][1])){
				si[str[i][1]]=1;
			}else{
				si[str[i][1]]++;
			}
		}
		while(si.size()){ 
			map<string,int>::iterator iter = si.begin();
			while(1){
				if(iter->second == 0){//前面没人(入度为0)就输出
					cout<<iter->first<<" ";
					for(i=0;i<n;i++){
						if(str[i][0]==iter->first){
							si[str[i][1]]--;//在她之后的入度都减一
						}
					}
					si.erase(iter->first);
					break;
				}else{
					iter++;
				}
			}
		}
		cout<<endl;
		T--;
	}
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区