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
const int N = 1e5 + 5;
vector<pair<pair<int , int> , int> > edge;
vector<pair<int , int> > g[N];
int level[N] , d , e , n , q , w , last;
void dfs(int ver , int par){
for(auto i : g[ver]){
int uu = i.first , ww = i.second;
if(uu == par) continue;
level[uu] = level[ver] + ww;
dfs(uu , ver);
}
}
void cal(){
for(int i = 1 ; i <= n ; i++) g[i].clear();
for(auto i : edge){
int U = i.first.first , V = i.first.second , W = i.second;
g[U].push_back({V , W});
g[V].push_back({U , W});
}
dfs(1 , 0);
int mx = 0 , vr = 1;
for(int i = 1 ; i <= n ; i++){
if(mx < level[i]){
mx = level[i];
vr = i;
}
}
level[vr] = 0;
dfs(vr , 0);
mx = 0;
for(int i = 1 ; i <= n ; i++)
mx = max(mx , level[i]);
cout << mx << '\n';
last = mx;
return;
}
void solve1(){
while(q--){
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
pair<pair<int , int> , int> p;
p = edge[d];
p.second = e;
swap(edge[d] , p);
cal();
}
}
int32_t main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> q >> w;
for(int i = 0 ; i < n - 1 ; i++){
int U , V , W;
cin >> U >> V >> W;
g[U].push_back({V , W});
g[V].push_back({U , W});
edge.push_back({{U , V} , W});
}
if(n <= 5000 && q <= 5000){
solve1();
return 0;
}
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... |