当前位置:网站首页>[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;
}
边栏推荐
- 诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
- Principle analysis of security rememberme
- Clean up system cache and free memory under Linux
- OpenAPI generator: simplify the restful API development process
- 每日一题:1175.质数排列
- mac(macos Monterey12.2 m1) 个人使用php开发
- Numpy array calculation
- Bridge of undirected graph
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
- Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
猜你喜欢
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Unity skframework framework (XIX), POI points of interest / information points
EasyDSS点播服务分享时间出错如何修改?
题解:《压缩技术》(原版、续集版)
Web基础
PR usage skills, how to use PR to watermark?
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
What are eNB, EPC and PGW?
Essential for operation and maintenance - Elk log analysis system
OpenFOAM:lduMatrix&lduAddressing
随机推荐
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
三谈exception——错误处理
【云原生数据库】遇到慢SQL该怎么办(上)?
Find love for speed in F1 delta time Grand Prix
Winter vacation daily question - lucky numbers in the matrix
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
numpy数组计算
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Jerry's watch reads the alarm clock [chapter]
Android kotlin material design technology points
Node.js通过ODBC访问PostgreSQL数据库
Skillfully use SSH to get through the Internet restrictions
Jerry's watch modifies the alarm clock [chapter]
Solve "sub number integer", "jump happily", "turn on the light"
为什么switch 的default后面要跟break?
嵌入式软件开发
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
Explanation of 34 common terms on the Internet
Unity skframework framework (XVI), package manager development kit Manager
Embedded software development