2018ACM-ICPC北京賽區 - A:Jin Yong’s Wukong Ranking List(DFS)
題目連結:
時間限制: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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- unsupported operand type(s) for +: ‘range‘ and ‘list‘
- int[] 、 list<int> 、 list<int>[] 的區別
- [賽記] csp-s加賽1
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- Pharmaceutical prescription yong gang sex pill drugs like ViagraMac
- BiRank: Towards Ranking on Bipartite Graphs
- list-style與list-style-type的區別
- 2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)Git
- DFS
- List、Set、Map的區別
- set、List、map的區別
- [北京]雲奧賽凡正在找gopher[招滿]Go
- Sphinx Ranking Mode(排序模式) (翻譯)排序模式
- 2019 MiiX 區塊鏈 全球黑客馬拉松超級大賽(北京站)區塊鏈黑客
- 藍橋杯2015初賽生命之樹 DFS圖解圖解
- html中list-style-type與list-style的區別HTML
- std::vector 和 std::list 區別
- HDU 5131 Song Jiang's rank list(排序)排序
- [譯]Kotlin中的龜(List)兔(Sequence)賽跑Kotlin
- 2014年北京師範大學新生程式設計競賽網路賽程式設計
- DFS樹
- dfs序
- 第九屆藍橋杯省賽C++A組 倍數問題(dfs)C++
- [Offer收割]程式設計練習賽2 hihocoder 1273 (DFS + 狀壓)程式設計
- [賽記] csp-s模擬3
- [賽記] csp-s模擬5
- [賽記] csp-s模擬7
- B/S與C/S的區別
- 北京資訊科技大學第十一屆程式設計競賽(重現賽)I程式設計
- Angie’s List財報:2013年Q1 Angie’s List營收為5220萬美元 淨虧損$790萬營收
- 「CCPC 2023 北京市賽」史萊姆工廠
- [Offer收割]程式設計練習賽1 hihocoder 1268 九宮 (DFS)程式設計
- 【北京】區塊鏈工程師招聘區塊鏈工程師
- list和tuple元組的區別
- [賽記] csp-s模擬8 && csp-s模擬9
- Windows Server DFSWindowsServer
- 深搜dfs
- [DMY]CSP-S 模擬賽 Day 20