From 9c6b3330b58fc4544d32247b9f6e64bdf64c4ed6 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 2 Oct 2024 11:21:23 +0800 Subject: [PATCH] update --- CMakeLists.txt | 1 + P2661/video.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 P2661/video.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e500f5..667bf05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,5 +19,6 @@ add_executable(P1031 ${CMAKE_CURRENT_LIST_DIR}/P1031/main.cpp) add_executable(P1031_pro ${CMAKE_CURRENT_LIST_DIR}/P1031/pro.cpp) add_executable(P2661 ${CMAKE_CURRENT_LIST_DIR}/P2661/main.cpp) +add_executable(P2661_video ${CMAKE_CURRENT_LIST_DIR}/P2661/video.cpp) add_executable(P2615 ${CMAKE_CURRENT_LIST_DIR}/P2615/P2615.cpp) \ No newline at end of file diff --git a/P2661/video.cpp b/P2661/video.cpp new file mode 100644 index 0000000..5beb6ac --- /dev/null +++ b/P2661/video.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include + +using std::cin,std::cout; +constexpr const auto range = std::ranges::views::iota; + +const int MAX_N = 2e5+5; +int n; +int next[MAX_N]; +int in_degree[MAX_N]; +std::queue q; +bool vis[MAX_N]; +int ans {std::numeric_limits::max()}; + +int main(){ + std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(0); + + cin>>n; + for(const int i:range(1,n+1)){ + cin>>next[i]; + in_degree[next[i]]++; + } + + const auto push_vis = [](const int n){ + q.push(n); + vis[n]=true; + }; + for(const int i:range(1,n+1)){ + if(in_degree[i]==0){ + push_vis(i); + } + } + + while(q.empty()==false){ + int front = q.front(); + q.pop(); + in_degree[next[front]]--; + if(in_degree[next[front]]==0){ + push_vis(next[front]); + } + } + + for(int i:range(1,n+1)){ + if(vis[i])continue; + const int start {i}; + int now {start}; + int ring_size {0}; + do{ + vis[now]=true; + ring_size++; + now = next[now]; + }while(now!=start); + ans = std::min(ans,ring_size); + } + + cout<