2018ACM-ICPC北京賽區 - A:Jin Yong’s Wukong Ranking List(DFS)

chw_throx發表於2022-03-09

題目連結:

時間限制: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;

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22198239/viewspace-2868744/,如需轉載,請註明出處,否則將追究法律責任。

相關文章