#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=1e6+5;
int n;
pair<int,int> nxt[N];
int deg[N];
queue<int> q;
vector<pair<int,int>> adj[N];
ll dp[N];
ll dia,ans;
template<class T>
struct Vector{
T dat[N];
int sz=0;
void clear(){
sz=0;
}
void push_back(T val){
dat[sz]=val;
sz++;
}
T* begin(){
return dat;
}
T* end(){
return dat+sz;
}
};
Vector<pair<int,ll>> comp;
template<class T>
struct Deque{
T dat[N*2];
int l=0,r=0;
bool empty(){
return l>r;
}
void clear(){
l=0,r=-1;
}
void push_back(T val){
dat[++r]=val;
}
void pop_front(){
l++;
}
void pop_back(){
r--;
}
T front(){
return dat[l];
}
T back(){
return dat[r];
}
};
Deque<pair<ll,int>> dq;
void dfs(int u){
ll mx=0,mx2=0;
for(auto [v,w]:adj[u]){
dfs(v);
mx2=max(mx2,dp[v]+w);
if(mx<mx2)swap(mx,mx2);
}
dp[u]=mx;
dia=max(dia,mx+mx2);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for(int i=1;i<=n;i++){
auto &[v,w]=nxt[i];
cin >> v >> w;
deg[v]++;
}
for(int i=1;i<=n;i++)if(!deg[i])q.emplace(i);
while(!q.empty()){
int u=q.front();
q.pop();
auto [v,w]=nxt[u];
adj[v].emplace_back(u,w);
if(--deg[v]==0)q.emplace(v);
}
for(int i=1;i<=n;i++)if(deg[i]){
comp.clear();
ll len=0;
for(int u=i;deg[u];){
comp.push_back({u,len});
deg[u]=0;
auto [v,w]=nxt[u];
len+=w;
u=v;
}
dia=0;
dq.clear();
for(auto [u,w]:comp){
dfs(u);
ll val=dp[u]-w;
while(!dq.empty()&&dq.back().first<val)dq.pop_back();
dq.push_back({val,u});
}
for(auto [u,w]:comp){
if(dq.front().second==u)dq.pop_front();
dia=max(dia,w+len+dp[u]+dq.front().first);
ll val=dp[u]-w-len;
while(!dq.empty()&&dq.back().first<val)dq.pop_back();
dq.push_back({val,u});
}
ans+=dia;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23964 KB |
Output is correct |
4 |
Correct |
11 ms |
24152 KB |
Output is correct |
5 |
Correct |
10 ms |
23740 KB |
Output is correct |
6 |
Correct |
12 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23772 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23940 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
10 ms |
23984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
11 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24664 KB |
Output is correct |
2 |
Correct |
19 ms |
25944 KB |
Output is correct |
3 |
Correct |
16 ms |
24924 KB |
Output is correct |
4 |
Correct |
13 ms |
24360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
26964 KB |
Output is correct |
2 |
Correct |
26 ms |
29008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
34532 KB |
Output is correct |
2 |
Correct |
46 ms |
35152 KB |
Output is correct |
3 |
Correct |
57 ms |
42516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
43348 KB |
Output is correct |
2 |
Correct |
125 ms |
50816 KB |
Output is correct |
3 |
Correct |
88 ms |
51468 KB |
Output is correct |
4 |
Correct |
126 ms |
71504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
66700 KB |
Output is correct |
2 |
Correct |
274 ms |
86752 KB |
Output is correct |
3 |
Correct |
148 ms |
62800 KB |
Output is correct |
4 |
Correct |
189 ms |
78792 KB |
Output is correct |
5 |
Correct |
183 ms |
77352 KB |
Output is correct |
6 |
Correct |
485 ms |
78296 KB |
Output is correct |
7 |
Correct |
183 ms |
98896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
220 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |