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>[] 的區別
- BiRank: Towards Ranking on Bipartite Graphs
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- P2853 [USACO06DEC]Cow Picnic S 【DFS】
- 2018 瀋陽賽區網路預賽 I.Lattice's basics in digital electronics(模擬)Git
- 冬奧賽事期間對北京和張家口三個比賽區的所有奧運場館
- [賽記] csp-s加賽1
- 藍橋杯2015初賽生命之樹 DFS圖解圖解
- 2019 MiiX 區塊鏈 全球黑客馬拉松超級大賽(北京站)區塊鏈黑客
- DFS
- html中list-style-type與list-style的區別HTML
- 競無限速,S聯賽2019年春季賽今日開賽
- std::vector 和 std::list 區別
- [XSS神器]XssEncode chrome外掛 - 0x_JinChrome
- 牛客練習賽27【C 水圖 dfs求最長路】
- [譯]Kotlin中的龜(List)兔(Sequence)賽跑Kotlin
- PAT A1025 PAT Ranking(sort部分排名)
- Allwinner(全志)V3s Camera sensor Support List
- [北京]雲奧賽凡正在找gopher[招滿]Go
- list和tuple元組的區別
- 北京pk賽車帶回血,北京pk10有回血成功的嗎
- CSP-S模擬賽20241004
- CSP-S2024初賽心得
- CSP-S 2024 初賽解析
- dfs序
- DFS樹
- dfs技巧
- Sigir2024 ranking相關論文速讀
- 第九屆藍橋杯省賽C++A組 倍數問題(dfs)C++
- 「CCPC 2023 北京市賽」史萊姆工廠
- CSP-S 2024 賽後總結
- [賽記] csp-s模擬7
- [賽記] csp-s模擬5
- [賽記] csp-s模擬3
- 深搜dfs
- Tempter of the Bone(DFS)
- C/S,B/S的應用的區別