제출 #379550

#제출 시각아이디문제언어결과실행 시간메모리
379550SavicSIslands (IOI08_islands)C++14
51 / 100
612 ms131076 KiB
#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 = 1000005; 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]; map<pii,ll> kol; vector<pii> krug; 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(w < 0)continue; // if(u == p)continue; if(!visited[u])dfs1(u, v); else{ if(visited[u] == 1){ int a = v; ll nxt = 0; while(a != u){ nxt = kol[{a, par[a]}]; krug.pb({a, nxt}); a = par[a]; } krug.pb({u, kol[{u, 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 = abs(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; krug.clear(); dfs1(x, -1); 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}); kol[{i, a}] = kol[{a, i}] = b; } ff(i,1,n){ if(!visited[i]){ Solve(i); } } cout << rez << endl; return 0; } /** 7 3 8 7 2 4 2 1 4 1 9 3 4 2 3 // probati bojenje sahovski ili slicno **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...