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>
#define tizoboz ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define int long long
#define itn int
#define ss set <int>
#define prq priority_queue <int>
#define endl '\n'
const ll MOD = 1e9 + 7;
#define md(x) (((x%MOD)+MOD)%MOD)
#define vi vector <int>
#define vl vector<ll>
#define str string
#define mata int32_t
#define sz size
#define lc id *2
#define rc lc +1
#define SZ(x) (int)x.size()
#define mid (l+r)/2
#define cn cin
#define ct cout
#define sep " "
#define F first
#define X first
#define S second
#define Y second
using namespace std;
typedef pair <int , int> pii;
const int maxn = 5e3 + 10;
ll N , Q , vis [maxn] , dis [maxn] , W , zzz;
vector <ll> g [maxn];
vector < pair <int , itn > > yal;
int r[maxn][maxn];
void dfs(int i,int p){
for(auto u:g[i]){
if(p==u){
continue;
}
dis[u]=dis[i]+r[u][i];
dfs(u,i);
}
if(dis[i]>dis[zzz]){
zzz=i;
}
}
void solve (){
cn >> N >> Q >> W;
for (int i = 1 ; i < N ; i ++){
int x1 , x2 , x3;
cn >> x1 >> x2 >> x3;
r[x1][x2]=x3;
r[x2][x1]=x3;
yal.pb({x1,x2});
g[x1].pb(x2);
g[x2].pb(x1);
}
ll lastAns = 0;
ll ja = 0;
while (Q--){
ll d,e;
cin>>d>>e;
d= ( d + lastAns) % (N-1);
e = ( lastAns + e) % W;
ll u = yal[d].F;
ll v = yal[d].S;
r[v][u] = e;
dis[1] = 0 ;
r[u][v] = e;
zzz=0;
dfs(1 ,-1);
dis[zzz ] = 0;
dfs( zzz, -1);
ct << dis[zzz] << endl;
lastAns = dis [zzz ];
}
}
mata main(){
tizoboz;
int tt = 1;
// cn >> tt;
while (tt--){
solve();
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'void solve()':
diameter.cpp:59:8: warning: unused variable 'ja' [-Wunused-variable]
59 | ll ja = 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... |