#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
using namespace std;
int n,x,p;
vector<pair<int,ll>> v[1000005];
vector<int> com[1000005];
ll ans,l,dis[1000005];
bitset<1000005> ok;
priority_queue<pair<ll,int>> q;
void dfs(int x){
if(ok[x]) return;
ok[x]=1;
com[p].push_back(x);
for(pair<int,ll> i:v[x]){
dfs(i.ff);
}
}
void dijk(){
while(!q.empty()){
ll d=q.top().ff;
int x=q.top().ss;
q.pop();
if(ok[x]) continue;
ok[x]=1;
dis[x]=d;
for(pair<int,ll> i:v[x]){
if(!ok[i.ff]) q.push({d+i.ss,i.ff});
}
}
}
ll solve(int p){
ll mx=0,m;
for(int i:com[p]){
q.push({0,i});
dijk();
m=0;
for(int j:com[p]){
m=max(m,dis[j]);
dis[j]=0;
ok[j]=0;
}
mx=max(mx,m);
}
return mx;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i=1;i<=n;i++){
cin >> x >> l;
v[i].push_back({x,l});
v[x].push_back({i,l});
}
for(int i=1;i<=n;i++){
if(!ok[i]){
p++;
dfs(i);
}
}
ok.reset();
for(int i=1;i<=p;i++) ans+=solve(i);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
48988 KB |
Output is correct |
2 |
Correct |
9 ms |
49108 KB |
Output is correct |
3 |
Correct |
12 ms |
48988 KB |
Output is correct |
4 |
Correct |
8 ms |
49104 KB |
Output is correct |
5 |
Correct |
8 ms |
48984 KB |
Output is correct |
6 |
Correct |
11 ms |
49076 KB |
Output is correct |
7 |
Correct |
8 ms |
48984 KB |
Output is correct |
8 |
Correct |
7 ms |
48968 KB |
Output is correct |
9 |
Correct |
8 ms |
49164 KB |
Output is correct |
10 |
Correct |
8 ms |
48988 KB |
Output is correct |
11 |
Correct |
9 ms |
48988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
49096 KB |
Output is correct |
2 |
Correct |
8 ms |
49496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
49244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2032 ms |
52268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
55256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2009 ms |
63684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2040 ms |
74064 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
247 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
229 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |