이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<pair<int, ll>>> adj;
vector<pair<int, int>> pa;
vector<pair<int, ll>> dist;
void init(int n, int q, ll l, vector<int> u, vector<int> v, vector<ll> w) {
pa.resize(n-1);
adj.resize(n);
for(int i = 0; i < n-1; i++) {
u[i]--; v[i]--;
pa[i] = {u[i], v[i]};
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
for(int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end());
dist.resize(n);
}
void dfs(int n, ll dis = 0, int par = -1) {
dist[n] = {dis, n};
for(auto ch : adj[n]) {
if(ch.first == par) continue;
dfs(ch.first, dis + ch.second, n);
}
}
ll get_diameter(int d, ll e) {
int x, y;
tie(x, y) = pa[d];
int l = 0, r = adj[x].size();
while(l != r-1) {
int mid = (l+r)/2;
if(adj[x][mid].first <= y) l = mid;
else r = mid;
}
adj[x][l].second = e;
l = 0; r = adj[y].size();
while(l != r-1) {
int mid = (l+r)/2;
if(adj[y][mid].first <= x) l = mid;
else r = mid;
}
adj[y][l].second = e;
dfs(0);
sort(dist.rbegin(), dist.rend());
dfs(dist[0].second);
sort(dist.rbegin(), dist.rend());
return dist[0].first;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
ll l;
cin >> n >> q >> l;
vector<int> u(n-1), v(n-1);
vector<ll> w(n-1);
for(int i = 0; i < n-1; i++) {
cin >> u[i] >> v[i] >> w[i];
}
init(n, q, l, u, v, w);
ll last = 0;
for(int i = 0; i < q; i++) {
ll d, e;
cin >> d >> e;
d += last;
d %= n-1;
e += last;
e %= l;
last = get_diameter(d, e);
cout << last << '\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... |