2018icpc北京Jin Yong Wukong Ranking List传递闭包

时间限制: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 s2This 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

思路:

​ 比赛时队友用传递闭包做的,今天自己做了一下。这个题可以转化为有向图判定是否成环(利用map<string, int>映射一下名字),可以用bool型的book{i}{j}来代表从i到j是否连通。每一行都更新一下,若book{i}{j}和book{j}{i}都为1就代表前后矛盾

代码:

#include <stdio.h>
#include <map>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

string s1[25], s2[25];
map<string, int>zcy;
bool book[50][50];
int res1[50], res2[50];

int main () {
    int n;
    while (scanf("%d", &n) == 1) {
        zcy.clear();
        memset(book, 0, sizeof(book));
        int tot = 0;
        int flag = 0;
        for (int i = 1; i <= n; i++) {
            cin >> s1[i];
            cin >> s2[i];
            if (!zcy[s1[i]]) {
                zcy[s1[i]] = ++tot;
            }
            if (!zcy[s2[i]]) {
                zcy[s2[i]] = ++tot;
            }
            if (book[zcy[s2[i]]][zcy[s1[i]]] || flag) {
                continue;
            }
            res1[0] = zcy[s2[i]];
            res2[0] = zcy[s1[i]];
            int tot1 = 0, tot2 = 0;
            for (int j = 1; j <= tot; j++) {
                if(book[j][zcy[s2[i]]] == 1) {
                    res1[++tot1] = j;
                }
            }
            for (int j = 1; j <= tot; j++) {
                if(book[zcy[s1[i]]][j] == 1) {
                    res2[++tot2] = j;
                }
            }
            for (int p = 0; p <= tot1; p++) {
                for (int q = 0; q <= tot2; q++) {
                    if(book[res2[q]][res1[p]] == 0) {
                        book[res1[p]][res2[q]] = 1;
                    } else {
                        flag = 1;
                        cout << s1[i] << " " << s2[i] << endl;
                        break;
                    }
                }
                if(flag) break;
            }
        }
        if (!flag) {
            printf("0\n");
        }
    }
    return 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值