当前位置:网站首页>[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;
}边栏推荐
- 关系型数据库的设计思想,20张图给你看的明明白白
- Can JVM tuning be done with single core CPU and 1G memory?
- 怎么还有人问 MySQL 是如何归档数据的呢?
- 软件测试面试题:什么是回归测试?
- 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?
- Import word document pictures blocking and non blocking IO operations
- 首发展锐5G芯片!纯国产5G手机海信F50曝光:搭载虎贲T710+春藤510
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
- Regular expression exercise
- 紫光展锐:2020年将有数十款基于春藤510的5G终端商用
猜你喜欢

day 1 - day 4

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

MySQL执行过程及执行顺序

LinkedList underlying source code

CBAM learning notes

为什么要使用MQ消息中间件?这几个问题必须拿下

The design idea of relational database is obvious to you in 20 pictures

8000字讲透OBSA原理与应用实践

怎么还有人问 MySQL 是如何归档数据的呢?

In addition to "adding machines", in fact, your micro service can be optimized like this
随机推荐
软件测试面试题:系统测试的策略有多少种?
Cocoapods reload
How to deal with high concurrency deadlock?
单核CPU, 1G内存,也能做JVM调优吗?
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
对L1正则化和L2正则化的理解[通俗易懂]
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
Software test interview question: please say who is the best person to complete these tests, and what is the test?
Finish learning redis cluster solution at one go
Understanding of L1 regularization and L2 regularization [easy to understand]
Common shortcut keys and setting methods of idea
Software testing interview question: how many strategies are there for system testing?
如何实现一个好的知识管理系统?
Simple use of enum
Software testing interview question: what project documents need to be referred to in designing the system test plan?
B站崩了,如果我们是那晚负责修复的开发人员
The US Department of justice added 16 new charges against Huawei, including stealing trade secrets