#include #include #include #include using namespace std; typedef long long ll; const int maxn = 1e5+10; #define pii pair #define fi first #define se second #define mp make_pair int N, M, P; int color[maxn]; struct edge { int v,next,w; edge() {} edge(int v,int next,int w):v(v),next(next),w(w) {} }E[maxn<<1]; int h[maxn], edge_cnt, pos_cnt; int deep[maxn], fa[maxn][20], top[maxn], son[maxn], size[maxn], pos[maxn]; int L, R; inline void add_edge(int a,int b,int c) { E[edge_cnt]=edge(b,h[a],c); h[a]=edge_cnt++; E[edge_cnt]=edge(a,h[b],c); h[b]=edge_cnt++; } void init() { memset(h,-1,sizeof h); edge_cnt = pos_cnt = 0; } int Q[maxn]; void split() { int r=0; Q[r++]=1; fa[1][0]=0; for(int i=0;i0;i--) { int v=Q[i], u=fa[v][0]; size[u]+=size[v]; if(size[v]>size[son[u]]) son[u]=v; } for(int i=0;ir) return; L=l; R=r; update(1,1,N,c); } int query(int o,int l,int r) { pushdown(o,l,r); if(L<=l&&r<=R) { return value[o]; } int m=(l+r)/2; int ret=0; if(L<=m) ret+=query(o<<1,l,m); if(mdeep[v]) swap(u,v); update(pos[u]+1,pos[v],c); } inline int lca(int u,int v) { if(deep[u]=0;i--) { if(fa[u][i]!=fa[v][i]) { u=fa[u][i]; v=fa[v][i]; } } return fa[u][0]; } inline int find_next_root(int u,int r) { if(u==r) return -1; int c=deep[u]-deep[r]-1; // printf("find_next_root %d\n", c); for(int i=0;i<20;i++) if(c&(1<deep[v]) { ret+=query(pos[top[u]],pos[u]); u=top[u]; ret-=(query(pos[u])==query(pos[fa[u][0]])); u=fa[u][0]; } return ret+query(pos[v],pos[u]); } int main(int argc,char *argv[]) { if(argc>1) { freopen( argv[1], "r", stdin ); } while(cin>>N>>P) { init(); // for(int i=1;i<=N;i++) scanf("%d", color+i); for(int i=1;i