当前位置:网站首页>12.5 concurrent search + violent DFS - [discovery ring]
12.5 concurrent search + violent DFS - [discovery ring]
2022-06-11 09:37:00 【I made the night white_】
List of articles
Title Description
Xiao Ming's lab has N Computers , Number 1…N. Originally this N There are... Between computers N-1 A data link , Just form a tree network . On a tree network , There is a unique path between any two computers .
But the last time I maintained the network , The administrator's misoperation caused a data link to be added between two computers , So there is a loop in the network . There is no longer only one path between two computers on the loop , This makes the data transmission on these computers appear BUG.
In order to resume normal transmission . Xiao Ming needs to find all the computers on the loop , Can you help him ?
Input description
Output description
Output the number of computers on the loop in descending order , Separated by a space in the middle .
I/o sample
Input :
5
1 2
3 1
2 4
2 5
5 3
Output :
1 2 3 5
The final code
1. c/c++
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+1100;
// Container array
vector<int> vt[N];
int s[N],vis[N],circle[N];
int start,flag,num;
void init_set(){
for(int i=1;i<=N;i++) s[i]=i;
}
int find_set(int x){
// Looking for ancestors
if(x!=s[x]) s[x]=find_set(s[x]); // Path compression
return s[x];
}
void merge_set(int x,int y){
int tmp = x;
x = find_set(x);
y = find_set(y);
if(x!=y) s[y]=s[x];
else start = tmp; // this x and y In the ring , Record a point
}
void dfs(int x,int step)
{
// From the starting point start Set out to find a way back to start The loop of
if(flag) return;
if(x==start) // After a circle, I came back , End of loop
if(vis[x]==1)
{
num = step-1;
flag = 1;
return ;
}
circle[step] = x;
for(int i=0;i<vt[x].size();i++)
{
int y=vt[x][i];
if(!vis[y])
{
vis[y]=1;
dfs(y,step+1);
vis[y]=0;
}
}
}
int main(){
int n;
scanf("%d",&n);
init_set();
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
// Build a two-dimensional container
vt[a].push_back(b);
vt[b].push_back(a);
merge_set(a,b); // Find a point on the ring start
}
flag = 0;
// With start Search again for the starting point , This is the ring
dfs(start,1);
sort(circle+1,circle+1+num);
for(int i=1;i<=num;i++)
printf("%d ",circle[i]);
return 0;
}
Process understanding
边栏推荐
- Machine learning notes - spatial transformer network using tensorflow
- Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
- ERP体系的这些优势,你知道吗?
- P1169 "chessboard making"
- Interview question 17.10 Main elements
- Talk about reading the source code
- Type-C蓝牙音箱单口可充可OTG方案
- Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
- Shandong University project training (IV) -- wechat applet scans web QR code to realize web login
- Fabric.js 动态设置字号大小
猜你喜欢

Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods

Openstack explanation (22) -- neutron plug-in configuration

ESP8266_SmartConfig

A summary of the problem type and method for proving the limit of sequence in postgraduate entrance examination

Revisiting Self-Training for Few-Shot Learning of Language Model,EMNLP2021

Typescript -- preliminary study of variable declaration
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s

Type-C docking station adaptive power supply patent protection case

MySQL:Got a packet bigger than ‘max_ allowed_ packet‘ bytes

Augmented reality experiment IV of Shandong University
随机推荐
Four data-driven behaviors of integral system
Interview question 17.10 Main elements
Flask (II) - route
Technical practice of dolphin dispatching in kubernetes system
OpenCV CEO教你用OAK(四):创建复杂的管道
Install jupyter in the specified environment
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
ESP8266_SmartConfig
Augmented reality experiment IV of Shandong University
Thread theory
Sword finger offer II 041 Average value of sliding window
机器学习笔记 - 深度学习技巧备忘清单
[intelligent development] scheme design and hardware development of sphygmomanometer
【ROS】noedic-moveit安装与UR5模型导入
ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling
What problems can ERP system help enterprises deal with?
Package details
What are the types of garment ERP system in the market?
[scheme development] sphygmomanometer scheme pressure sensor sic160
[scheme development] scheme of infrared thermometer

