# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
53728 |
2018-07-01T06:02:58 Z |
강한남자킹현수(#2036) |
Islands (IOI08_islands) |
C++11 |
|
555 ms |
136528 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
#define X first
#define Y second
const int N = 1000005;
const ll INF = ll(1e18);
vector<pil> e[N];
int n, v[N], o[N], c, p[N], q[N];
vector<int> w;
vector<ll> d;
ll a[2 * N], b[2 * N], r, u[N];
void f(int x, int y){
if(q[x]){
p[c] = -1;
for(int i = c - 1; p[i + 1] != x; i--){
w.push_back(p[i]);
d.push_back(u[i]);
o[p[i]] = 1;
}
return;
}
if(v[x]) return;
v[x] = 1;
int r = 0;
for(pil i : e[x]){
if(i.X == y) continue;
q[x] = 1;
p[c] = x;
u[c] = i.Y;
c++;
f(i.X, x);
q[x] = 0;
c--;
}
}
ll g(int x, int p){
ll r = 0;
for(pil i : e[x]){
if(o[i.X] || i.X == p) continue;
r = max(r, g(i.X, x) + i.Y);
}
return r;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int x; ll y;
scanf("%d%lld", &x, &y);
e[i].push_back({x, y});
e[x].push_back({i, y});
}
for(int i = 1; i <= n; i++){
if(v[i]) continue;
w.clear();
d.clear();
f(i, 0);
int s = w.size();
//for(int j = 0; j < s; j++) printf("aa %d %lld\n", w[j], d[j]);
for(int j = 0; j < s; j++){
a[j] = a[j + s] = g(w[j], 0);
b[j] = d[j] + (j ? b[j - 1] : 0);
}
//for(int j = 0; j < s; j++) printf("bb %lld %lld\n", a[j], b[j]);
for(int j = 0; j < s; j++) b[j + s] = b[j + s - 1] + d[j];
ll t = 0;
priority_queue<pli> lq, rq;
for(int j = 1; j < s; j++) rq.push({a[j] + b[j], j});
for(int j = 0, k = 0; j < s; j++){
while(!lq.empty() && lq.top().Y <= j) lq.pop();
while(k < j + s && b[k] - b[j] < b[j + s] - b[k]) k++;
while(!rq.empty() && rq.top().Y < k){
int x = rq.top().Y;
lq.push({a[x] - b[x], x});
rq.pop();
}
t = max(t, max((lq.empty() ? -INF : lq.top().X) + b[j + s],
(rq.empty() ? -INF : rq.top().X) - b[j]) + a[j]);
rq.push({a[j + s] + b[j + s], j + s});
}
r += t;
}
printf("%lld\n", r);
}
Compilation message
islands.cpp: In function 'void f(int, int)':
islands.cpp:32:9: warning: unused variable 'r' [-Wunused-variable]
int r = 0;
^
islands.cpp: In function 'int main()':
islands.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%lld", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Incorrect |
24 ms |
24168 KB |
Output isn't correct |
3 |
Correct |
24 ms |
24168 KB |
Output is correct |
4 |
Incorrect |
23 ms |
24168 KB |
Output isn't correct |
5 |
Correct |
23 ms |
24168 KB |
Output is correct |
6 |
Incorrect |
27 ms |
24168 KB |
Output isn't correct |
7 |
Incorrect |
24 ms |
24168 KB |
Output isn't correct |
8 |
Incorrect |
25 ms |
24168 KB |
Output isn't correct |
9 |
Incorrect |
28 ms |
24168 KB |
Output isn't correct |
10 |
Incorrect |
24 ms |
24168 KB |
Output isn't correct |
11 |
Incorrect |
26 ms |
24172 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
24188 KB |
Output is correct |
2 |
Incorrect |
25 ms |
24192 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
24192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
25472 KB |
Output is correct |
2 |
Correct |
53 ms |
28676 KB |
Output is correct |
3 |
Incorrect |
41 ms |
28676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
30708 KB |
Output is correct |
2 |
Correct |
83 ms |
33664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
39816 KB |
Output is correct |
2 |
Incorrect |
307 ms |
50080 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
51652 KB |
Output is correct |
2 |
Correct |
369 ms |
84824 KB |
Output is correct |
3 |
Incorrect |
519 ms |
105504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
433 ms |
105504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
555 ms |
136528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |