#include <bits/stdc++.h>
using namespace std;
#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;
void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}
const int maxn = 1000001;
int n, deg[maxn];
ll Ans, dm[maxn];
pii adj[maxn];
pair<ll,ll> dp[maxn];
ll solve(int x)
{
deque<pair<ll,int> > Q;
vector<pair<int,ll> > V; V.emplace_back(mp(0,0ll));
int sz = 0; ll ret = 0;
while(deg[x])
{
-- deg[x];
V.pb(mp(x, adj[x].se));
dm[x] = max(dm[x], dp[x].fi + dp[x].se);
ret = max(ret, dm[x]);
x = adj[x].fi;
++ sz;
}
fo(i,1,sz) V[i].se += V[i-1].se;
fo(i,2,sz)
{
while(SZ(Q) && Q.back().fi < V[i-1].se + dp[V[i].fi].fi) Q.pop_back();
Q.emplace_back(mp(V[i-1].se + dp[V[i].fi].fi, V[i].fi));
}
ret = max(ret, dp[V[1].fi].fi + Q.front().fi);
fo(i,1,sz-1)
{
if(Q.front().se == V[i+1].fi) Q.pop_front();
while(SZ(Q) && Q.back().fi < V[sz].se + V[i-1].se + dp[V[i].fi].fi) Q.pop_back();
Q.emplace_back(mp(V[sz].se+V[i-1].se+dp[V[i].fi].fi, V[i].fi));
ret = max(ret, Q.front().fi + dp[V[i+1].fi].fi - V[i].se);
}
return ret;
}
int main()
{
sf("%d",&n);
fo(i,1,n) {int u, v; sf("%d%d",&u,&v); deg[u] ++; adj[i] = (mp(u, v));}
queue<int> Q;
fo(i,1,n) if(!deg[i]) Q.push(i);
while(!Q.empty())
{
int h = Q.front(); Q.pop();
pii p = adj[h];
dm[h] = max(dm[h], dp[h].fi + dp[h].se);
dm[p.fi] = max(dm[p.fi], dm[h]);
ll x = dp[h].fi + p.se;
if(x > dp[p.fi].fi) swap(x, dp[p.fi].fi);
if(x > dp[p.fi].se) swap(x, dp[p.fi].se);
if(!(--deg[p.fi])) Q.push(p.fi);
}
fo(i,1,n) if(deg[i]) Ans += solve(i);
cout << Ans << endl;
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:73:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d",&n);
^
islands.cpp:74:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,1,n) {int u, v; sf("%d%d",&u,&v); deg[u] ++; adj[i] = (mp(u, v));}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
472 KB |
Output is correct |
5 |
Correct |
2 ms |
476 KB |
Output is correct |
6 |
Correct |
2 ms |
524 KB |
Output is correct |
7 |
Correct |
2 ms |
532 KB |
Output is correct |
8 |
Correct |
2 ms |
536 KB |
Output is correct |
9 |
Correct |
2 ms |
540 KB |
Output is correct |
10 |
Correct |
2 ms |
544 KB |
Output is correct |
11 |
Correct |
2 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
564 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
740 KB |
Output is correct |
2 |
Correct |
3 ms |
952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1668 KB |
Output is correct |
2 |
Correct |
14 ms |
3028 KB |
Output is correct |
3 |
Correct |
10 ms |
3028 KB |
Output is correct |
4 |
Correct |
6 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
4900 KB |
Output is correct |
2 |
Correct |
33 ms |
7536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
14100 KB |
Output is correct |
2 |
Correct |
71 ms |
17484 KB |
Output is correct |
3 |
Correct |
88 ms |
24960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
24996 KB |
Output is correct |
2 |
Correct |
165 ms |
34472 KB |
Output is correct |
3 |
Correct |
170 ms |
34472 KB |
Output is correct |
4 |
Correct |
232 ms |
43324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
43324 KB |
Output is correct |
2 |
Correct |
417 ms |
47396 KB |
Output is correct |
3 |
Correct |
260 ms |
47396 KB |
Output is correct |
4 |
Correct |
578 ms |
56172 KB |
Output is correct |
5 |
Correct |
314 ms |
69448 KB |
Output is correct |
6 |
Correct |
536 ms |
69448 KB |
Output is correct |
7 |
Correct |
343 ms |
79876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
79876 KB |
Output is correct |
2 |
Correct |
314 ms |
79876 KB |
Output is correct |
3 |
Correct |
370 ms |
79876 KB |
Output is correct |
4 |
Correct |
415 ms |
79876 KB |
Output is correct |
5 |
Correct |
313 ms |
79876 KB |
Output is correct |
6 |
Correct |
322 ms |
79876 KB |
Output is correct |
7 |
Correct |
587 ms |
90168 KB |
Output is correct |