# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
151513 |
2019-09-03T13:22:27 Z |
luciocf |
Islands (IOI08_islands) |
C++14 |
|
561 ms |
131076 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6+10;
const ll inf = 1e18+10;
int n;
int peso[maxn];
int firstCycle, lastCycle, backEdge;
int pai[maxn], indCycle[maxn], edgePai[maxn];
ll dist[maxn], max1[maxn], max2[maxn];
ll maiorDist[maxn], maiorDist2;
int indDist;
bool mark[maxn], markCycle[maxn];
vector<pii> grafo[maxn];
void findCycle(int u, int p)
{
mark[u] = 1;
for (auto pp: grafo[u])
{
int v = pp.first, e = pp.second;
if (e == p) continue;
if (mark[v])
{
firstCycle = u, lastCycle = v;
backEdge = peso[e];
continue;
}
pai[v] = u, edgePai[v] = peso[e];
findCycle(v, e);
}
}
void dfs(int u, int p, int root)
{
if (dist[u] >= maiorDist[root])
indDist = u;
maiorDist[root] = max(maiorDist[root], dist[u]);
for (auto pp: grafo[u])
{
int v = pp.first, e = pp.second;
if (v == p || markCycle[v]) continue;
dist[v] = dist[u]+1ll*peso[e];
dfs(v, u, root);
}
}
void dfs2(int u, int p)
{
maiorDist2 = max(maiorDist2, dist[u]);
for (auto pp: grafo[u])
{
int v = pp.first, e = pp.second;
if (v == p || markCycle[v]) continue;
dist[v] = dist[u]+1ll*peso[e];
dfs2(v, u);
}
}
int main(void)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int u, w;
scanf("%d %d", &u, &w);
peso[i] = w;
grafo[u].push_back({i, i});
grafo[i].push_back({u, i});
}
ll ans = 0, ans2 = 0;
for (int T = 1; T <= n; T++)
{
if (mark[T]) continue;
findCycle(T, 0);
int at = lastCycle;
ll soma = backEdge;
vector<int> ciclo;
while (true)
{
ciclo.push_back(at);
markCycle[at] = 1;
if (at == firstCycle) break;
soma += 1ll*edgePai[at];
at = pai[at];
}
reverse(ciclo.begin(), ciclo.end());
for (auto c: ciclo)
{
dfs(c, 0, c);
dist[indDist] = maiorDist2 = 0;
dfs2(indDist, 0);
ans2 = max(ans2, maiorDist2);
}
ll M = 0;
max1[ciclo.back()] = max2[ciclo.back()] = -inf;
for (int i = ciclo.size()-2; i >= 0; i--)
{
int u = ciclo[i], v = ciclo[i+1];
max1[u] = max(maiorDist[v], max1[v]) + 1ll*edgePai[v];
max2[u] = max(soma-1ll*edgePai[v]+maiorDist[v], max2[v]-1ll*edgePai[v]);
M = max(max1[u], max2[u]) + maiorDist[u];
}
ans += M;
}
printf("%lld\n", max(ans, ans2));
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &w);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Incorrect |
24 ms |
23928 KB |
Output isn't correct |
3 |
Incorrect |
23 ms |
23928 KB |
Output isn't correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
24 ms |
23928 KB |
Output is correct |
6 |
Correct |
25 ms |
23800 KB |
Output is correct |
7 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
8 |
Incorrect |
23 ms |
23804 KB |
Output isn't correct |
9 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
10 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
11 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
24056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
24028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
25436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
29848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
135 ms |
43784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
221 ms |
62304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
428 ms |
99148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
561 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |