# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
655400 | 2022-11-04T09:07:54 Z | ShirAriel | 연결리스트 수사하기 (NOI12_forensic) | C++17 | 4 ms | 1748 KB |
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef long long ll; vector<vector<ll>> branch; vector<bool> visited; ll getlen(ll v) { ll ans = 0; visited[v] = true; for (ll i = 0; i < branch[v].size(); i++) { ll u = branch[v][i]; if (!visited[u]) ans = max(getlen(u) + 1, ans); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; vector<ll> chain(n); for (ll i = 0; i < n; i++) cin >> chain[i]; branch.resize(n); for (ll i = 0; i < n; i++) if (chain[i] != -1) branch[chain[i]].push_back(i); ll b = 0, c = 0; bool circle = false; vector<bool> bigchain(n, false); ll v = chain[0], cnt=0; bigchain[0] = true; while (v!=-1 && !bigchain[v]) { bigchain[v] = true; v = chain[v]; cnt++; } v = chain[0]; visited.resize(n, false); while (v != -1 && !visited[v]) { visited[v] = true; b = max(b, getlen(v)); v = chain[v]; } if (v != -1) circle = true; for (ll i = 0; i < n; i++) if (chain[i] == -1 && !visited[i]) c = max(c, getlen(i) + 2); if (circle&&c == 0) cout << cnt + 1; else cout << cnt + max(c, (circle ? 0 : b)); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Incorrect | 1 ms | 212 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 468 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 1240 KB | Output is correct |
2 | Incorrect | 4 ms | 1748 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |