# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1056531 |
2024-08-13T09:54:46 Z |
lovrot |
Islands (IOI08_islands) |
C++17 |
|
286 ms |
131072 KB |
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#define debug(...) //fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
int n, deg[N], to[N], weg[N];
vector<pii> g[N];
int cyci;
pii cyc[N];
void push_back(pii a) {
cyc[cyci++] = a;
}
void clear() {
cyci = 0;
}
char bio[N];
int dfs(int u, int p) {
bio[u] = 1;
for(pii &e : g[u]) {
int v = e.X, w = e.Y;
if(w != p) {
if(bio[v]) {
push_back({e.X, weg[e.Y]});
// cyc.PB({e.X, weg[e.Y]});
return v;
} else {
int res = dfs(v, w);
if(res) {
push_back({e.X, weg[e.Y]});
// cyc.PB({e.X, weg[e.Y]});
return res != u ? res : 0;
}
}
}
}
bio[u] = 0;
return 0;
}
void mark(int u) {
bio[u] = 2;
for(pii e : g[u]) {
if(bio[e.X] != 2) {
mark(e.X);
}
}
}
ll dep[N], ans_;
ll depth(int u, int p) {
ll max_ = 0;
for(pii e : g[u]) {
int v = e.X, w = e.Y;
if(w != p && !bio[v]) {
ll res = (ll) weg[w] + depth(v, w);
if(dep[u] < res) {
max_ = dep[u];
dep[u] = res;
} else if(max_ < res) {
max_ = res;
}
}
}
ans_ = max(ans_, dep[u] + max_);
return dep[u];
}
multiset<ll> s;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d%d", to + i, weg + i);
++deg[i];
++deg[to[i]];
}
for(int i = 1; i <= n; ++i) {
g[i].reserve(deg[i]);
}
for(int i = 1; i <= n; ++i) {
g[i].PB({to[i], i});
g[to[i]].PB({i, i});
}
ll prf, prf_, len, ans = 0;
int v, w;
for(int i = 1; i <= n; ++i) {
if(bio[i]) { continue; }
clear();
s.clear();
ans_ = 0;
dfs(i, 0);
for(int j = 0; j < cyci; ++j) {
depth(cyc[j].X, 0);
debug("%d(%d, %lld)\n", e.X, e.Y, dep[e.X]);
}
mark(i);
prf = 0;
for(int j = 0; j < cyci; ++j) {
v = cyc[j].X;
w = cyc[j].Y;
s.insert(dep[v] + prf);
prf += w;
}
prf_ = 0;
len = prf;
for(int j = 0; j < cyci; ++j) {
v = cyc[j].X;
w = cyc[j].Y;
s.erase(s.find(dep[v] + prf - len));
ans_ = max(ans_, *prev(s.end()) + dep[v] - prf + len);
s.insert(dep[v] + prf);
prf += w;
}
ans += ans_;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:106:10: warning: variable 'prf_' set but not used [-Wunused-but-set-variable]
106 | ll prf, prf_, len, ans = 0;
| ^~~~
islands.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d", to + i, weg + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
3 ms |
31068 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31224 KB |
Output is correct |
7 |
Correct |
4 ms |
31064 KB |
Output is correct |
8 |
Correct |
4 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
4 ms |
31068 KB |
Output is correct |
11 |
Correct |
4 ms |
31064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31324 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31320 KB |
Output is correct |
2 |
Correct |
5 ms |
31684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
32088 KB |
Output is correct |
2 |
Correct |
15 ms |
36812 KB |
Output is correct |
3 |
Correct |
9 ms |
34452 KB |
Output is correct |
4 |
Correct |
6 ms |
31664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
38236 KB |
Output is correct |
2 |
Correct |
29 ms |
42320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
48584 KB |
Output is correct |
2 |
Correct |
74 ms |
56284 KB |
Output is correct |
3 |
Correct |
96 ms |
67800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
57940 KB |
Output is correct |
2 |
Correct |
162 ms |
85740 KB |
Output is correct |
3 |
Correct |
252 ms |
91728 KB |
Output is correct |
4 |
Correct |
259 ms |
116820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
90960 KB |
Output is correct |
2 |
Runtime error |
286 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
193 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |