# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
197464 |
2020-01-21T12:07:53 Z |
SamAnd |
Islands (IOI08_islands) |
C++17 |
|
1242 ms |
131076 KB |
#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));
}
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:150:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vv.size(); ++i)
~~^~~~~~~~~~~
islands.cpp:153:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < vv[i].size(); ++j)
~~^~~~~~~~~~~~~~
islands.cpp:155: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);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
27740 KB |
Output is correct |
2 |
Correct |
28 ms |
27768 KB |
Output is correct |
3 |
Correct |
28 ms |
27768 KB |
Output is correct |
4 |
Correct |
29 ms |
27768 KB |
Output is correct |
5 |
Correct |
28 ms |
27768 KB |
Output is correct |
6 |
Correct |
28 ms |
27768 KB |
Output is correct |
7 |
Correct |
27 ms |
27768 KB |
Output is correct |
8 |
Correct |
26 ms |
27768 KB |
Output is correct |
9 |
Correct |
28 ms |
27740 KB |
Output is correct |
10 |
Correct |
28 ms |
27768 KB |
Output is correct |
11 |
Correct |
24 ms |
27768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
27896 KB |
Output is correct |
2 |
Correct |
29 ms |
27916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
27996 KB |
Output is correct |
2 |
Correct |
30 ms |
28152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
29284 KB |
Output is correct |
2 |
Correct |
53 ms |
31352 KB |
Output is correct |
3 |
Correct |
42 ms |
29560 KB |
Output is correct |
4 |
Correct |
35 ms |
28664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
32856 KB |
Output is correct |
2 |
Correct |
84 ms |
36724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
45120 KB |
Output is correct |
2 |
Correct |
140 ms |
47404 KB |
Output is correct |
3 |
Correct |
170 ms |
55068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
59888 KB |
Output is correct |
2 |
Correct |
278 ms |
74204 KB |
Output is correct |
3 |
Correct |
293 ms |
78292 KB |
Output is correct |
4 |
Correct |
373 ms |
90580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
442 ms |
85148 KB |
Output is correct |
2 |
Correct |
934 ms |
120100 KB |
Output is correct |
3 |
Correct |
422 ms |
88824 KB |
Output is correct |
4 |
Correct |
544 ms |
113684 KB |
Output is correct |
5 |
Correct |
522 ms |
113876 KB |
Output is correct |
6 |
Correct |
1242 ms |
100728 KB |
Output is correct |
7 |
Correct |
570 ms |
131072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
540 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |