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
const int maxn = 1e5;
struct edge {
int to, wei;
edge() : to(0), wei(0) {}
edge(int to_, int wei_) :
to(to_), wei(wei_) {}
};
int n, q, w;
edge edges[2 * (maxn - 1)];
vector<edge* > g[maxn + 1];
void dfs(int cur, vector<bool> &vis, vector<int> &dist) {
vis[cur] = true;
for(edge* e : g[cur]) {
if(!vis[e->to]) {
dist[e->to] = dist[cur] + (e->wei);
dfs(e->to, vis, dist);
}
}
}
int query() {
int res = 0;
vector<bool> vis(1 + n, false);
vector<int> dist(1 + n, 0);
dfs(1, vis, dist);
int next_start = 1;
for(int i = 1; i <= n; i++) {
if(dist[i] > dist[next_start]) {
next_start = i;
}
}
vis.assign(1 + n, false);
dist.assign(1 + n, 0);
dfs(next_start, vis, dist);
for(int i = 1; i <= n; i++) {
res = max(res, dist[i]);
}
return res;
}
void solve() {
cin >> n >> q >> w;
for(int i = 0; i < n - 1; i++) {
int idx_1 = 2 * i, idx_2 = 2 * i + 1;
int a, b, c;
cin >> a >> b >> c;
edges[idx_1] = edge(b, c);
edges[idx_2] = edge(a, c);
g[a].pb(edges + idx_1);
g[b].pb(edges + idx_2);
}
int last = 0;
while(q--) {
int d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
// ----- changable -----
int idx_1 = 2 * d, idx_2 = 2 * d + 1;
edges[idx_1].wei = e;
edges[idx_2].wei = e;
int result = query();
// ----- changable -----
cout << result << "\n";
last = result;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | 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... |