제출 #359301

#제출 시각아이디문제언어결과실행 시간메모리
359301jesus_coconutDynamic Diameter (CEOI19_diameter)C++17
24 / 100
5057 ms17516 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, q; ll w; vector<map<int, int>> adj; vector<pair<int, int>> edges; bool s3 = true; void read() { cin >> n >> q >> w; adj.resize(n); edges.resize(n - 1); for (int i = 0; i < n - 1; ++i) { int a, b; ll c; cin >> a >> b >> c; --a; --b; if (a != 0) s3 = false; edges[i] = {a, b}; adj[a][b] = c; adj[b][a] = c; } } void solve3() { vector<set<ll>::iterator> v(n - 1); set<ll, greater<>> s; for (int i = 0; i < n - 1; ++i) { v[i] = s.insert(adj[edges[i].first][edges[i].second]).first; } ll last = 0; while (q--) { int dd; ll ee; cin >> dd >> ee; int d = (dd + last) % (n - 1); ll e = (ee + last) % w; adj[edges[d].first][edges[d].second] = e; adj[edges[d].second][edges[d].first] = e; s.erase(v[d]); v[d] = s.insert(e).first; last = *s.begin() + *(++s.begin()); cout << last << '\n'; } } ll mxcost; int far; void dfs(int ver, int par, ll cost) { if (cost > mxcost) { mxcost = cost; far = ver; } for (auto &[u, c] : adj[ver]) if (u != par) { dfs(u, ver, cost + c); } } ll diameter() { mxcost = 0; far = 0; dfs(0, -1, 0); mxcost = 0; dfs(far, -1, 0); return mxcost; } void solve() { ll last = 0; while (q--) { int dd; ll ee; cin >> dd >> ee; int d = ((ll)dd + last) % (n - 1); ll e = (ee + last) % w; adj[edges[d].first][edges[d].second] = e; adj[edges[d].second][edges[d].first] = e; last = diameter(); cout << last << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read(); if (s3) { solve3(); } else { solve(); } 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...