当前位置:网站首页>[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
2022-07-27 21:55:00 【Dream and wake up, happy and carefree】
Catalog

Description
Xiao Zhang is a mystery , He likes watching detective novels and detective movies very much . At the same time, he can also play some reasoning Games , In the detective game , Xiao Zhang needs to explore the connection between events . Through a clue , He can pass events A Infer events B. If through an event A Be able to launch events A In itself , Then he can complete the reasoning . Now in order m Bar clue , Please calculate that he can at least form a logical closed loop with the first few clues to complete the reasoning .
Input first line n,m(1 \leq n,m \leq 300000 ) Two Numbers , Indicates the number of events and clues . Next m That's ok , Two numbers per line A,B(1 \leq A,B \leq n ) , Indicates an event A Be able to launch events B.
Output Count one line at a time , It means that at least the first few clues can form a logical closed loop to complete reasoning . If the output cannot be completed -1.
Ideas
Still bfs.
In fact, this kind of question is simple bfs, But the details will change a lot .
Like before 22 Non minimum steps occur .
This way 24 Many more details :
- The logic closed loop does not need to record the beginning and end , As long as there are numbers in the first few that can form a closed loop
- Since it is a closed loop , Then any number can be used as the beginning , So you start from A Found B Sure , You can also get it from B Found A
- How do we determine which one is in the closed loop ? Is it to create an array to store the first few reads , Then read in each number , For every number in the whole array bfs once ? Too tired , absolute TLE. You might as well optimize : Before hypothesis n-1 The number cannot form a closed loop , And with the third n Logarithm forms a closed loop , Note No n Logarithm must be in closed loop , In this way, each time a new number is read in bfs At a time can be .
- According to this idea , For the first time TLE fall 4 individual hhh, If there is nothing wrong with the algorithm , Or you can't live with the same algorithm if others can , It must be that you have a lot of miscellaneous steps , Then start to analyze .
- This question does not require the shortest number of steps , So as long as it can form a logical closed loop , Don't need to use vis Array step , use vis Array requires judgment ,memset, It's a waste of time .
- Find the specific thinking framework in the code
Code
TLE fall 4 Version of
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define SIZE 300010
//int step[SIZE];
int cause[SIZE] = { 0 };
int result[SIZE] = { 0 };
int step;
std::map<int, std::vector<int>> graph;
std::queue<int> q;
bool bfs(int h2, int h1,bool vis[]);
int main(void)
{
// freopen("input.txt", "r", stdin);
int n, m, temp1, temp2;
scanf("%d %d", &n, &m);
for (step = 1; step <= m; step++)
{
bool vis[SIZE] = { false };
scanf("%d %d", &temp1, &temp2);
graph[temp1].push_back(temp2);
if (bfs(temp2, temp1, vis))// Meet the conditions that have appeared before bfs
{
printf("%d\n", step);
return 0;
}
}
// There is no result here
printf("-1\n");
return 0;
}
bool bfs(int h2,int h1,bool vis[])// From the end bfs To the beginning of the first
{
int top = h2;
q.push(h2);
while (!q.empty())
{
// Take out
top = q.front();
q.pop();
// Put it in the neighbor
for (auto x : graph[top])
{
if (x == h1) // Find the logic closed loop
return true;
if (!vis[x])
{
q.push(x);
vis[x] = true;
}
}
}
// Finally empty , Namely bfs Failure
return false;
}Remove miscellaneous steps ac edition
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define SIZE 300010
int step;
std::map<int, std::vector<int>> graph;
std::queue<int> q;
bool bfs(int h2, int h1);
int main(void)
{
freopen("input.txt", "r", stdin);
int n, m, temp1, temp2;
scanf("%d %d", &n, &m);
for (step = 1; step <= m; step++)
{
scanf("%d %d", &temp1, &temp2);
graph[temp1].push_back(temp2);
if (bfs(temp2, temp1))// Meet the conditions that have appeared before bfs
{
printf("%d\n", step);
return 0;
}
}
// There is no result here
printf("-1\n");
return 0;
}
bool bfs(int h2, int h1)// From the end bfs To the beginning of the first
{
int top = h2;
q.push(h2);
while (!q.empty())
{
// Take out
top = q.front();
q.pop();
// Put it in the neighbor
for (auto x : graph[top])
{
if (x == h1) // Find the logic closed loop
return true;
q.push(x);
}
}
// Finally empty , Namely bfs Failure
return false;
}边栏推荐
- Finish learning redis cluster solution at one go
- @Autowired注解与@Resource注解的区别
- 最高7.5Gbps!全球首款5nm 5G基带骁龙X60发布:支持聚合全部主要频段!
- JVM memory model interview summary
- Search, insert and delete of hash table
- Can JVM tuning be done with single core CPU and 1G memory?
- Mysql 数据恢复流程 基于binlog redolog undolog
- 面向3nm及以下工艺,ASML新一代EUV光刻机曝光
- Simple use of enum
- CocoaPods 重装
猜你喜欢

MySQL execution process and order
![[day_4-review, basic concepts of objects and arrays - 1]](/img/57/750f12d5315b682d2aeec81f39dafb.png)
[day_4-review, basic concepts of objects and arrays - 1]

8000 word explanation of OBSA principle and application practice

How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath

为什么服务端程序都需要先 listen 一下

IDEA连接MySQL数据库并执行SQL查询操作

In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))

How to deal with high concurrency deadlock?

微软商店无法下载应用,VS2019无法下载插件问题解决方案
![[2022 Niuke multi School Game 2] k-link with bracket sequence I](/img/95/9d6710bfb7b9282b4a06a5f61a1f08.png)
[2022 Niuke multi School Game 2] k-link with bracket sequence I
随机推荐
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
Log4j vulnerability is still widespread and continues to cause impact
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this
2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
IDEA连接MySQL数据库并执行SQL查询操作
一口气学完 Redis 集群方案
Form of objects in memory & memory allocation mechanism
华为成立全球生态发展部:全力推进HMS全球生态建设
Encapsulate an array into a class
Unit-- read Excel
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
Technical practice behind bloom model: how to refine 176billion parameter model?
QT take out the input box string, lineedit
IDEA常用快捷键及设置方法
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
软件测试面试题:请说出这些测试最好由那些人员完成,测试的是什么?
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!