BZOJ4817: [Sdoi2017]树点涂色

来源:互联网 发布:灵肉结合知乎 编辑:程序博客网 时间:2024/05/29 17:16

Description

Bob有一棵n个点的有根树,其中1号点是根节点。Bob在每个点上涂了颜色,并且每个点上的颜色不同。定义一条路
径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色。Bob可能会进行这几种操作:
1 x:
把点x到根节点的路径上所有的点染上一种没有用过的新颜色。
2 x y:
求x到y的路径的权值。
3 x y:
在以x为根的子树中选择一个点,使得这个点到根节点的路径权值最大,求最大权值。
Bob一共会进行m次操作

Input

第一行两个数n,m。
接下来n-1行,每行两个数a,b,表示a与b之间有一条边。
接下来m行,表示操作,格式见题目描述
1<=n,m<=100000

Output

每当出现2,3操作,输出一行。
如果是2操作,输出一个数表示路径的权值
如果是3操作,输出一个数表示权值的最大值

Sample Input

5 6
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5

Sample Output

3
4
2
2

HINT

Source

LCT
我们把相同颜色的点之间的边认为是preferred path
发现第一个操作就是access
然后用链剖线段树维护区间最值即可
#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define fill( x, y ) memset( x, y, sizeof x )#define copy( x, y ) memcpy( x, y, sizeof x )using namespace std;typedef long long LL;typedef pair < int, int > pa;inline int read(){int sc = 0; char ch = getchar();while( ch < '0' || ch > '9' ) ch = getchar();while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();return sc;}const int MAXN = 100010;struct edge { int to, nxt; } e[MAXN << 1];int n, m, head[MAXN], e_cnt, f[MAXN], dep[MAXN], fa[MAXN], ch[MAXN][2];int sz[MAXN], bl[MAXN], dfn[MAXN], tim, rl[MAXN], mx[MAXN << 2], tag[MAXN << 2];inline void add(int x, int y) { e[ ++e_cnt ] = { y, head[ x ] }; head[ x ] = e_cnt; }inline void addedge(int x, int y) { add( x, y ); add( y, x ); }inline void dfs(int x){sz[ x ] = 1;for( int i = head[ x ] ; i ; i = e[ i ].nxt ) if( e[ i ].to ^ f[ x ] )f[ e[ i ].to ] = x, dep[ e[ i ].to ] = dep[ x ] + 1, dfs( e[ i ].to ), sz[ x ] += sz[ e[ i ].to ];}inline void dfs2(int x, int chain){bl[ rl[ dfn[ x ] = ++tim ] = x ] = chain; int k = 0;for( int i = head[ x ] ; i ; i = e[ i ].nxt ) if( e[ i ].to != f[ x ] && sz[ e[ i ].to ] > sz[ k ] ) k = e[ i ].to;if( !k ) return ;dfs2( k, chain );for( int i = head[ x ] ; i ; i = e[ i ].nxt ) if( e[ i ].to != f[ x ] && e[ i ].to != k ) dfs2( e[ i ].to, e[ i ].to );}inline int lca(int x, int y){for( ; bl[ x ] ^ bl[ y ] ; x = f[ bl[ x ] ] )if( dep[ bl[ x ] ] < dep[ bl[ y ] ] ) swap( x, y );return ( dep[ x ] < dep[ y ] ) ? x : y;}inline void pushup(int x) { mx[ x ] = max( mx[ x << 1 ], mx[ x << 1 | 1 ] ) + tag[ x ]; }inline void build(int x, int l, int r){if( l == r ) { mx[ x ] = dep[ rl[ l ] ]; return ; }int mid = l + r >> 1; build( x << 1, l, mid ); build( x << 1 | 1, mid + 1, r );pushup( x );}inline void modify(int x, int l, int r, int ql, int qr, int v){if( l == ql && r == qr ) { tag[ x ] += v; mx[ x ] += v; return ; }int mid = l + r >> 1;if( qr <= mid ) modify( x << 1, l, mid, ql, qr, v );else if( ql > mid ) modify( x << 1 | 1, mid + 1, r, ql, qr, v );else modify( x << 1, l, mid, ql, mid, v ), modify( x << 1 | 1, mid + 1, r, mid + 1, qr, v );pushup( x );}inline int query(int x, int l, int r, int ql, int qr){if( l == ql && r == qr ) return mx[ x ];int mid = l + r >> 1;if( qr <= mid ) return query( x << 1, l, mid, ql, qr ) + tag[ x ];if( ql > mid ) return query( x << 1 | 1, mid + 1, r, ql, qr ) + tag[ x ];return max( query( x << 1, l, mid, ql, mid ), query( x << 1 | 1, mid + 1, r, mid + 1, qr ) ) + tag[ x ];}inline bool isroot(int x) { return ch[ fa[ x ] ][ 0 ] != x && ch[ fa[ x ] ][ 1 ] != x; }inline void rotate(int x){int y = fa[ x ], z = fa[ y ], k = ch[ y ][ 1 ] == x;if( !isroot( y ) ) ch[ z ][ ch[ z ][ 1 ] == y ] = x;fa[ ch[ y ][ k ] = ch[ x ][ k ^ 1 ] ] = y;fa[ fa[ ch[ x ][ k ^ 1 ] = y ] = x ] = z;}inline void splay(int x){for( int y = fa[ x ] ; !isroot( x ) ; rotate( x ), y = fa[ x ] ) if( !isroot( y ) )rotate( ( ( ch[ y ][ 1 ] == x ) == ( ch[ fa[ y ] ][ 1 ] == y ) ) ? y : x );}inline void access(int x){for( int t = 0 ; x ; t = x, x = fa[ x ] ){splay( x );if( ch[ x ][ 1 ] ){int cur = ch[ x ][ 1 ];while( ch[ cur ][ 0 ] ) cur = ch[ cur ][ 0 ];modify( 1, 1, n, dfn[ cur ], dfn[ cur ] + sz[ cur ] - 1, 1 );}ch[ x ][ 1 ] = t;if( ch[ x ][ 1 ] ){int cur = ch[ x ][ 1 ];while( ch[ cur ][ 0 ] ) cur = ch[ cur ][ 0 ];modify( 1, 1, n, dfn[ cur ], dfn[ cur ] + sz[ cur ] - 1, -1 );}}}int main(){#ifdef wxh010910freopen( "data.in", "r", stdin );#endifn = read(), m = read();for( int i = 1 ; i < n ; i++ ) addedge( read(), read() );dep[ 1 ] = 1; dfs( 1 ); dfs2( 1, 1 ); build( 1, 1, n );for( int i = 1 ; i <= n ; i++ ) fa[ i ] = f[ i ];while( m-- ){int opt = read();if( opt == 1 ) access( read() );else if( opt == 2 ){int x = read(), y = read(), k = lca( x, y );printf( "%d\n", query( 1, 1, n, dfn[ x ], dfn[ x ] ) + query( 1, 1, n, dfn[ y ], dfn[ y ] ) - 2 * query( 1, 1, n, dfn[ k ], dfn[ k ] ) + 1 );}else{int x = read();printf( "%d\n", query( 1, 1, n, dfn[ x ], dfn[ x ] + sz[ x ] - 1 ) );}}}


0 0
原创粉丝点击