# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
219669 |
2020-04-05T22:59:58 Z |
tatyam |
Islands (IOI08_islands) |
C++17 |
|
1696 ms |
104156 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 0x1fffffffffffffff;
#define overload3(a,b,c,d,...) d
#define all(a) begin(a), end(a)
#define rep1(n) for(int i = 0; i < n; i++)
#define rep2(i,n) for(int i = 0; i < n; i++)
#define rep3(i,a,b) for(int i = a; i < b; i++)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
struct SlidMax{
deque<pair<ll, int>> q;
int low = 0, high = 0;
ll pop(){
ll ans = q[0].first;
if(q.front().second == low++) q.pop_front();
return ans;
}
void push(ll x){
while(q.size() && q.back().first <= x) q.pop_back();
q.emplace_back(x, high++);
}
};
const int MAX = 1000000;
bool circle[MAX], used[MAX];
vector<pii> g[MAX];
ll d[MAX], cost1[MAX], cost2[MAX];
inline ll Diameter(int at){
queue<int> q; // 6MB
cost1[at] = 0;
q.push(at);
ll max = 0;
int idx;
while(q.size()){
int at = q.front();
q.pop();
each(i, g[at]) if(cost1[i.first] == INF){
cost1[i.first] = cost1[at] + i.second;
if(chmax(max, cost1[i.first])) idx = i.first;
q.push(i.first);
}
}
cost2[idx] = 0;
q.push(idx);
while(q.size()){
int at = q.front();
q.pop();
each(i, g[at]) if(cost2[i.first] == INF){
cost2[i.first] = cost2[at] + i.second;
chmax(max, cost2[i.first]);
q.push(i.first);
}
}
return max;
}
int main(){
fill(cost1, cost1 + MAX, INF);
fill(cost2, cost2 + MAX, INF);
int n;
scanf("%d", &n);
rep(n){
int a, b;
scanf("%d%d", &a, &b);
a--;
g[i].emplace_back(a, b);
}
rep(n) g[g[i][0].first].emplace_back(i, g[i][0].second);
each(i, g) i.shrink_to_fit();
ll ans = 0;
rep(n) if(!used[i]){
int at = i;
vector<int> list = {i};
while(!used[at]){
used[at] = 1;
list.push_back(at);
at = g[at][0].first;
}
each(i, list) used[i] = 0;
int circle_size = 0;
while(!circle[at]){
circle[at] = 1;
circle_size++;
at = g[at][0].first;
}
used[at] = 1;
list = {at};
for(int i = 0; i < list.size(); i++){
const int at = list[i];
for(int j = 1; j < g[at].size(); j++){
const int to = g[at][j].first;
if(!used[to]){
used[to] = 1;
list.push_back(to);
}
}
}
while(list.size() > 1){
const int at = list.back();
if(!circle[at]) chmax(d[g[at][0].first], d[at] + g[at][0].second);
list.pop_back();
}
ll max = Diameter(at);
vector<ll> len(circle_size * 2 + 1);
rep(circle_size * 2){
const int to = g[at][0].first;
len[i + 1] = len[i] + g[at][0].second;
at = to;
}
SlidMax sw;
rep(circle_size - 1){
at = g[at][0].first;
sw.push(len[i + 1] + d[at]);
}
at = g[at][0].first;
rep(circle_size){
chmax(max, sw.pop() - len[i] + d[at]);
sw.push(len[circle_size + i] + d[at]);
at = g[at][0].first;
}
ans += max;
}
cout << ans << endl;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:97:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < list.size(); i++){
~~^~~~~~~~~~~~~
islands.cpp:99:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < g[at].size(); j++){
~~^~~~~~~~~~~~~~
islands.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
39424 KB |
Output is correct |
2 |
Correct |
28 ms |
39552 KB |
Output is correct |
3 |
Correct |
27 ms |
39424 KB |
Output is correct |
4 |
Correct |
29 ms |
39424 KB |
Output is correct |
5 |
Correct |
28 ms |
39424 KB |
Output is correct |
6 |
Correct |
28 ms |
39424 KB |
Output is correct |
7 |
Correct |
28 ms |
39424 KB |
Output is correct |
8 |
Correct |
27 ms |
39424 KB |
Output is correct |
9 |
Correct |
27 ms |
39424 KB |
Output is correct |
10 |
Correct |
29 ms |
39424 KB |
Output is correct |
11 |
Correct |
28 ms |
39552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
39552 KB |
Output is correct |
2 |
Correct |
31 ms |
39552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
39552 KB |
Output is correct |
2 |
Correct |
33 ms |
39808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
40320 KB |
Output is correct |
2 |
Correct |
56 ms |
41972 KB |
Output is correct |
3 |
Correct |
42 ms |
40832 KB |
Output is correct |
4 |
Correct |
37 ms |
40056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
42868 KB |
Output is correct |
2 |
Correct |
76 ms |
44664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
50920 KB |
Output is correct |
2 |
Correct |
148 ms |
50276 KB |
Output is correct |
3 |
Correct |
188 ms |
54264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
59768 KB |
Output is correct |
2 |
Correct |
308 ms |
74596 KB |
Output is correct |
3 |
Correct |
315 ms |
67748 KB |
Output is correct |
4 |
Correct |
380 ms |
82936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
462 ms |
87984 KB |
Output is correct |
2 |
Correct |
1258 ms |
98524 KB |
Output is correct |
3 |
Correct |
444 ms |
83576 KB |
Output is correct |
4 |
Correct |
589 ms |
95764 KB |
Output is correct |
5 |
Correct |
577 ms |
104156 KB |
Output is correct |
6 |
Correct |
1696 ms |
91584 KB |
Output is correct |
7 |
Correct |
575 ms |
99576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
512 ms |
83680 KB |
Output is correct |
2 |
Correct |
495 ms |
83892 KB |
Output is correct |
3 |
Correct |
557 ms |
92384 KB |
Output is correct |
4 |
Correct |
729 ms |
72696 KB |
Output is correct |
5 |
Correct |
536 ms |
97448 KB |
Output is correct |
6 |
Correct |
634 ms |
92944 KB |
Output is correct |
7 |
Correct |
1666 ms |
92636 KB |
Output is correct |