码迷,mamicode.com
首页 > 其他好文 > 详细

链式前向星上DFS(Pants On Fire)

时间:2019-08-18 17:53:22      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:leader   integer   http   input   problem   put   group   printf   sea   

Pants On Fire

时间限制: 1 Sec  内存限制: 128 MB
提交: 161  解决: 66
[提交] [状态] [命题人:admin]

题目描述

Donald and Mike are the leaders of the free world and haven’t yet (after half a year) managed to start a nuclear war. It is so great! It is so tremendous!
Despite the great and best success of Donald’s Administration, there are still a few things he likes to complain about.
The Mexican government is much smarter, much sharper, and much more cunning.
And they send all these bad hombres over because they don’t want to pay for them.
They don’t want to take care of them.
Donald J. Trump, First Republican Presidential Debate, August 6, 2015
He also frequently compares Mexicans to other bad people (like Germans, since they are exporting so many expensive cars to the US). Due to the tremendous amount of statements he has made (mostly containing less than 140 characters ...) the “Fake-News” New York Telegraph (NYT) has to put in a lot of effort to clarify and comment on all the statements of Donald. To check a statement, they have a list of facts they deem to be true and classify Donald’s
statements into three groups: real facts (which are logical conclusions from their list of true facts), exaggerations (which do not follow, but are still consistent with the papers list of facts),and alternative facts (which contradict the knowledge of the newspaper).
They have asked you to write a program helping them to classify all of Donald’s statements –after all it is hard for a journalist to go through them all and check them all, right?

 

输入

The input consists of:
• one line containing two integers n and m, where
– n (1 ≤ n ≤ 200) is the number of facts deemed true by the NYT;
– m (1 ≤ m ≤ 200) is the number of statements uttered by the Donald.
• n lines each containing a statement deemed true by the NYT.
• m lines each containing a statement uttered by the Donald.
All statements are of the form a are worse than b, for some strings a and b, stating that a is (strictly) worse than b. The strings a and b are never identical. Both a and b are of length between 1 and 30 characters and contain only lowercase and uppercase letters of the English alphabet.
Note that Donald’s statements may contain countries that the NYT does not know about. You may assume that worseness is transitive and that the first n lines do not contain any contradictory statement. Interestingly, Donald’s press secretary (Grumpy Sean) has managed to convince him not to make up countries when tweeting, thus the input mentions at most 193 different countries.

 

输出

For every of the m statements of Donald output one line containing
• Fact if the statement is true given the n facts of the NYT
• Alternative Fact if the inversion of the statement is true given the n facts of the NYT
• Pants on Fire if the statement does not follow, but neither does its inverse.

 

样例输入

 

 1 4 5
 2 Mexicans are worse than Americans
 3 Russians are worse than Mexicans
 4 NorthKoreans are worse than Germans
 5 Canadians are worse than Americans
 6 Russians are worse than Americans
 7 Germans are worse than NorthKoreans
 8 NorthKoreans are worse than Mexicans
 9 NorthKoreans are worse than French
10 Mexicans are worse than Canadians

 

样例输出

 1 Fact           
2 Alternative Fact
3 Pants on Fire
4 Pants on Fire
5 Pants on Fire
 

 


 

题意:给n个语句,代表两两之间的关系;m次询问语句,判断是否语句关系正确,是否反向关系正确;如果不正确或者不存在,输出 Pants on Fire

 


题解:首先用map把国家与国家之间的关系映射成数字;然后加边,建图;DFS一下,判断国家与国家之间是否可达,国家是否存在; 

代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define MAXN 210
 4 const int N=1e4+5;
 5 map<string,int> h;
 6 struct ss
 7 {
 8    int u,v,next;
 9 }edge[N];
10  
11 int head[N],sum_edge=1,n;
12  
13 void addedge(int u,int v)
14 {
15     edge[sum_edge]=(ss){u,v,head[u]};
16     head[u]=sum_edge++;
17 }
18 bool dfs(int now,int t)
19 {
20     if(now==t) //如果t可达 返回true
21     {
22         return true;
23     }
24     for(int i=head[now];i!=-1;i=edge[i].next)
25     {
26         if(dfs(edge[i].v,t)) 
27         return true;
28     }
29     return false;
30 }
31 char str1[20],str2[20],te[20];
32 int main()
33 {
34     int m,j=1;
35     scanf("%d%d",&n,&m);
36     memset(head,-1,sizeof(head));
37     for(int i=0;i<n;i++)
38     {
39         scanf("%s",str1);
40         if (h[str1]== 0) //判断映射是否存在
41         h[str1]=j++;
42         
43         scanf("%s",te);scanf("%s",te);scanf("%s",te);
44         
45         scanf("%s",str2);
46         if (h[str2]== 0)
47         h[str2]=j++;
48         addedge(h[str1],h[str2]);
49     }
50     while(m--)
51     {
52         scanf("%s",str1);
53         scanf("%s",te);scanf("%s",te);scanf("%s",te);
54         scanf("%s",str2);
55         if(h[str1]==0||h[str2]==0) 
56         {
57             printf("Pants on Fire\n");
58             continue;
59         }
60         if(dfs(h[str1],h[str2]))
61             printf("Fact\n");
62         else if(dfs(h[str2],h[str1]))
63             printf("Alternative Fact\n");
64         else
65             printf("Pants on Fire\n");
66     }
67     return 0;
68 }
69  

 

链式前向星上DFS(Pants On Fire)

标签:leader   integer   http   input   problem   put   group   printf   sea   

原文地址:https://www.cnblogs.com/sylvia1111/p/11372766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!