This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) (int)v.size()
const int INF = 1e18;
const int mod = 998244353;
const int N = 1e5+5;
int p[N], d[N], dp[N], _res[N];
map<pair<int,int>,int> w;
vector<int> g[N], order;
multiset<int> ans;
void dfs(int v){
for(auto to : g[v]){
if(to != p[v]){
p[to] = v;
d[to] = d[v] + 1;
dfs(to);
}
}
order.pb(v);
}
void solve(){
int n, q, wmax;
cin >> n >> q >> wmax;
vector<pair<int,int> > edges(n + 1);
for(int i = 0; i < n - 1; i++){
int u, v, wi;
cin >> u >> v >> wi;
g[u].pb(v);
g[v].pb(u);
w[{u, v}] = wi;
w[{v, u}] = wi;
edges[i] = {u, v};
}
dfs(1);
for(int v : order){
for(auto to : g[v]){
if(to != p[v]){
dp[v] = max(dp[v], dp[to] + w[{v, to}]);
_res[v] += dp[to] + w[{v, to}];
}
}
ans.insert(_res[v]);
}
int last = 0;
while(q--){
int ind, val;
cin >> ind >> val;
ind = (ind + last) % (n - 1);
val = (val + last) % wmax;
auto [u, v] = edges[ind];
if(d[u] < d[v]) swap(u, v);
w[{u, v}] = val;
w[{v, u}] = val;
while(v){
int res = 0;
dp[v] = 0;
for(auto to : g[v]){
if(to != p[v]){
dp[v] = max(dp[v], dp[to] + w[{v, to}]);
res += dp[to] + w[{v, to}];
}
}
ans.erase(ans.find(_res[v]));
ans.insert(res);
_res[v] = res;
v = p[v];
}
last = *ans.rbegin();
cout << last << '\n';
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message (stderr)
diameter.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |