update
This commit is contained in:
parent
5df1af7505
commit
9c6b3330b5
@ -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)
|
61
P2661/video.cpp
Normal file
61
P2661/video.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <ranges>
|
||||
#include <queue>
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
|
||||
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<int> q;
|
||||
bool vis[MAX_N];
|
||||
int ans {std::numeric_limits<int>::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<<ans<<"\n";
|
||||
}
|
Loading…
Reference in New Issue
Block a user