# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
151531 |
2019-09-03T13:51:08 Z |
luciocf |
Islands (IOI08_islands) |
C++14 |
|
1625 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], 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)
{
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;
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());
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);
dist[indDist] = maiorDist2 = 0;
markCycle[c] = 0;
dfs2(indDist, 0);
markCycle[c] = 1;
diam = max(diam, maiorDist2);
}
ll maxi = -inf;
for (int i = 0; i < ciclo.size(); i++)
{
int u = ciclo[i];
M = max(M, maiorDist[u]+pref[i] + maxi);
maxi = max(maxi, maiorDist[u]-pref[i]);
}
maxi = -inf;
for (int i = 0; i < ciclo.size(); i++)
{
int u = ciclo[i];
M = max(M, maiorDist[u]-pref[i] + maxi + soma);
maxi = max(maxi, maiorDist[u]+pref[i]);
}
ans += max(M, diam);
}
printf("%lld\n", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:121:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ciclo.size(); i++)
~~^~~~~~~~~~~~~~
islands.cpp:140:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ciclo.size(); i++)
~~^~~~~~~~~~~~~~
islands.cpp:150:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ciclo.size(); i++)
~~^~~~~~~~~~~~~~
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 |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
29 ms |
23928 KB |
Output is correct |
5 |
Correct |
28 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23928 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23872 KB |
Output is correct |
9 |
Correct |
24 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
25 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
24056 KB |
Output is correct |
2 |
Correct |
26 ms |
24384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
25208 KB |
Output is correct |
2 |
Correct |
48 ms |
27696 KB |
Output is correct |
3 |
Correct |
39 ms |
25592 KB |
Output is correct |
4 |
Correct |
37 ms |
24764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
29176 KB |
Output is correct |
2 |
Correct |
72 ms |
32676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
40840 KB |
Output is correct |
2 |
Correct |
138 ms |
44468 KB |
Output is correct |
3 |
Correct |
173 ms |
53220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
56148 KB |
Output is correct |
2 |
Correct |
280 ms |
74932 KB |
Output is correct |
3 |
Correct |
296 ms |
81524 KB |
Output is correct |
4 |
Correct |
373 ms |
98788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
435 ms |
86944 KB |
Output is correct |
2 |
Correct |
1012 ms |
123452 KB |
Output is correct |
3 |
Correct |
416 ms |
88440 KB |
Output is correct |
4 |
Correct |
539 ms |
120256 KB |
Output is correct |
5 |
Correct |
526 ms |
120820 KB |
Output is correct |
6 |
Correct |
1625 ms |
96608 KB |
Output is correct |
7 |
Correct |
584 ms |
131072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
557 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |