This commit is contained in:
Zengtudor 2024-08-30 18:04:27 +08:00
parent 0caa758c70
commit c1dc73f772
3 changed files with 98 additions and 1 deletions

50
20240830/P2661/P2661.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <algorithm>
#include <iostream>
#include <queue>
const int MAX_N{int(2e5+5)};
int in_degree[MAX_N]{},n,next[MAX_N];
std::queue<int> q;
bool vis[MAX_N];
int main(){
std::cin>>n;
for(int i=1;i<=n;i++){
int input;
std::cin>>input;
in_degree[input]++;
next[i]=input;
}
for(int i=1;i<=n;i++){
if(in_degree[i]==0){
q.push(i);
}
}
while(q.empty()==false){
int top = q.front();
in_degree[next[top]]--;
q.pop();
if(in_degree[next[top]]==0){
q.push(next[top]);
}
}
int ans{0};
for(int i=1;i<=n;i++){
if(in_degree[i]!=0 && vis[i]==false){
int begin{i},s{1};
int now{next[begin]};
vis[begin]=true;
while(now!=begin){
vis[now]=true;
s++;
now=next[now];
}
ans=std::max(ans,s);
}
}
std::cout<<ans<<"\n";
}

40
20240830/P4089/P4089.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <queue>
const int MAX_N{int(1e5+5)};
int in_degree[MAX_N]{},n,next[MAX_N];
std::queue<int> q;
void find(int num){
}
int main(){
std::cin>>n;
for(int i=1;i<=n;i++){
int input;
std::cin>>input;
in_degree[input]++;
next[i]=input;
}
int zero_in_degree_dir = -1;
int cnt{};
for(int i=1;i<=n;i++){
if(in_degree[i]==0){
q.push(i);
cnt++;
}
}
while(q.empty()==false){
int top = q.front();
in_degree[next[top]]--;
q.pop();
if(in_degree[next[top]]==0){
q.push(next[top]);
cnt++;
}
}
std::cout<<(n-cnt)<<"\n";
}

View File

@ -7,6 +7,7 @@ if is_mode("release") then
add_cxxflags("-march=native") add_cxxflags("-march=native")
end end
set_languages("c++17") set_languages("c++17")
set_warnings("all")
target("P1158") target("P1158")
set_rundir("20240821/P1158") set_rundir("20240821/P1158")
@ -28,4 +29,10 @@ target("P2615")
add_files("20240828/P2615/P2615.cpp") add_files("20240828/P2615/P2615.cpp")
target("B4015") target("B4015")
add_files("20240829/B4015/B4015.cpp") add_files("20240829/B4015/B4015.cpp")
target("P4089")
add_files("20240830/P4089/P4089.cpp")
target("P2661")
add_files("20240830/P2661/P2661.cpp")