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

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  

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

时间: 2024-08-02 18:58:57

链式前向星上DFS(Pants On Fire)的相关文章

链式前向星-学习笔记

模板: 数据结构: int head[LEN]; //记录源点u在mp中第一个地址i=head[u] 调用完之后就可以用mp[i]访问边表mp int cnt=0; //边表下标,随着数据的录入而扩张 struct edge{ //边 int to,next,w; }; edge mp[LEN]; //边表 加边函数: void add(int u,int v,int w){ //增加边 mp[cnt].to=v; mp[cnt].w=w; mp[cnt].next=head[u]; //指向源

链式前向星写法下的DFS和BFS

Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace std; const int maxn = 150; const int maxm = 1050; int n, m;//顶点数,边数 int head[maxm], tot; bool used[maxn]; //head[u]表示已知的最后一条以u为起点的边在边集e中的下标 struct edge {

链式前向星DFS

采用链式前向星存图的DFS: #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> using namespace std; typedef long long LL; const int maxN = 100 + 3

NYOJ 20 吝啬的国度 【BFS+链式前向星建图,Vector建图】

吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设你不走重复的路). 输入 第一行输入一个整数M表示测试数据共有M(1<=M<=5)组 每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000

【前向星】链式前向星实现以及它的遍历

深度理解链式前向星 链式前向星的构成由一个结构体(包括目标点.边权值和下一个同起点的边)和head数组(用于存放某点的第一条出边) 必要的时候还可以添加一个统计入度的数组,因为进行BFS DFS的时候是依靠点的出度和出边的邻接关系来进行的.假如有多于一个点的入度为0,那么将只能遍历到其中一个点以及往后的内容. 对于链式前向星:链式前向星每添加一条边就会更新head数组,使得head数组存放的总是最新添加的某点的出边,此出边的next总指向head数组之前存放的出边的序号. 我们输入边的顺序为:

poj-1459-最大流dinic+链式前向星

title: poj-1459-最大流dinic+链式前向星 date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM-网络流-最大流 概述 这道是一道网络流里最大流的板子题,,, 暑期集训网络流草草水过,,连基本的算法都不知道有哪些,,,更别提怎么实现了,,,只知道网络流的大致的概念,, 今天花了一天的时间重新学习了一波,,,本以为这东西很简单,,,没想到不仅算法的实现一大堆的东西,,就连题目都有时候看不懂,,,,感受就是网络流的题不仅算法实

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

图的存储结构:邻接矩阵(邻接表)&amp;链式前向星

[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组graph[ ][ ]来记录图中点a与点b之间是否连通,初始化为0(或者-1之类的看情况):如果图中有可忽略的重边(如 只需重边中的最小边或最大边),则保存需要的那条边的边权,但如果有无法忽略的重边,就一定不要用邻接矩阵. int graph[MAXN][MAXN]; void graphInit() { me

HDU3342 Legal or Not【拓扑排序】【链式前向星】

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4633    Accepted Submission(s): 2115 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is