#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
int N;
vector<pii> adj[MAXN+1];
pii to[MAXN+1];
ll ans, val;
bool vis[MAXN+1];
ll dfs(int now, ll dep)
{
vis[now]=true;
ll ret=dep;
ll ta=0, tb=0;
for(auto nxt : adj[now])
{
if(vis[nxt.first]) continue;
ll p=dfs(nxt.first, dep+nxt.second);
ret=max(ret, p);
if(p>ta) tb=ta, ta=p;
else if(p>tb) tb=p;
}
val=max(val, ta+tb-dep-dep);
return ret;
}
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<=N; i++)
{
int u=i, v, w;
scanf("%d%d", &v, &w);
to[u]={v, w};
adj[v].push_back({u, w});
}
for(i=1; i<=N; i++)
{
if(vis[i]) continue;
int now=i;
vector<int> cyc;
while(!vis[now])
{
cyc.push_back(now);
vis[now]=1;
now=to[now].first;
}
reverse(cyc.begin(), cyc.end());
while(!cyc.empty() && cyc.back()!=now) vis[cyc.back()]=0, cyc.pop_back();
reverse(cyc.begin(), cyc.end());
vector<ll> A, D;
ll s=0; val=0;
D.push_back(0);
for(auto it : cyc) A.push_back(dfs(it, 0));
for(auto it : cyc) D.push_back(D.back()+to[it].second), s+=to[it].second;
D.pop_back();
ll t=-1e18;
for(j=0; j<cyc.size(); j++)
{
if(j) val=max(val, D[j]+A[j]+t);
t=max(t, A[j]-D[j]);
}
t=-1e18;
for(j=0; j<cyc.size(); j++)
{
if(j) val=max(val, s-D[j]+A[j]+t);
t=max(t, D[j]+A[j]);
}
ans+=val;
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:75:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<cyc.size(); j++)
~^~~~~~~~~~~
islands.cpp:82:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<cyc.size(); j++)
~^~~~~~~~~~~
islands.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
islands.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &w);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23672 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23800 KB |
Output is correct |
5 |
Correct |
20 ms |
23800 KB |
Output is correct |
6 |
Correct |
20 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
21 ms |
23800 KB |
Output is correct |
10 |
Correct |
22 ms |
23804 KB |
Output is correct |
11 |
Correct |
40 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24292 KB |
Output is correct |
2 |
Correct |
37 ms |
25080 KB |
Output is correct |
3 |
Correct |
30 ms |
24440 KB |
Output is correct |
4 |
Correct |
25 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
25720 KB |
Output is correct |
2 |
Correct |
59 ms |
27440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
30196 KB |
Output is correct |
2 |
Correct |
103 ms |
35184 KB |
Output is correct |
3 |
Correct |
133 ms |
35564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
36684 KB |
Output is correct |
2 |
Correct |
207 ms |
42212 KB |
Output is correct |
3 |
Correct |
220 ms |
54604 KB |
Output is correct |
4 |
Correct |
295 ms |
52316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
40528 KB |
Output is correct |
2 |
Correct |
695 ms |
65724 KB |
Output is correct |
3 |
Correct |
320 ms |
46848 KB |
Output is correct |
4 |
Correct |
397 ms |
60772 KB |
Output is correct |
5 |
Correct |
394 ms |
59112 KB |
Output is correct |
6 |
Correct |
918 ms |
52860 KB |
Output is correct |
7 |
Correct |
442 ms |
70340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
485 ms |
130648 KB |
Output is correct |
2 |
Correct |
476 ms |
95480 KB |
Output is correct |
3 |
Correct |
454 ms |
88016 KB |
Output is correct |
4 |
Correct |
564 ms |
64112 KB |
Output is correct |
5 |
Correct |
399 ms |
58716 KB |
Output is correct |
6 |
Correct |
395 ms |
57836 KB |
Output is correct |
7 |
Correct |
959 ms |
53500 KB |
Output is correct |