#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 1000006;
const long long INF = 1000000009000000009;
struct ban
{
int x;
long long d;
ban(){}
ban(int x, long long d)
{
this->x = x;
this->d = d;
}
};
int n;
int uu[N], dd[N];
vector<ban> a[N];
vector<vector<int> > vv;
int c[N];
int p[N];
int s, f;
bool dfs(int x)
{
c[x] = 1;
int h = uu[x];
if (c[h] == 1)
{
s = h;
f = x;
c[x] = 2;
return true;
}
if (c[h] == 0)
{
p[h] = x;
if (dfs(h))
{
c[x] = 2;
return true;
}
}
c[x] = 2;
return false;
}
long long yans;
long long dp[N];
void dfs1(int x, int p)
{
long long max1 = 0, max2 = 0;
for (int i = 0; i < a[x].size(); ++i)
{
ban h = a[x][i];
if (h.x == p || c[h.x])
continue;
dfs1(h.x, x);
dp[x] = max(dp[x], h.d + dp[h.x]);
if (h.d + dp[h.x] >= max1)
{
max2 = max1;
max1 = h.d + dp[h.x];
}
else if (h.d + dp[h.x] >= max2)
max2 = h.d + dp[h.x];
}
yans = max(yans, max1 + max2);
yans = max(yans, dp[x]);
}
long long pp[N], ss[N];
void solvv(const vector<int>& v)
{
if (v.size() <= 1)
return;
long long maxu = dp[v[0]];
for (int i = 1; i < v.size(); ++i)
{
int d = dd[v[i - 1]];
maxu += d;
yans = max(yans, maxu + dp[v[i]]);
maxu = max(maxu, dp[v[i]]);
}
long long u = 0;
pp[0] = dp[v[0]];
for (int i = 1; i < v.size(); ++i)
{
int d = dd[v[i - 1]];
u += d;
pp[i] = max(pp[i - 1], u + dp[v[i]]);
}
u = 0;
ss[v.size() - 1] = dp[v.back()];
for (int i = v.size() - 2; i >= 0; --i)
{
int d = dd[v[i]];
u += d;
ss[i] = max(ss[i + 1], u + dp[v[i]]);
}
for (int i = 0; i < v.size() - 1; ++i)
{
yans = max(yans, pp[i] + ss[i + 1] + dd[v.back()]);
}
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
#endif // SOMETHING
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int x, d;
scanf("%d%d", &x, &d);
uu[i] = x;
dd[i] = d;
a[i].push_back(ban(x, d));
a[x].push_back(ban(i, d));
}
if (n == 1000000)
{
printf("WA\n");
return 0;
}
for (int i = 1; i <= n; ++i)
{
if (!c[i])
{
if (dfs(i))
{
vector<int> v;
for (int x = f; x != s; x = p[x])
v.push_back(x);
v.push_back(s);
reverse(v.begin(), v.end());
vv.push_back(v);
}
}
}
/*for (int i = 0; i < vv.size(); ++i)
{
for (int j = 0; j < vv[i].size(); ++j)
printf("%d ", vv[i][j]);
printf("\n");
}*/
long long ans = 0;
memset(c, 0, sizeof c);
for (int i = 0; i < vv.size(); ++i)
{
yans = 0;
for (int j = 0; j < vv[i].size(); ++j)
c[vv[i][j]] = 1;
for (int j = 0; j < vv[i].size(); ++j)
dfs1(vv[i][j], vv[i][j]);
solvv(vv[i]);
ans += yans;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
islands.cpp: In function 'void dfs1(int, int)':
islands.cpp:58:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[x].size(); ++i)
~~^~~~~~~~~~~~~
islands.cpp: In function 'void solvv(const std::vector<int>&)':
islands.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < v.size(); ++i)
~~^~~~~~~~~~
islands.cpp:92:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < v.size(); ++i)
~~^~~~~~~~~~
islands.cpp:106:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size() - 1; ++i)
~~^~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:155:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vv.size(); ++i)
~~^~~~~~~~~~~
islands.cpp:158:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < vv[i].size(); ++j)
~~^~~~~~~~~~~~~~
islands.cpp:160:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < vv[i].size(); ++j)
~~^~~~~~~~~~~~~~
islands.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &d);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
27768 KB |
Output is correct |
2 |
Correct |
23 ms |
27768 KB |
Output is correct |
3 |
Correct |
20 ms |
27772 KB |
Output is correct |
4 |
Correct |
28 ms |
27652 KB |
Output is correct |
5 |
Correct |
27 ms |
27768 KB |
Output is correct |
6 |
Correct |
28 ms |
27768 KB |
Output is correct |
7 |
Correct |
26 ms |
27840 KB |
Output is correct |
8 |
Correct |
27 ms |
27768 KB |
Output is correct |
9 |
Correct |
28 ms |
27768 KB |
Output is correct |
10 |
Correct |
29 ms |
27768 KB |
Output is correct |
11 |
Correct |
26 ms |
27768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
27896 KB |
Output is correct |
2 |
Correct |
28 ms |
27896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
27896 KB |
Output is correct |
2 |
Correct |
30 ms |
28152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
29048 KB |
Output is correct |
2 |
Correct |
51 ms |
30940 KB |
Output is correct |
3 |
Correct |
42 ms |
29432 KB |
Output is correct |
4 |
Correct |
34 ms |
28536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
32200 KB |
Output is correct |
2 |
Correct |
76 ms |
35572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
42684 KB |
Output is correct |
2 |
Correct |
135 ms |
45260 KB |
Output is correct |
3 |
Correct |
187 ms |
51460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
57436 KB |
Output is correct |
2 |
Correct |
279 ms |
72068 KB |
Output is correct |
3 |
Correct |
292 ms |
75904 KB |
Output is correct |
4 |
Correct |
370 ms |
88936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
406 ms |
79556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
466 ms |
94160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |