# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1123248 | kh0i | 연결리스트 수사하기 (NOI12_forensic) | C++20 | 2 ms | 840 KiB |
#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 20003;
int n, a[N], cnt;
bool vis[N], loop;
int in[N], f[N];
void dfs(int u){
vis[u] = 1;
f[u] = 0;
int v = a[u];
++cnt;
if(v == -1)
return;
if(vis[v]){
loop = 1;
return;
}
dfs(v);
}
int find_path(int u){
if(u == -1)
return 0;
int v = a[u];
if((loop and vis[v]) or v == 0)
return -1e9;
if(f[u] != -1)
return f[u];
if(in[u] == 1)
return -1e9;
in[u] = 1;
f[u] = find_path(v) + 1;
in[u] = 2;
return f[u];
}
void solve(){
cin >> n;
memset(f, -1, sizeof(f));
for(int i = 0; i < n; ++i)
cin >> a[i];
dfs(0);
int mx = 0;
for(int i = 0; i < n; ++i)
if(!vis[i])
mx = max(mx, find_path(i));
cout << cnt + mx;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |