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 mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
vector<vii> adj;
ii f;
void dfs(int s, int p=-1, int d=0){
if(d>f.F) f = max(f, {d, s});
FOA(v, adj[s]){
if(v.F!=p) dfs(v.F, s, d+v.S);
}
}
int main(){
int q;
int w;
cin>>n>>q>>w;
adj.assign(n+1, vii());
vector<pair<ii,ii> > edg(n-1);
FOR(i,n-1){
int a, b, c;
cin>>a>>b>>c;
edg[i] = {{a, adj[a].size()}, {b, adj[b].size()}};
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
int last=0;
while(q--){
int a, b;
cin>>a>>b;
a= (a + last) % (n-1);
b = (b+last) % w;
adj[edg[a].F.F][edg[a].F.S].S = b;
adj[edg[a].S.F][edg[a].S.S].S = b;
f = {0,1};
dfs(1);
dfs(f.S);
last = f.F;
cout<<last<<endl;
}
}
# | 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... |