This commit is contained in:
Zengtudor 2024-10-02 00:00:28 +08:00
parent c2dbaafa24
commit 5df1af7505
2 changed files with 60 additions and 32 deletions

View File

@ -1,10 +1,51 @@
#include <iostream> #include <iostream>
#include <istream> #include <istream>
#include <ranges>
using std::cin,std::cout; using std::cin,std::cout;
static constexpr const auto range = std::ranges::views::iota;
const int MAX_N = 39+5;
int n;
int map[MAX_N][MAX_N];
int x,y,now{1};
int main(){ int main(){
std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
std::cin>>n;
x=1,y=n/2+1;
map[x][y]=now++;
/*
1. $(K-1)$ $K$ $(K-1)$
2. $(K-1)$ $K$ $(K-1)$
3. $(K-1)$ $K$ $(K-1)$
4. $(K-1)$ $(K-1)$ $K$ $(K-1)$ $K$ $(K-1)$
*/
while(now<=n*n){
if(x==1 && y!=n){
x=n,y++;
}else if(y==n && x!=1){
y=1,x--;
}else if(x==1 && y==n){
x++;
}else if(x!=1 && y!=n){
if(map[x-1][y+1]==0){
x--;
y++;
}else{
x++;
}
}
map[x][y]=now++;
}
for(int i:range(1,n+1)){
for(int j:range(1,n+1)){
std::cout<<map[i][j]<<" ";
}
std::cout<<"\n";
}
} }

View File

@ -2,42 +2,25 @@
#include <iostream> #include <iostream>
#include <istream> #include <istream>
#include <queue> #include <queue>
#include <vector> #include <limits>
using std::cin,std::cout; using std::cin,std::cout;
const int MAX_N {(int)2e5+5}; const int MAX_N {(int)2e5+5};
int n; int n;
std::vector<int> next[MAX_N]; int next[MAX_N];
int in_degree[MAX_N]; int in_degree[MAX_N];
bool vis[MAX_N]; bool vis[MAX_N];
std::queue<int> q; std::queue<int> q;
int ans; int ans {std::numeric_limits<int>::max()};
int input;
int dfs(const int start, const int now, int depth){
depth++;
vis[now] = true;
int max_depth{ depth };
for (int i : next[now]) {
if (i == start) {
max_depth = std::max(max_depth, depth);
}
else {
max_depth = std::max(dfs(start, i, depth), max_depth);
}
}
return max_depth;
};
int main(){ int main(){
std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); std::iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
std::cin>>n; std::cin>>n;
for(int i=1;i<=n;i++){ for(int i=1;i<=n;i++){
cin>>input; cin>>next[i];
in_degree[input]++; in_degree[next[i]]++;
next[i].push_back(input);
} }
const auto push_vis = [](int n){ const auto push_vis = [](int n){
q.push(n); q.push(n);
@ -51,17 +34,21 @@ int main(){
while(q.empty()==false){ while(q.empty()==false){
int front = q.front(); int front = q.front();
q.pop(); q.pop();
for(int i:next[front]){ if(--in_degree[next[front]]<=0){
if(--in_degree[i]<=0){ push_vis(next[front]);
push_vis(i);
} }
} }
}
for(int i=1;i<=n;i++){ for(int i=1;i<=n;i++){
if(vis[i])continue; if(vis[i])continue;
ans=std::max(ans,dfs(i,i,0)); int loop_size {0};
int start {i};
int now {start};
do{
vis[now]=true;
now=next[now];
loop_size++;
}while(now!=start);
ans=std::min(ans,loop_size);
} }
cout<<ans<<"\n"; std::cout<<ans<<"\n";
} }