当前位置:网站首页>[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;
}边栏推荐
- 递归/回溯刷题(上)
- 简单手动实现Map
- Can JVM tuning be done with single core CPU and 1G memory?
- University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
- Array expansion, sorting, nested statement application
- 对象在内存中存在形式&内存分配机制
- V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
- Huawei establishes global ecological development department: fully promote HMS global ecological construction
- @Component可以和@Bean 用在同一个类上吗?
- How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath
猜你喜欢

高并发遇到死锁了,如何搞?
![[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

Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design

Station B collapsed. What did the developer responsible for the repair do that night?
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool

Small change project (two versions) with detailed ideas

微软商店无法下载应用,VS2019无法下载插件问题解决方案

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

day 1 - day 4

@Component可以和@Bean 用在同一个类上吗?
随机推荐
Simple manual implementation of map
Cocoapods reload
Commercial delay of self-developed 5g chip? Apple iPhone will adopt Qualcomm 5g chip in the next four years
软件测试面试题:请说出这些测试最好由那些人员完成,测试的是什么?
Software testing interview question: how many strategies are there for system testing?
Software testing interview question: what is regression testing?
为什么要使用MQ消息中间件?这几个问题必须拿下
Software testing interview question: what project documents need to be referred to in designing the system test plan?
Can JVM tuning be done with single core CPU and 1G memory?
2021-11-05 understanding of class variables and class methods
How can anyone ask how MySQL archives data?
In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))
Log4j 漏洞仍普遍存在?
枚举Enum的简单使用
Common shortcut keys and setting methods of idea
Talk about MySQL transaction two-phase commit
简单手动实现Map
软件测试面试题:软件测试项目从什么时候开始?为什么?
How to realize a good knowledge management system?
@Autowired注解与@Resource注解的区别