有向无环图 STL map

Training 2 - B题

Jin Yong was the most famous and popular Chinese wuxia (The one who fight bad people by his Wukong i.e. Wushu and Kongfu) novelist who lived in Hong Kong. Between 1955 and 1972, he wrote 14 novels which earned him a reputation as one of the greatest and most popular Chinese writers. Over 100 million copies of his works have been sold worldwide,not including a countless number of pirated copies. Jin Yong’s works seem to have magic. Once you begin to read a novel of his, you just can’t stop until you finish it.
Last month, Jin Yong passed away at the age of 94. Many Jin Yong’s fans in PKU held a meeting to memorize him. Jin Yong’s fans always like to discuss or argue or even quarrel about whose Wukong are better among the wuxia characters of his novel. During the meeting, this happened again:
Every fans said some words like “Qiao Feng’s Wukong is better than Guo Jing’s”. Obviously, those words may contradict each other and then cause quarrels. As a boring and girlfriendless male programmer of EECS school, you always want to make some things. So you are eager to point out the contradictions as soon as possible. That means, you want to find out the first one whose words contradict the words said by others before him.
Please note that if A is better than B, and B is better than C, then of course A must be better than C.

Input

There are no more than 15 test cases.
For each test case:
The first line is an integer n( 1 <= n <=20), meaning that there are n sentences.
The following n lines are those n sentences which is in the format below:
s1 s2.
This means someone said that s1’s Wukong was better than s2’s. Both s1 and s2 are names of Jin Yong’s characters which consists of only English letters. It’s guaranteed that s1 and s2 are different, and their length is no more than 30. Names are case sensitive.

Output

For each test case, print the first sentence which cause a contradiction. If there are no contradiction, print 0 instead.

Sample Input

2
BrokenReputation ExtinctNun
HelloLaught EnvelopeNotFlat
6
LandOverWind LonelyLight
FireMonk CutTheForest
CutTheForest LookCrazy
MakeFoxRush LetMeGo
HeroAunt UniqueLand
LookCrazy FireMonk

Sample Output

0
LookCrazy FireMonk

Hint

DON’T try to figure out who are those names in the sample and waste your time.

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 20 + 5;

map<string, vector<string>> a;
set<string> visit;

bool dfs(string x, string y)
{
	visit.insert(x);
	map<string, vector<string>>::iterator iter = a.find(x);
	if (iter != a.end())
	{
		for (int i = 0; i < (*iter).second.size(); i++)
		{
			if ((*iter).second[i] == y)
				return true;
			if (visit.end() == visit.find((*iter).second[i]))
				return dfs((*iter).second[i], y);
		}
	}
	return false;
}

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		a.clear();
		getchar();
		bool flag = 0;
		for (int cas = 1; cas <= n; cas++)
		{
			string str;
			getline(cin, str);
			string s1, s2;
			string tmp = "";
			for (int i = 0; str[i] != '\0'; i++)
			{
				if (str[i] == ' ')
				{
					s1 = tmp;
					tmp = "";
				}
				else
					tmp += str[i];
			}
			s2 = tmp;
			visit.clear();
			if (!dfs(s2, s1))
			{
				a[s1].push_back(s2);
			}
			else
			{
				cout << s1 << " " << s2 << endl;
				flag = 1;
			}
		}
		if (!flag)
			printf("0\n");
	}
	return 0;
}

思路:
维护一个节点是string类型的图,每次判断新的边在图中是否能构成环(判断逆向边是否路径相通)。
string类型的图用map存储。

附上大佬的简短代码 %%%
在这里插入图片描述

发布了28 篇原创文章 · 获赞 0 · 访问量 330

猜你喜欢

转载自blog.csdn.net/xukeke12138/article/details/104712160
今日推荐