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;
vector<int> g[N];
map<pair<int,int>,int> w;
void solve(){
int n, qr, maxw;
cin >> n >> qr >> maxw;
vector<pair<int,int> > edges;
for(int i = 1; i < n; i++){
int u, v, iw;
cin >> u >> v >> iw;
g[u].pb(v);
g[v].pb(u);
w[{u, v}] = iw;
w[{v, u}] = iw;
edges.pb({u, v});
}
int last = 0;
while(qr--){
int ind, val;
cin >> ind >> val;
ind = (ind + last) % (n - 1);
val = (val + last) % maxw;
auto [x, y] = edges[ind];
w[{x, y}] = val;
w[{y, x}] = val;
vector<int> dis(n + 1, INF);
dis[1] = 0;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
q.push({0, 1});
while(!q.empty()){
auto [disv, v] = q.top();
q.pop();
if(disv > dis[v]) continue;
for(auto to : g[v]){
if(dis[v] + w[{v, to}] < dis[to]){
dis[to] = dis[v] + w[{v, to}];
q.push({dis[to], to});
}
}
}
dis[0] = 0;
int mx = max_element(all(dis)) - dis.begin();
dis = vector<int>(n + 1, INF);
dis[mx] = 0;
q.push({0, mx});
while(!q.empty()){
auto [disv, v] = q.top();
q.pop();
if(disv > dis[v]) continue;
for(auto to : g[v]){
if(dis[v] + w[{v, to}] < dis[to]){
dis[to] = dis[v] + w[{v, to}];
q.push({dis[to], to});
}
}
}
dis[0] = 0;
last = *max_element(all(dis));
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:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
84 | 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... |