이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// subtask12
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int MAX_N = 5000 + 1;
vector <pair <int, ll>> adj[MAX_N];
struct edge {
int a, b;
ll w;
};
edge edges[MAX_N];
int node;
ll max_dis;
int N, Q;
ll W;
void dfs(int s, int p, ll dis) {
if (dis > max_dis) {
node = s;
max_dis = dis;
}
for (auto i : adj[s]) {
if (i.first == p)
continue;
dfs(i.first, s, dis + i.second);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> Q >> W;
for (int i = 0; i < N-1; i++) {
int a, b;
ll w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
edge tp;
tp.a = a;
tp.b = b;
tp.w = w;
edges[i] = tp;
}
for (int i = 1; i <= N; i++)
sort(adj[i].begin(), adj[i].end());
ll last = 0;
while (Q--) {
ll d;
ll e;
cin >> d >> e;
d = (d + last) % (N-1);
e = (e + last) % (W);
int a = edges[d].a, b = edges[d].b;
pair <int, ll> tp = {b, 0};
auto x = lower_bound(adj[a].begin(), adj[a].end(), tp);
x->second = e;
tp = {a, 0};
x = lower_bound(adj[b].begin(), adj[b].end(), tp);
x->second = e;
edges[d].w = e;
max_dis = 0;
dfs(1, 1, 0);
max_dis = 0;
dfs(node, node, 0);
last = max_dis;
cout << max_dis << '\n';
}
}
# | 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... |