Submission #1054668

#TimeUsernameProblemLanguageResultExecution timeMemory
1054668VMaksimoski008Dynamic Diameter (CEOI19_diameter)C++17
24 / 100
5082 ms21668 KiB
#include <bits/stdc++.h> //#define int long long using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 5; ll n, W; int d[maxn]; ll val[maxn], dp[maxn][3]; vector<pll> gshit[maxn]; vector<int> graph[maxn]; void dfs1(int u, int p) { for(auto &[v, w] : gshit[u]) { if(v == p) continue; d[v] = d[u] + 1; val[v] = w; dfs1(v, u); } } void dfs(int u, int p) { ll mx1=0, mx2=0; for(int &v : graph[u]) { if(v == p) continue; dfs(v, u); dp[u][0] = max(dp[u][0], dp[v][0] + val[v]); dp[u][2] = max(dp[u][2], dp[v][2]); if(dp[v][0] + val[v] >= mx1) { mx2 = mx1; mx1 = dp[v][0] + val[v]; } else if(dp[v][0] + val[v] >= mx2) { mx2 = dp[v][0] + val[v]; } } dp[u][1] = mx1 + mx2; dp[u][2] = max(dp[u][2], max(dp[u][0], dp[u][1])); } signed main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int q; cin >> n >> q >> W; vector<array<ll, 3> > edges; for(int i=0; i<n-1; i++) { ll a, b, w; cin >> a >> b >> w; edges.push_back({ a, b, w }); gshit[a].push_back({ b, w }); gshit[b].push_back({ a, w }); graph[a].push_back(b); graph[b].push_back(a); } dfs1(1, 1); for(auto &[a, b, w] : edges) if(d[a] > d[b]) swap(a, b); ll last = 0; while(q--) { ll d, e; cin >> d >> e; d = (d + last) % (n - 1); e = (e + last) % W; val[edges[d][1]] = e; for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 0; dfs(1, 1); cout << dp[1][2] << '\n'; last = dp[1][2]; } 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...