#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 = 1000005;
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 |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
628 KB |
Output is correct |
7 |
Correct |
2 ms |
628 KB |
Output is correct |
8 |
Correct |
2 ms |
628 KB |
Output is correct |
9 |
Correct |
2 ms |
628 KB |
Output is correct |
10 |
Correct |
2 ms |
628 KB |
Output is correct |
11 |
Correct |
2 ms |
628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
628 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
660 KB |
Output is correct |
2 |
Correct |
4 ms |
952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1512 KB |
Output is correct |
2 |
Correct |
15 ms |
3000 KB |
Output is correct |
3 |
Correct |
10 ms |
3000 KB |
Output is correct |
4 |
Correct |
6 ms |
3000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4872 KB |
Output is correct |
2 |
Correct |
34 ms |
7500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
14100 KB |
Output is correct |
2 |
Correct |
69 ms |
17472 KB |
Output is correct |
3 |
Correct |
91 ms |
24896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
25020 KB |
Output is correct |
2 |
Correct |
164 ms |
34332 KB |
Output is correct |
3 |
Correct |
168 ms |
34332 KB |
Output is correct |
4 |
Correct |
225 ms |
43376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
43376 KB |
Output is correct |
2 |
Correct |
411 ms |
47324 KB |
Output is correct |
3 |
Correct |
258 ms |
47324 KB |
Output is correct |
4 |
Correct |
330 ms |
56264 KB |
Output is correct |
5 |
Correct |
313 ms |
69388 KB |
Output is correct |
6 |
Correct |
554 ms |
74204 KB |
Output is correct |
7 |
Correct |
352 ms |
107224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
107224 KB |
Output is correct |
2 |
Correct |
322 ms |
107224 KB |
Output is correct |
3 |
Correct |
367 ms |
120080 KB |
Output is correct |
4 |
Correct |
414 ms |
120080 KB |
Output is correct |
5 |
Runtime error |
341 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. |
6 |
Halted |
0 ms |
0 KB |
- |