//Arayi
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <bitset>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 0, -1, 0, 1, -1, -1, 1, 1, 0 };
int dy[] = { -1, 0, 1, 0, -1, 1, -1, 1, 0 };
const int N = 1e6 + 30;
lli mod = 1e9 + 7;
const ld pi = acos(-1);
const int T = 200;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template<class T>
void maxi(T& a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T& a, T b)
{
a = min(a, b);
}
int n;
vector <pair<int, lli> > g[N];
int col[N], cl[N];
vector<lli> fp, sm;
lli dp[N], dp1[N], pat;
void dfs0(int v, int par)
{
col[v] = 1;
fp.ad(v);
bool bl = 0;
for(auto p : g[v])
{
if(p.fr == par && !bl) {bl = 1; continue;}
if(col[p.fr] && sm.empty())
{
while(fp.back() != p.fr) sm.ad(fp.back()), cl[fp.back()] = 1, fp.pop_back();
cl[p.fr] = 1;
sm.ad(p.fr);
}
if(!col[p.fr]) dfs0(p.fr, v);
}
fp.pop_back();
}
void dfs1(int v, int par)
{
for(auto p : g[v])
{
if(cl[p.fr] || p.fr == par) continue;
dfs1(p.fr, v);
dp[v] = max(dp[v], dp[p.fr] + p.sc);
}
}
lli dfs(int v, int par)
{
vector <lli> fp;
lli ret = dp[v] + dp1[v];
for(auto p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
dp1[p.fr] = dp1[v] + p.sc;
fp.ad(dp[p.fr] + p.sc);
}
sort(all(fp));
reverse(all(fp));
if(fp.size() > 1)
{
for(auto p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
if(dp[p.fr] + p.sc == fp[0]) dp1[p.fr] = max(dp1[p.fr], fp[1] + dp[p.fr] + p.sc);
else dp1[p.fr] = max(dp1[p.fr], fp[0] + dp[p.fr] + p.sc);
}
}
for(auto p : g[v])
{
if(p.fr == par || cl[p.fr]) continue;
ret = max(ret, dfs(p.fr, v));
}
return ret;
}
int main()
{
fastio;
freopen("input.in", "r", stdin);
cin >> n;
for (int i = 1; i <= n; i++)
{
int a, b;
cin >> a >> b;
g[i].ad(MP(a, b));
g[a].ad(MP(i, b));
}
for (int i = 1; i <= n; i++)
{
if(!col[i])
{
dfs0(i, i);
lli ans = 0;
for(auto p : sm)
{
dfs1(p, p);
ans = max(ans, dfs(p, p));
}
fp.clear();
reverse(all(g[sm.back()]));
sm.ad(sm[0]);
for (int i = 0; i < (int)sm.size() - 1; i++)
for(auto p : g[sm[i]]) if(p.fr == sm[i + 1]) {fp.ad(p.sc); break;}
sm.pop_back();
lli mx = 0, nx = 0;
for (int i = 0; i < sm.size(); i++)
{
ans = max(ans, dp[sm[i]] + nx + mx);
mx = max(mx, dp[sm[i]] - nx);
nx += fp[i];
}
lli sum = nx; nx = mx = 0;
for (int i = 0; i < sm.size(); i++)
{
if(i) ans = max(ans, dp[sm[i]] + sum - nx + mx);
mx = max(mx, dp[sm[i]] + nx);
nx += fp[i];
}
pat += ans;
//cout << ans << " ";
fp.clear(), sm.clear();
}
}
cout << pat << endl;
cout << "ended" << endl;
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:187:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
187 | for (int i = 0; i < sm.size(); i++)
| ~~^~~~~~~~~~~
islands.cpp:194:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
194 | for (int i = 0; i < sm.size(); i++)
| ~~^~~~~~~~~~~
islands.cpp:160:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | freopen("input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23884 KB |
Output isn't correct |
2 |
Incorrect |
16 ms |
23820 KB |
Output isn't correct |
3 |
Incorrect |
16 ms |
23836 KB |
Output isn't correct |
4 |
Incorrect |
18 ms |
23884 KB |
Output isn't correct |
5 |
Incorrect |
17 ms |
23884 KB |
Output isn't correct |
6 |
Incorrect |
16 ms |
23884 KB |
Output isn't correct |
7 |
Incorrect |
16 ms |
23884 KB |
Output isn't correct |
8 |
Incorrect |
17 ms |
23884 KB |
Output isn't correct |
9 |
Incorrect |
16 ms |
23832 KB |
Output isn't correct |
10 |
Incorrect |
19 ms |
23884 KB |
Output isn't correct |
11 |
Incorrect |
16 ms |
23888 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
23884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
23888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
23884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |