1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 2000006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; int A[MAXN]; vi G[MAXN];
int L[MAXN] , R[MAXN] , clo; int g[MAXN][19] , dep[MAXN]; void dfs( int u , int f ) { L[u] = ++ clo; for( int v : G[u] ) if( v != f ) { dep[v] = dep[u] + 1; g[v][0] = u; rep( k , 1 , 18 ) if( g[g[v][k-1]][k-1] ) g[v][k] = g[g[v][k-1]][k-1]; else break; dfs( v , u ); } R[u] = clo; }
int work( int u , int v ) { per( k , 18 , 0 ) if( dep[g[u][k]] > dep[v] ) u = g[u][k]; return u; }
struct mdf { int x , l , r , c; } Q[MAXN] ; int cnt = 0;
bool cmp( mdf a , mdf b ) { return a.x < b.x; }
int T[MAXN << 2] , lz[MAXN << 2]; inline void pu( int rt ) { T[rt] = max( T[rt << 1] , T[rt << 1 | 1] ); } inline void ad( int rt , int c ) { T[rt] += c , lz[rt] += c; } inline void pd( int rt ) { if( lz[rt] ) { ad( rt << 1 , lz[rt] ) , ad( rt << 1 | 1 , lz[rt] ); lz[rt] = 0; } } void add( int rt , int l , int r , int L , int R , int c ) { if( L <= l && R >= r ) { ad( rt , c ); return; } pd( rt ); int m = l + r >> 1; if( L <= m ) add( rt << 1 , l , m , L , R , c ); if( R > m ) add( rt << 1 | 1 , m + 1 , r , L , R , c ); pu( rt ); }
int ori[MAXN]; void afs( int u , int fa ) { if( ori[u] ) { int w = ori[u]; Q[++ cnt] = (mdf) { 1 , L[u] , L[u] , w }; Q[++ cnt] = (mdf) { L[u] + 1 , L[u] , L[u] , -w }; Q[++ cnt] = (mdf) { R[u] + 1 , L[u] , L[u] , w }; Q[++ cnt] = (mdf) { L[u] , 1 , L[u] - 1 , w }; Q[++ cnt] = (mdf) { L[u] + 1 , 1 , L[u] - 1 , -w }; Q[++ cnt] = (mdf) { L[u] , R[u] + 1 , n , w }; Q[++ cnt] = (mdf) { L[u] + 1 , R[u] + 1 , n , -w }; } int rr = 0; for( int v : G[u] ) if( v != fa ) { if( ori[u] ) { int w = ori[u]; Q[++ cnt] = (mdf) { 1 , L[v] , R[v] , w }; Q[++ cnt] = (mdf) { L[v] , L[v] , R[v] , -w }; Q[++ cnt] = (mdf) { R[u] + 1 , L[v] , R[v] , w }; Q[++ cnt] = (mdf) { L[v] , 1 , L[v] - 1 , w }; Q[++ cnt] = (mdf) { R[v] + 1 , 1 , L[v] - 1 , -w }; Q[++ cnt] = (mdf) { L[v] , R[u] + 1 , n , w }; Q[++ cnt] = (mdf) { R[v] + 1 , R[u] + 1 , n , -w }; } afs( v , u ); rr = R[v]; } }
void solve() {
cin >> n >> m; rep( i , 2 , n ) { static int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } dep[1] = 1 , dfs( 1 , 1 ); rep( i , 1 , m ) { static int u , v , w; scanf("%d%d%d",&u,&v,&w); if( u == v ) { ori[u] += w; continue; } if( L[u] > R[v] || L[v] > R[u] ) { Q[++ cnt] = (mdf) { L[v] , L[u] , R[u] , w }; Q[++ cnt] = (mdf) { R[v] + 1 , L[u] , R[u] , -w }; } else { if( L[u] < L[v] ) { int k = work( v , u ); Q[++ cnt] = (mdf) { L[v] , 1 , L[k] - 1 , w }; Q[++ cnt] = (mdf) { R[v] + 1 , 1 , L[k] - 1 , -w }; Q[++ cnt] = (mdf) { L[v] , R[k] + 1 , n , w }; Q[++ cnt] = (mdf) { R[v] + 1 , R[k] + 1 , n , -w }; } else { int k = work( u , v ); Q[++ cnt] = (mdf) { 1 , L[u] , R[u] , w }; Q[++ cnt] = (mdf) { L[k] , L[u] , R[u] , -w }; Q[++ cnt] = (mdf) { R[k] + 1 , L[u] , R[u] , w }; } } } afs( 1 , 1 ); sort( Q + 1 , Q + 1 + cnt , cmp ); int cur = 1 , res = 0; rep( x , 1 , n ) { while( cur <= cnt && Q[cur].x == x ) { add( 1 , 0 , n + 1 , Q[cur].l , Q[cur].r , Q[cur].c ); ++ cur; } res = max( res , T[1] ); } cout << res << endl; }
signed main() {
solve(); }
|