#include <bits/stdc++.h>
int n;
std::vector <std::vector <std::pair <int, long long>>> g;
std::vector <int> cc;
void dfs(int node, int c, std::vector <bool>& vis) {
if (vis[node]) {
return;
}
vis[node] = true;
cc[node] = c;
for (auto p : g[node]) {
dfs(p.first, c, vis);
}
}
std::set <int> st;
long long dfs(int node, int par, int orig) {
if (st.count(node)) {
return 0;
}
if (par != -1 && node == orig) {
return 0;
}
st.insert(node);
long long r = 0;
for (auto p : g[node]) {
if (p.first != par && !st.count(p.first)) {
r = std::max(r, dfs(p.first, node, orig) + p.second);
}
}
st.erase(node);
return r;
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n;
g.resize(n);
for (int i = 0; i < n; i++) {
int u, w; std::cin >> u >> w; u--;
g[u].emplace_back(i, w),
g[i].emplace_back(u, w);
}
cc.resize(n, -1);
std::vector <bool> vis(n, false);
int cnt = 0;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i, cnt++, vis);
}
}
std::vector <long long> cans(cnt, 0);
for (int i = 0; i < n; i++) {
cans[cc[i]] = std::max(cans[cc[i]], dfs(i, -1, i));
}
long long ans = 0;
for (long long x : cans) {
ans += x;
}
std::cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
87 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
514 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Execution timed out |
2076 ms |
840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2096 ms |
1748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2085 ms |
6740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2072 ms |
18412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2090 ms |
33484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2088 ms |
74768 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
415 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |