# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
151534 |
2019-09-03T13:55:44 Z |
luciocf |
Islands (IOI08_islands) |
C++14 |
|
1626 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 pref[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, ll dist)
{
if (dist >= maiorDist[root])
indDist = u;
maiorDist[root] = max(maiorDist[root], dist);
for (auto pp: grafo[u])
{
int v = pp.first, e = pp.second;
if (v == p || markCycle[v]) continue;
dfs(v, u, root, dist+1ll*peso[e]);
}
}
void dfs2(int u, int p, ll dist)
{
maiorDist2 = max(maiorDist2, dist);
for (auto pp: grafo[u])
{
int v = pp.first, e = pp.second;
if (v == p || markCycle[v]) continue;
dfs2(v, u, dist+1ll*peso[e]);
}
}
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;
vector<int> ciclo;
for (int T = 1; T <= n; T++)
{
if (mark[T]) continue;
findCycle(T, 0);
int at = lastCycle;
ll soma = backEdge;
ciclo.clear();
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());
ll M = 0, diam = 0;
for (int i = 0; i < ciclo.size(); i++)
{
int c = ciclo[i];
if (i > 0)
pref[i] = pref[i-1] + 1ll*edgePai[c];
dfs(c, 0, c, 0);
maiorDist2 = 0;
markCycle[c] = 0;
dfs2(indDist, 0, 0);
markCycle[c] = 1;
diam = max(diam, maiorDist2);
}
ll maxi1 = -inf, maxi2 = -inf;
for (int i = 0; i < ciclo.size(); i++)
{
int u = ciclo[i];
M = max(M, maiorDist[u]+pref[i] + maxi1);
maxi1 = max(maxi1, maiorDist[u]-pref[i]);
M = max(M, maiorDist[u]-pref[i] + maxi2 + soma);
maxi2 = max(maxi2, maiorDist[u]+pref[i]);
}
ans += max(M, diam);
}
printf("%lld\n", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:120:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ciclo.size(); i++)
~~^~~~~~~~~~~~~~
islands.cpp:139:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ciclo.size(); i++)
~~^~~~~~~~~~~~~~
islands.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:83: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 |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23800 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
28 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
29 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
23928 KB |
Output is correct |
2 |
Correct |
26 ms |
24144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
24952 KB |
Output is correct |
2 |
Correct |
45 ms |
26872 KB |
Output is correct |
3 |
Correct |
39 ms |
25080 KB |
Output is correct |
4 |
Correct |
31 ms |
24464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
28124 KB |
Output is correct |
2 |
Correct |
71 ms |
30820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
36828 KB |
Output is correct |
2 |
Correct |
132 ms |
40052 KB |
Output is correct |
3 |
Correct |
163 ms |
47564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
47736 KB |
Output is correct |
2 |
Correct |
265 ms |
65516 KB |
Output is correct |
3 |
Correct |
291 ms |
70444 KB |
Output is correct |
4 |
Correct |
390 ms |
84320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
439 ms |
75864 KB |
Output is correct |
2 |
Correct |
988 ms |
108092 KB |
Output is correct |
3 |
Correct |
399 ms |
71640 KB |
Output is correct |
4 |
Correct |
525 ms |
100272 KB |
Output is correct |
5 |
Correct |
547 ms |
101220 KB |
Output is correct |
6 |
Correct |
1626 ms |
74252 KB |
Output is correct |
7 |
Correct |
555 ms |
127668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
616 ms |
131072 KB |
Output is correct |
2 |
Correct |
613 ms |
130292 KB |
Output is correct |
3 |
Runtime error |
586 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |