Submission #126749

#TimeUsernameProblemLanguageResultExecution timeMemory
126749groeneprofIslands (IOI08_islands)C++14
80 / 100
1748 ms131076 KiB
#include <bits/stdc++.h> #define P(x) do {if(debug) cout << x << endl;} while(false) #define H(x) P(#x << ": " << x) #define FR(i, a, b) for(int i = (a); i < (b); ++i) #define F(i, n) FR(i, 0, n) #define DR(i, a, b) for(int i = (b); i --> (a);) #define D(i, n) DR(i, 0, n) #define S(s) (int)(s).size() #define ALL(x) (x).begin(), (x).end() #define MI(x, a) (x) = min(x, a) #define MA(x, a) (x) = max(x, a) #define V vector #define pb push_back #define mp make_pair using namespace std; const bool debug = 0; const long long inf = 1e18; struct edge { int u, v, w; bool operator== (edge o){ return u == o.u && v == o.v && w == o.w; } bool operator!= (edge o){ return !(*this == o); } }; struct sliding_maximum { deque<pair<long long, int> > qu; int i, l; sliding_maximum(int _l){ i = 0; l = _l; } long long getmax(){ if(qu.empty()) return -inf; return qu.front().first; } void slide(long long newval){ while(!qu.empty()&&newval > qu.back().first){ qu.pop_back(); } qu.push_back({newval,i}); if(qu.front().second<=i-l){ qu.pop_front(); } i++; } }; int N; vector<edge> edgelist; //12 MB vector<vector<int> > graph; //8 MB (16 MB worst case)? vector<bool> vis; //1 MB bool flag = false; int dfs(int id, vector<int>& cycle){ edge p = edgelist[id]; H(p.u); H(p.v); H(p.w); //F(ii, 1e8); if(vis[p.v]&&flag){ P("return 1"); return -1; } if(vis[p.v]){ flag = true; P("return 2"); P("cyclee: "<<p.v); H(id); cycle.push_back(id); return p.v; } vis[p.v] = true; int ce = -1; for(int i : graph[p.v]) if(id != (i^1)) { int a = dfs(i, cycle); if(a!=-1) ce = a; } if(ce == p.v){ //cycle.push_back(cycle[0]); P("return 3"); return -1; } if(ce != -1){ P("cycle: "<<p.v); H(id); cycle.push_back(id); } P("return 4: "<<ce); return ce; } int f1, f2; long long _maxu, _maxd; void dfs2(int id, long long d){ //F(i, 1e8); //edge p = edgelist[id]; //H(p.u); H(p.v); H(p.w); //H(id); H(f1); H(f2); if(d>_maxd){ _maxu = edgelist[id].v; _maxd = d; P("update (maxu, maxd) to"); H(_maxu); H(_maxd); } //int d = 0; for(int i : graph[edgelist[id].v]) if(i-i%2 != id-id%2 && i-i%2 != f1-f1%2 && i-i%2 != f2-f2%2){ dfs2(i, d+edgelist[i].w); } //return d+p.w; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); //input cin>>N; edgelist.resize(2*N+1); graph.resize(N); F(u, N){ int v,w; cin>>v>>w; v--; edgelist[2*u] = {u, v, w}; graph[u].push_back(2*u); edgelist[2*u+1] = {v, u, w}; graph[v].push_back(2*u+1); } //loop to calculate everything vis.resize(N, 0); edgelist[2*N] = {-1, 0, 0}; long long sum = 0; F(t, N) if(!vis[t]){ P("--------------------------------------------"); H(t); long long maxtot = 0; vector<int> cycle; //4MB flag = false; edgelist[2*N].v = t; dfs(2*N, cycle); int c = cycle.size(); reverse(cycle.begin(), cycle.end()); //(0,1), (1,2), ..., (c-1, 0) P("step 1"); for(int i : cycle){ edge p = edgelist[i]; P("cycle edge "<<i); H(p.u); H(p.v); H(p.w); } vector<long long> d(c, 0); //8MB F(i, c){ H(i); edge p = edgelist[cycle[i]]; f1 = cycle[(i-1+c)%c]; f2 = cycle[i]; edgelist[2*N].v = p.u; //long long maxu = -1, maxd = -1; P("root: "<<p.u); _maxu = -1; _maxd = -1; dfs2(2*N, 0); d[i] = _maxd; //long long maxu2 = -1, maxd2 = -1; edgelist[2*N].v = _maxu; _maxu = -1; _maxd = -1; //P("root2: "<<maxu); dfs2(2*N, 0); maxtot = max(maxtot, _maxd); } H(maxtot); P("step 2"); //vector<long long> pref(c*2+1, 0); long long csum = 0; F(i, 2*c-1){ //pref[i+1] = pref[i] + edgelist[cycle[i%c]].w; csum = csum + edgelist[cycle[i%c]].w; } //csum == pref(c*2-1) P("step 3"); sliding_maximum sm(c-1); H(c); for(int i = 2*c-1; i>=0; i--){ H(i); maxtot = max((long long)maxtot, sm.getmax()-csum+d[i%c]); sm.slide(csum+d[i%c]); csum -= edgelist[cycle[(i+c-1)%c]].w; } P("step 4"); H(maxtot); sum += maxtot; } cout<<sum<<endl; return 0; }
#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...