当前位置:网站首页>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
边栏推荐
- OpenCV OAK相机对比及介绍
- Talk about reading the source code
- 报错Version mismatch between installed depthai lib and the required one by the scrip.
- Don't use redis list to implement message queue. Stream is designed for queues
- 报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
- Comment l'entreprise planifie - t - elle la mise en oeuvre?
- Device = depthai Device(““, False) TypeError: _init_(): incompatible constructor arguments.
- 服装ERP:企业如何进行施行规划?
- 从企业评价的方历来看ERP软件成功与失利
- 2161. 根据给定数字划分数组
猜你喜欢

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

报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer

【ROS】noedic-moveit安装与UR5模型导入

Openstack explanation (21) -- installation and configuration of neutron components

Redis source code analysis hash object (z\u hash)

Technical practice of dolphin dispatching in kubernetes system

实现边充边OTG的PD芯片GA670-10

报错Version mismatch between installed depthai lib and the required one by the scrip.

Type-C docking station adaptive power supply patent protection case

Machine learning notes - convolutional neural network memo list
随机推荐
OpenCV CEO教你用OAK(四):创建复杂的管道
Set MySQL as externally connectable
Talk about how to customize data desensitization
Comment l'entreprise planifie - t - elle la mise en oeuvre?
Automation operation and maintenance articles collection
document对象
1400. construct K palindrome strings
P4147 "jade toad Palace"
Machine learning notes - convolutional neural network memo list
affair
考研數學 【數列極限證明題】題型方法總結
PD chip ga670-10 for OTG while charging
Package details
Day39 content summary
js中的事件
ERP体系的这些优势,你知道吗?
机器学习笔记 - 使用TensorFlow的Spatial Transformer网络
Flask (III) -- variable rules
Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
Machine learning notes - spatial transformer network using tensorflow

