#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 = 1000050;
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 |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
528 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 |
712 KB |
Output is correct |
10 |
Correct |
2 ms |
712 KB |
Output is correct |
11 |
Correct |
2 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
736 KB |
Output is correct |
2 |
Correct |
3 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
736 KB |
Output is correct |
2 |
Correct |
4 ms |
1136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1620 KB |
Output is correct |
2 |
Correct |
14 ms |
3092 KB |
Output is correct |
3 |
Correct |
9 ms |
3092 KB |
Output is correct |
4 |
Correct |
5 ms |
3092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
5044 KB |
Output is correct |
2 |
Correct |
33 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
14236 KB |
Output is correct |
2 |
Correct |
71 ms |
17628 KB |
Output is correct |
3 |
Correct |
88 ms |
25592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
31092 KB |
Output is correct |
2 |
Correct |
157 ms |
46004 KB |
Output is correct |
3 |
Correct |
166 ms |
49956 KB |
Output is correct |
4 |
Correct |
224 ms |
69812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
73528 KB |
Output is correct |
2 |
Correct |
431 ms |
104744 KB |
Output is correct |
3 |
Correct |
252 ms |
107888 KB |
Output is correct |
4 |
Runtime error |
321 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
318 ms |
132096 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
2 |
Halted |
0 ms |
0 KB |
- |