#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::vector <bool> st;
long long dfs(int node) {
if (st[node]) {
return 0;
}
st[node] = true;
long long r = 0;
for (auto p : g[node]) {
if (!st[p.first]) {
r = std::max(r, dfs(p.first) + p.second);
}
}
st[node] = false;
return r;
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n;
st.resize(n, false);
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));
}
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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
738 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
1620 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2033 ms |
5460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2080 ms |
17100 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2079 ms |
32604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2079 ms |
74904 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
347 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |