当前位置:网站首页>[Blue Bridge Cup] children's worship circle
[Blue Bridge Cup] children's worship circle
2022-07-02 13:38:00 【Do you like Japanese girls?】
Children worship circle
Class N N N Two children , Everyone has one of their favorite children ( You can also be yourself ).
In a game , We need children to sit in a circle , Every child has his favorite child on his right side .
How many people can satisfy the circle ?
The children's number is 1 , 2 , 3 , ⋯ N 1,2,3,\cdots N 1,2,3,⋯N.
Input description
Enter the first line , An integer N ( 3 < N < 1 0 5 ) N(3<N<10^5) N(3<N<105)
Next line N N N It's an integer , Separated by spaces .
Output description
An integer is required , Represents the number of people in the largest circle that satisfies the condition .
I/o sample
Example
Input
9
3 4 2 5 3 8 4 6 9
Output
4
Sample explanation
As shown in the figure below , Worship relations are indicated by arrows , Red means not in the circle .
obviously , The biggest circle is [ 2 4 5 3 ] [2\ 4\ 5\ 3] [2 4 5 3] The circle formed .
Ideas
- The problem can be transformed into the problem of finding the maximum number of sides of a cycle in a directed graph
- Each child is regarded as a node , The worship relationship is regarded as a directed side , So if there is N N N Two children , Then there will be N N N Nodes , N N N The strip has a directed edge
- For each node , It can be observed that : There is and only one edge , If you want to form a circle , There must be an entry edge , Then we can see : A node without an edge must not be a node in a circle
- At the beginning, first remove the incoming edge as 0 0 0 The node of , Don't consider , So easy to know , The remaining nodes must form several circles
- d f s dfs dfs: There are two parameters , One is the i i i Two children , One is the i i i A child worshipped by children
- If these two children are the same , Then it must have formed a circle , Then this condition can be used as a recursive exit
- If these two children are different , Continued to d f s dfs dfs , Until these two children are the same ( The initialized data has been guaranteed )
- Set the maximum number of sides of a circle , If a d f s dfs dfs The number of sides found is greater than this maximum , Update
- Finally, output the maximum value
The code is as follows
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;// Children array , The first i Children worship the first a[i] Two children
vector<bool> vis;// Have you been visited
vector<bool> flag;// Whether there is an edge
int max_circle;// The maximum number of edges
int circle;// Temporary variable , Reduce recursion overhead
int n;// The number of children
void init() {
scanf("%d", &n);
a.resize(n + 1);
vis.resize(n + 1, false);
flag.resize(n + 1, false);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
flag.at(a[i]) = true;// True means there is an edge , Otherwise no
}
}
void dfs(int start_id, int next_id) {
if (next_id == start_id) {
max_circle = max(circle, max_circle);
return;
}
vis.at(next_id) = true;
circle++;
dfs(start_id, a.at(next_id));
}
void solve() {
for (int i = 1; i <= n; i++) {
if (flag.at(i) && !vis.at(i)) {
vis.at(i) = true;
circle = 1;
dfs(i, a.at(i));
}
}
printf("%d\n", max_circle);
}
int main(void) {
init();
solve();
return 0;
}
边栏推荐
- Numpy array calculation
- EasyDSS点播服务分享时间出错如何修改?
- mac(macos Monterey12.2 m1) 个人使用php开发
- Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
- leetcode621. 任务调度器
- Nohup command
- Performance optimization of memory function
- Unity SKFramework框架(二十)、VFX Lab 特效库
- Unity skframework framework (XII), score scoring module
- Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
猜你喜欢

记忆函数的性能优化

A better database client management tool than Navicat

最近公共祖先LCA的三种求法

Performance optimization of memory function

Skillfully use SSH to get through the Internet restrictions

OpenFOAM:lduMatrix&lduAddressing

研究表明“气味相投”更易成为朋友

题解:《压缩技术》(原版、续集版)

Answer: can the audio be set to on by default during easydss video on demand?

Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
随机推荐
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
OpenApi-Generator:简化RESTful API开发流程
Find love for speed in F1 delta time Grand Prix
Unity SKFramework框架(十四)、Extension 扩展函数
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
De4000h storage installation configuration
[youcans' image processing learning course] general contents
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Memory management 01 - link script
Jerry's watch ringtone audition [article]
SSL证书的分类有哪些?如何选择合适的SSL证书?
TVOC, VOC, VOCs gas detection + Solution
Security RememberMe原理分析
Unity skframework framework (XIV), extension extension function
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
【OpenGL】笔记二十九、高级光照(镜面高光)
Three talking about exception -- error handling
What are eNB, EPC and PGW?
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Gee learning notes 2