当前位置:网站首页>AcWing 1977. Information relay (base ring tree, parallel search set)
AcWing 1977. Information relay (base ring tree, parallel search set)
2022-06-13 08:12:00 【Eurya song】
【 Title Description 】
Farmer John has N N N A cow , Number 1 ∼ N 1\sim N 1∼N.
A home-made telephone made of cans and ropes , They will communicate with each other when John is not paying attention .
Each cow can forward messages to another cow at most :
For cows i i i, It will forward any information it receives to the cow F i F_i Fi( F i F_i Fi And i i i It must be different ).
If F i F_i Fi by 0 0 0, Then cow i i i Do not forward messages .
Unfortunately , Cows realize that news from some cows may eventually fall into a cycle , Constantly being forwarded .
Please make sure how many cows out of all cows send messages that won't fall into a cycle forever .
【 Input format 】
The first line contains integers N N N.
Next N N N That's ok , Each line contains an integer F i F_i Fi.
【 Output format 】
Output the number of cows that send messages that will not fall into a loop .
【 Data range 】
1 ≤ N ≤ 1000 1≤N≤1000 1≤N≤1000
0 ≤ F i ≤ N 0≤F_i≤N 0≤Fi≤N
【 sample input 】
5
0
4
1
5
4
【 sample output 】
2
【 Sample explanation 】
cow 1 1 1 Don't send any messages , So it won't fall into a cycle .
cow 3 3 3 Send a message to the cow 1 1 1, So it won't fall into a cycle .
The messages sent by the other three cows will fall into a cycle .
【 analysis 】
Because each cow can only forward one message to another cow at most , Therefore, the maximum output of each point is 1 1 1. So the graph is composed of several base ring trees ( Every point has a degree ) And some ordinary trees ( The degree of 0 0 0 The point of is the root node ), As shown in the figure below :
One of the solutions to this problem is to find the number of points that can go to a certain ring c n t cnt cnt, Total points minus c n t cnt cnt Is the answer . For judging whether each point can go to the ring , have access to set
, Go straight from the current point , Save all the points passed into set
in , If you reach the exit degree of 0 0 0 The point of the note will not go to the ring , If you get there, you are already set
In the point , It shows that a ring is formed . The time complexity of this solution is O ( n 2 ) O(n^2) O(n2).
Another solution is to search the set by union , For a point i i i, If it points to another point j j j, So let's connect these two points , Due to the direction limitation , So let p r e [ f i n d ( i ) ] = f i n d ( j ) pre[find(i)]=find(j) pre[find(i)]=find(j), In this way, if it is an ordinary tree, the root node of this set is with an outgoing degree of 0 0 0 The point of . Finally, we traverse each point , If the outgoing degree of the root node is 0 0 0, It shows that this point is in the ordinary tree , That is, they will not go to the ring , Add one... To the answer .
【 O ( n 2 ) O(n^2) O(n2) Code 】
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int N = 1010;
int to[N];
int n, res;//res Indicates the number of points that can reach the ring
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> to[i];
for (int i = 1; i <= n; i++)
{
unordered_set<int> st;
for (int j = i; to[j]; j = to[j])
{
if (st.count(j)) {
res++; break; }
st.insert(j);
}
}
cout << n - res << endl;
return 0;
}
【 O ( n ) O(n) O(n) Code 】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int pre[N], to[N];
int n, res;
int find(int k)
{
if (pre[k] == k) return k;
return pre[k] = find(pre[k]);
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) pre[i] = i;
for (int i = 1; i <= n; i++)
{
cin >> to[i];
if (to[i]) pre[find(i)] = find(to[i]);
}
for (int i = 1; i <= n; i++)
if (!to[find(i)]) res++;
cout << res << endl;
return 0;
}
边栏推荐
- Remote office solution under epidemic situation
- [complete information static game characteristics of Nash equilibrium]
- 2022 simulated examination question bank and online simulated examination of hoisting machinery command examination questions
- Dfinity (ICP) identity authentication and ledger quick start-3
- 判断一个字符串是否由另外一个字符串旋转而来
- 微服务项目搭建二:数据库设计
- 2022 electrician (elementary) examination questions and simulation examination
- 实践出真知--你的字节对齐和堆栈认知可能是错误的
- 1. fabric2.2 comprehensive learning - Preface
- Effective Go - The Go Programming Language
猜你喜欢
2022起重机械指挥考试题模拟考试题库及在线模拟考试
【clickhouse专栏】基础数据类型说明
Examination question bank and simulation examination for special operation certificate of safety management personnel of hazardous chemical business units in 2022
SolidWorks修改工程图中文字字体的方法
CCNP_ BT static routing
Dfinity (ICP) identity authentication and ledger quick start-3
JMeter UDP pressure measurement
MySQL table partitioning
BD新标签页(BdTab)插件如何登入?
How does the BD new tab plug-in log in?
随机推荐
微服务项目搭建二:数据库设计
Overall process analysis of account book operation in fabric0.6
Dfinity (ICP) identity authentication and ledger quick start-3
Cosmos star application case
Rust writes near smart contract
安装CUDA+CUSP环境,并创建第一个HelloWord入门工程
P7712 [Ynoi2077] hlcpq
OpenHarmony笔记-----------(一)
Win10系统如何修改桌面路径
MySQL table partitioning
Basic operation of dfinity (ICP) development-4
Local shooting range 2- file upload vulnerability (III) - Network Security
JMeter common commands
Clickhouse column basic data type description
Dfinity (ICP) basic development tutorial-5
[deep learning]: introduction to pytorch to project practice (XII) convolutional neural network: padding and stride
Shell script Basics
Go 接口实现原理【高阶篇】: type _interface struct
Founder of Starbucks: no longer open "public toilets" to non store consumers for safety reasons
SFTP login and download file script