# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
379589 |
2021-03-18T17:43:13 Z |
SavicS |
Islands (IOI08_islands) |
C++14 |
|
546 ms |
131076 KB |
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,ll> pii;
const int maxn = 2000005;
const int inf = 1e9 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n;
vector<pii> g[maxn];
int nxt[maxn];
ll cost[maxn];
int krug_sta = 0;
int krug_end = 0;
int par[maxn];
int visited[maxn];
void dfs1(int v, int p){
par[v] = p;
visited[v] = 1;
for(auto c : g[v]){
int u = c.fi;
ll w = c.se;
if(u == p && !(nxt[p] == v && nxt[v] == p))continue;
if(!visited[u])dfs1(u, v);
else{
if(visited[u] == 1){
krug_sta = u;
krug_end = v;
}
}
}
visited[v] = 2;
}
ll dia = 0;
bool was[maxn];
ll dfs2(int v, ll duz){
was[v] = 1;
ll best = duz;
for(auto c : g[v]){
int u = c.fi;
ll w = c.se;
if(!was[u]){
ll dist = dfs2(u, duz + w);
dia = max(dia, dist + best - 2 * duz);
best = max(best, dist);
}
}
return best;
}
ll rez = 0;
ll pref[maxn];
ll sta[maxn];
void Solve(int x){
dia = 0;
dfs1(x, 0);
vector<int> cycle;
for(int i = krug_end;i != krug_sta; i = par[i]){
cycle.pb(i);
}
cycle.pb(krug_sta);
vector<pii> krug;
ff(i,1,sz(cycle) - 1){
int a = cycle[i - 1];
int b = cycle[i];
ll w = (nxt[a] == b ? cost[a] : cost[b]);
krug.pb({a, w});
}
ll w = (nxt[cycle.back()] == cycle[0] ? cost[cycle.back()] : cost[cycle[0]]);
krug.pb({cycle.back(), w});
for(auto c : krug)was[c.fi] = 1;
ff(i,0,sz(krug) - 1)sta[i] = dfs2(krug[i].fi, 0);
int len = sz(krug);
pref[0] = 0;
ff(i,0,len - 1)pref[i + 1] = pref[i] + krug[i].se;
ll zbir = pref[len];
ll mn = -sta[0];
ll mx = sta[0];
ll najv = 0;
ff(i,1,len - 1){
najv = max(najv, pref[i] + sta[i] - mn);
najv = max(najv, zbir + sta[i] - pref[i] + mx);
mn = min(mn, pref[i] - sta[i]);
mx = max(mx, pref[i] + sta[i]);
}
rez += max(najv, dia);
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n;
ff(i,1,n){
int a;
ll b;
cin >> a >> b;
g[i].pb({a, b});
g[a].pb({i, b});
nxt[i] = a;
cost[i] = b;
}
ff(i,1,n){
if(!visited[i]){
Solve(i);
}
}
cout << rez << endl;
return 0;
}
/**
3
1 1
3 2
2 3
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
// probati bojenje sahovski ili slicno
**/
Compilation message
islands.cpp: In function 'void dfs1(int, int)':
islands.cpp:44:6: warning: unused variable 'w' [-Wunused-variable]
44 | ll w = c.se;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
47340 KB |
Output is correct |
2 |
Correct |
35 ms |
47340 KB |
Output is correct |
3 |
Correct |
32 ms |
47468 KB |
Output is correct |
4 |
Correct |
35 ms |
47340 KB |
Output is correct |
5 |
Correct |
32 ms |
47340 KB |
Output is correct |
6 |
Correct |
32 ms |
47340 KB |
Output is correct |
7 |
Correct |
32 ms |
47340 KB |
Output is correct |
8 |
Correct |
33 ms |
47340 KB |
Output is correct |
9 |
Correct |
32 ms |
47340 KB |
Output is correct |
10 |
Correct |
32 ms |
47360 KB |
Output is correct |
11 |
Correct |
32 ms |
47352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47468 KB |
Output is correct |
2 |
Correct |
36 ms |
47596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
47468 KB |
Output is correct |
2 |
Correct |
38 ms |
47852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
48768 KB |
Output is correct |
2 |
Correct |
50 ms |
51628 KB |
Output is correct |
3 |
Correct |
42 ms |
48876 KB |
Output is correct |
4 |
Correct |
42 ms |
48108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
53092 KB |
Output is correct |
2 |
Correct |
70 ms |
56624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
63716 KB |
Output is correct |
2 |
Correct |
125 ms |
72452 KB |
Output is correct |
3 |
Correct |
155 ms |
80860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
77296 KB |
Output is correct |
2 |
Correct |
229 ms |
103612 KB |
Output is correct |
3 |
Correct |
254 ms |
117588 KB |
Output is correct |
4 |
Runtime error |
298 ms |
131076 KB |
Execution killed with signal 9 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
114868 KB |
Output is correct |
2 |
Runtime error |
546 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
371 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |