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;
using ll = long long;
template<typename T>
using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using pi = pair<int,int>;
#define F0R(i,n) for (int i = 0; i < n;i++)
#define FOR(i, j, n) for(int i = j; i < n;i++)
#define ckmin(x,y) x = min(x,y)
#define f first
#define s second
#define pb push_back
#define sor(a,b) min(a,b), max(a,b)
vi dx = {0,1,0,-1};
vi dy = {1,0,-1,0};
V<V<V<pi>>> grid;
V<vi> exists;
V<vb> visited;
V<V<int>> g;
map<pi, int> weights;
vi dp;
void dfs(int x, int p, int d) {
dp[x] = d;
for (auto c : g[x]) {
if (c ==p) continue;
dfs(c,x, d + weights[{sor(c,x)}]);
}
}
int far(int n) {
int sel = 1, dist = 0;
FOR(i, 1, n + 1) {
if (dp[i] > dp[sel]) sel = i;
}
return sel;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, q, cMAX; cin >> n >> q >> cMAX;
g.resize(n + 1);
V<pi> edges(n);
F0R(i, n - 1) {
int a, b, c; cin >> a >> b >> c;
g[a].pb(b);
g[b].pb(a);
edges[i] = {a,b};
weights[{sor(a, b)}] = c;
}
dp.resize(n + 1);
int last = 0;
F0R(i, q) {
int e, c; cin >> e >> c;
e = (e + last) % (n - 1);
c = (last + c) % (cMAX);
auto [a,b] = edges[e];
weights[{sor(a,b)}] = c;
dfs(1, -1, 0);
int pa = far(n);
dfs(pa, -1, 0);
int pa2 = far(n);
int results = dp[pa2];
cout << results << "\n";
last = results;
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'int far(int)':
diameter.cpp:41:18: warning: unused variable 'dist' [-Wunused-variable]
41 | int sel = 1, dist = 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... |