This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mp make_pair
#define mid (l+r)/2
#define le node * 2
#define ri node * 2 + 1
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
#define gc getchar_unlocked
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef pair<double,ll>pdi;
const ll inf=1e18;
const ll Mod=1e9+7;
const ld Pi=acos(-1) ;
int n , q , MX ;
vpi v [100009] ;
pi edges [100009] ;
//
void input () {
cin >> n >> q >> MX ;
for ( int i = 0 ; i < n - 1 ; i ++ ) {
int a , b , c ;
scanf ("%d%d%d", &a , &b , &c ) ;
a -- , b -- ;
edges [i] = { a , b } ;
v [a] .pb ( { b , c } ) ;
v [b] .pb ( { a , c } ) ;
}
}
// Sub1_2
pi Far ;
int Sub1_2 () {
return ( n <= 5009 && q <= 5009 ) ;
}
void Dfs1 ( int node , int p , int Crnt ) {
Far = max ( Far , { Crnt , node } ) ;
for ( auto u : v [node] ) {
if ( u .fi == p ) C ;
Dfs1 ( u .fi , node , Crnt + u .se ) ;
}
}
int BruteForce () {
Far = { 0 , 0 } ;
Dfs1 ( 0 , 0 , 0 ) ;
Dfs1 ( Far .se , Far .se , 0 ) ;
return Far .fi ;
}
void Sol1_2 () {
int Last = 0 ;
while ( q -- ) {
int id , cost ;
cin >> id >> cost ;
id = ( id + Last ) % ( n - 1 ) ;
cost = ( cost + Last ) % ( MX ) ;
int a = edges [id] .fi , b = edges [id] .se ;
for ( auto &u : v [a] ) {
if ( u .fi == b ) {
u .se = cost ;
break ;
}
}
for ( auto &u : v [b] ) {
if ( u .fi == a ) {
u .se = cost ;
break ;
}
}
Last = BruteForce () ;
cout << Last << endl ;
}
}
// Sub4
int Dp [100009] , Mx [100009] , P [100009] ;
int Sub4 () {
for ( int i = 0 ; i < n - 1 ; i ++ ) {
int a = edges [i] .fi , b = edges [i] .se ;
if ( a > b ) {
swap ( edges [i] .fi , edges [i] .se ) ;
}
a ++ , b ++ ;
if ( a * 2 != b && a * 2 + 1 != b ) return 0 ;
}
return 1 ;
}
void Fill ( int node , int p ) {
P [node] = p ;
for ( auto u : v [node] ) {
if ( u .fi == p ) C ;
Fill ( u .fi , node ) ;
Dp [node] = max ( Dp [node] , Dp [u.fi] ) ;
Dp [node] = max ( Dp [node] , Mx [u.fi] + u .se + Mx [node] ) ;
Mx [node] = max ( Mx [node] , Mx [u.fi] + u .se ) ;
}
Dp [node] = max ( Dp [node] , Mx [node] ) ;
}
void Up ( int node ) {
Dp [node] = 0 , Mx [node] = 0 ;
for ( auto u : v [node] ) {
if ( u .fi == P [node] ) C ;
Dp [node] = max ( Dp [node] , Dp [u.fi] ) ;
Dp [node] = max ( Dp [node] , Mx [u.fi] + u .se + Mx [node] ) ;
Mx [node] = max ( Mx [node] , Mx [u.fi] + u .se ) ;
}
if ( P [node] != node )
Up ( P [node] ) ;
}
void Sol4 () {
Fill ( 0 , 0 ) ;
int Last = 0 ;
while ( q -- ) {
int id , cost ;
cin >> id >> cost ;
id = ( id + Last ) % ( n - 1 ) ;
cost = ( cost + Last ) % ( MX ) ;
int a = edges [id] .fi , b = edges [id] .se ;
for ( auto &u : v [a] ) {
if ( u .fi == b ) {
u .se = cost ;
break ;
}
}
for ( auto &u : v [b] ) {
if ( u .fi == a ) {
u .se = cost ;
break ;
}
}
Up ( a ) ;
Last = Dp [0] ;
cout << Dp [0] << endl ;
}
}
int main () {
input () ;
if ( Sub1_2 () ) Sol1_2 () ;
if ( Sub4 () ) Sol4 () ;
// if ( Sub3_5 () ) Sol3_5 () ;
}
Compilation message (stderr)
diameter.cpp: In function 'void input()':
diameter.cpp:43:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
43 | scanf ("%d%d%d", &a , &b , &c ) ;
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |