题目链接:https://hihocoder.com/problemset/problem/1870
时间限制:1000ms 单点时限:1000ms 内存限制:512MB
描述
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.
输入
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.
输出
For each test case, print the first sentence which cause a contradiction. If there are no contradiction, print 0 instead.
提示
DON'T try to figure out who are those names in the sample and waste your time.
样例输入
2
BrokenReputation ExtinctNun
HelloLaught EnvelopeNotFlat
6
LandOverWind LonelyLight
FireMonk CutTheForest
CutTheForest LookCrazy
MakeFoxRush LetMeGo
HeroAunt UniqueLand
LookCrazy FireMonk
样例输出
0
LookCrazy FireMonk
解题报告
题意:给出一个n代表有n行,每行两个a,b字符串,代表a的武功高于b,问最早从第几行开始导致前后矛盾,若不矛盾输出0.
思路:这个题可以转化为有向图判定是否能连成环,因为数据比较小,我是用搜索做的。每加入一条边就判断一下,直到找到开始矛盾为止。
#include <stdio.h>
#include <string.h>
int s[25][25], vis[25], temp;
struct edge {
int s;
char str[35];
}e[25];
void DFS(int x, int n) {
temp = 0;
vis[x] = 1;
for (int y = 0; y < n; y++) {
if (s[x][y] && vis[y]) {
temp = 1;
return ;
}
if (s[x][y]) {
dfs(y, n);
if (temp)
return ;
vis[y] = 0;
}
}
}
int main() {
char a[35], b[35];
int x, y, n, m, k, cc, flag, flbg;
while (~scanf("%d", &n)) {
cc = k = 0;
memset(s, 0, sizeof(s));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
flag = flbg = 0;
scanf("%s%s", a, b);
for (int j = 0; j < k; j++) {
if (!flag && !strcmp(e[j].str, a)) {
x = e[j].s;
flag = 1;
}
if (!flbg && !strcmp(e[j].str, b)) {
y = e[j].s;
flbg = 1;
}
if (flag && flbg)
break;
}
if (!flag) {
strcpy(e[k].str, a);
x = e[k++].s = k;
}
if (!flbg) {
strcpy(e[k].str, b);
y = e[k++].s = k;
}
s[x][y] = 1;
DFS(x, k);
if (!cc && temp) {
printf("%s %s\n", a, b);
cc = 1;
}
}
if (!cc)
printf("0\n");
}
return 0;
}