#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mx = 0, bes = 0;
vector<vector<array<ll,2>>> gr;
vector<int> vis;
void dfs(int x, set<int> &s, ll dis){
vis[x] = 1;
s.insert(x);
if(dis > mx){
mx = dis;
bes = x;
}
for(auto y : gr[x]){
if(!s.count(y[0]))dfs(y[0],s,dis+y[1]);
}
s.erase(x);
}
int main()
{
int n; cin >> n;
gr.resize(n);
vis.resize(n,0);
for(int i = 0; i < n; ++i){
ll a, b; cin >> a >> b;
a--;
gr[i].push_back({a,b});
gr[a].push_back({i,b});
}
ll ans = 0;
for(int i = 0; i < n; ++i){
if(!vis[i]){
mx = 0;
set<int> s;
dfs(i,s,0);
dfs(bes,s,0);
ans+=mx;
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
1960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
7108 KB |
Output is correct |
2 |
Incorrect |
140 ms |
11324 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
20492 KB |
Output is correct |
2 |
Correct |
416 ms |
28140 KB |
Output is correct |
3 |
Incorrect |
520 ms |
40612 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
506 ms |
38492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
765 ms |
90280 KB |
Output is correct |
2 |
Runtime error |
986 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
958 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |