2018ACM-ICPC北京站A题

Jin Yong’s Wukong Ranking List
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.

感谢学长挑的这道题,看到别人都过了有一点自闭,读完题第一感觉就是一道并查集,对之前并查集写的食物链题有点印象,A吃B,B吃C,C吃A,丢到三个集合里,还有另外一道黑帮的题,A和B不是同意集合,所以觉得是并查集,立马就开始写了,WA了两发之后还是不回头,到比赛结束都没写出来,赛后查题解没找到我这种做法,老老实实康了康别人的做法,有好几种,可惜我一种也没想出来,这里说一下我过了的做法,读入一句语句后,找第一个人名的祖先,一个一个判断,看第一个人的祖先里有没有第二个人,如果有,这句话就是错的,如果找到头都没找到,就把第二个人的祖先定位第一个人。队友还写了另外一种找环的写法,另外还有拓扑排序判环,弗洛伊德判环(时间复杂度太高)。

#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
using namespace std;
string a,b;
map<string,int>m;
int fa[1050];
void init()
{
    for(int i=0; i<1050; ++i)fa[i]=i;
}
int find(int x,int y)
{
    if(fa[x]==x)return 0;
    if(fa[x]==y)return 1;
    return find(fa[x],y);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        m.clear();
        string ans1,ans2;
        int cut=1;
        int ans=0;
        while(n--)
        {
            cin>>a>>b;
            if(!m[a])
            {
                m[a]=cut++;
            }
            if(!m[b])
            {
                m[b]=cut++;
            }
            int x=m[a],y=m[b];
            if(!find(x,y))
            {
                fa[y]=x;
            }
            else
            {
                if(!ans)
                {
                    ans++;
                    ans1=a,ans2=b;
                }
            }
        }
        if(!ans)printf("%d\n",0);
        else cout<<ans1<<" "<<ans2<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值