#include<bits/stdc++.h>
using namespace std;
ifstream fin("susss.txt");
ofstream fout("res.txt");
#define forr(i, a, b) for(int i = a; i < b; i++)
#define pb push_back
#define vt vector
#define int long long
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define sz(v) (int)v.size()
const int mxn = 1e6 + 5, inf = 1e9, mod = 1e9 + 7, mxm = 1e5 + 5;
int n;
vt<pii>adj[mxn + 1];
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rr(ll l, ll r){
return(uniform_int_distribution<ll>(l, r)(rng));
}
bool vis[mxn + 1], in[mxn + 1];
ll mxd[mxn + 1], cost[mxn + 1];
int to[mxn + 1];
ll mx, id;
vt<int>comp;
void dfs(int s, int pre, int root, ll dep){
mxd[root] = max(mxd[root], dep);
for(int ii = 0; ii < sz(adj[s]); ii++){
int i = adj[s][ii].first; ll w = adj[s][ii].second;
if(i != pre && !in[i])dfs(i, s, root, dep + w);
}
}
void dfs2(int s, int pre, int root, ll dep){
if(dep > mx){
mx = dep; id = s;
}
for(int ii = 0; ii < sz(adj[s]); ii++){
int i = adj[s][ii].first; ll w = adj[s][ii].second;
if(i != pre && (s == root || !in[s])){
dfs2(i, s, root, dep + w);
}
}
}
void get_comp(int s){
vis[s] = 1; comp.pb(s);
for(int idd = 0; idd < sz(adj[s]); idd++){
int i = adj[s][idd].first;
if(!vis[i]){
get_comp(i);
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
int v, w; cin >> v >> w;
to[i] = v; cost[i] = w;
adj[i].pb({v, w}); adj[v].pb({i, w});
}
ll ans = 0;
for(int i = 1; i <= n; i++){
if(!vis[i]){
ll cand = 0;
comp.clear(); get_comp(i);
int a = to[i], b = to[to[i]];
while(a != b){
a = to[a]; b = to[to[b]];
}
a = i;
while(a != b){
a = to[a]; b = to[b];
}
ll pp = 0;
vt<int>cyc; cyc.pb(a); in[a] = 1; pp += cost[a]; a = to[a];
vt<ll>pref; pref.pb(0);
while(a != b){
in[a] = 1;
cyc.pb(a); pref.pb(pp); pp += cost[a]; a = to[a];
}
for(int ii = 0; ii < sz(cyc); ii++){
int j = cyc[ii];
dfs(j, -1, j, 0);
mx = id = -1;
dfs2(j, -1, j, 0);
dfs2(id, -1, j, 0);
cand = max(cand, mx);
}
ll pmx = mxd[cyc[0]];
for(int j = 1; j < sz(cyc); j++){
cand = max(cand, pref[j] + mxd[cyc[j]] + pmx);
pmx = max(pmx, -pref[j] + mxd[cyc[j]]);
}
pmx = mxd[cyc[0]];
for(int j = 1; j < sz(cyc); j++){
cand = max(cand, -pref[j] + mxd[cyc[j]] + pmx + pp);
pmx = max(pmx, pref[j] + mxd[cyc[j]]);
}
ans += cand;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23820 KB |
Output is correct |
3 |
Correct |
11 ms |
23808 KB |
Output is correct |
4 |
Correct |
10 ms |
23832 KB |
Output is correct |
5 |
Correct |
10 ms |
23832 KB |
Output is correct |
6 |
Correct |
10 ms |
23832 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23776 KB |
Output is correct |
9 |
Correct |
10 ms |
23764 KB |
Output is correct |
10 |
Correct |
10 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23968 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24012 KB |
Output is correct |
2 |
Correct |
12 ms |
24364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
25392 KB |
Output is correct |
2 |
Correct |
23 ms |
28372 KB |
Output is correct |
3 |
Correct |
20 ms |
26008 KB |
Output is correct |
4 |
Correct |
16 ms |
24984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
30288 KB |
Output is correct |
2 |
Correct |
37 ms |
33920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
44664 KB |
Output is correct |
2 |
Correct |
77 ms |
49284 KB |
Output is correct |
3 |
Correct |
93 ms |
60792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
60520 KB |
Output is correct |
2 |
Correct |
148 ms |
86544 KB |
Output is correct |
3 |
Correct |
165 ms |
92288 KB |
Output is correct |
4 |
Correct |
203 ms |
118048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
118920 KB |
Output is correct |
2 |
Runtime error |
492 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
241 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |