当前位置:网站首页>pta补坑简单图论
pta补坑简单图论
2022-08-08 05:42:00 【一条小小yu】
记忆化搜索可以避免超时,还有就是好好读题。
下图转自“英式没品笑话百科”的新浪微博 —— 所以无论有没有遇到难题,其实都不用担心。

博主将这种逻辑推演称为“逻辑自洽”,即从某个命题出发的所有推理路径都会将结论引导到同一个最终命题(开玩笑的,千万别以为这是真正的逻辑自洽的定义……)。现给定一个更为复杂的逻辑推理图,本题就请你检查从一个给定命题到另一个命题的推理是否是“逻辑自洽”的,以及存在多少种不同的推理路径。例如上图,从“你遇到难题了吗?”到“那就别担心了”就是一种“逻辑自洽”的推理,一共有 3 条不同的推理路径。
输入格式:
输入首先在一行中给出两个正整数 N(1<N≤500)和 M,分别为命题个数和推理个数。这里我们假设命题从 1 到 N 编号。
接下来 M 行,每行给出一对命题之间的推理关系,即两个命题的编号 S1 S2,表示可以从 S1 推出 S2。题目保证任意两命题之间只存在最多一种推理关系,且任一命题不能循环自证(即从该命题出发推出该命题自己)。
最后一行给出待检验的两个命题的编号 A B。
输出格式:
在一行中首先输出从 A 到 B 有多少种不同的推理路径,然后输出 Yes 如果推理是“逻辑自洽”的,或 No 如果不是。
题目保证输出数据不超过 109。
输入样例 1:
7 8
7 6
7 4
6 5
4 1
5 2
5 3
2 1
3 1
7 1
输出样例 1:
3 Yes
输入样例 2:
7 8
7 6
7 4
6 5
4 1
5 2
5 3
6 1
3 1
7 1
输出样例 2:
3 No
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N=510;
int a[N][N];
int path[N];
bool vis[N];
int n,m;
int dfs(int s)
{
vis[s] = 1;
if(path[s])
{
return path[s];
}
for(int i=1; i<=n;i++)
{
if(a[s][i])
{
path[s]+=dfs(i);
}
}
return path[s];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int s1,s2;
cin>>s1>>s2;
a[s1][s2]=1;
}
int start,finish;
cin>>start>>finish;
path[finish]=1;
int sum=dfs(start);
cout<<sum<<" ";
int f=0;
for(int i=1;i<=n;i++)
{
if(vis[i]&&!path[i]) //如果该命题访问过但是到不了要求的命题
{
f=1;
break;
}
}
if(f==0)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
使用 Zap 和 W3af 进行 Web 应用程序漏洞评估
神经网络一般训练多少次,神经网络训练时间过长
Efficient and beautiful scrolling component Slivers of Flutter tutorial (tutorial includes source code)
主脑提示( Master-Mind Hints )
Entering the world of audio and video - RGB and YUV formats
postman---postman parameterization
stack-queue
KDD'22 Recommendation System Papers (24 Research & 36 Application Papers)
轮播文字! QPainter
自动化工具
Database sub-database sub-table, when?How to divide?
How to batch import files and rename them all to the same file name
nonebot插件:说话的艺术
C language - score and loop statement
APISIX Ingress v1.5-rc1 发布
Week 8 Generative Adversarial Networks
Use of Filter
【matlab】matlab中变量赋值函数deal
The difference between CHAR_LENGTH() and LENGTH() in MySQL
基本工具-NETCAT(telnet-banner、传输文本信息)









