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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include <bits/stdc++.h> using namespace std;
const int N=4e4+5; struct Edge { int go,next; } eg[N<<1]; int last[N],tot,e[N][3]; int num[N],son[N],dep[N],fa[N],pos; int top[N],p[N],fp[N]; int col[N];
void init() { tot=0;pos=0; memset(last,-1,sizeof(last)); memset(son,-1,sizeof(son)); }
void addedge(int x,int y) { eg[tot]={y,last[x]}; last[x]=tot++; }
void dfs(int u,int pre,int d) { dep[u]=d; fa[u]=pre; num[u]=1; for (int i=last[u];i!=-1;i=eg[i].next) { int &v=eg[i].go; if (v!=pre) { dfs(v,u,d+1); num[u]+=num[v]; if (son[u]==-1||num[v]>num[son[u]]) { son[u]=v; } } } }
void getpos(int u,int sp) { top[u]=sp; p[u]=++pos; fp[p[u]]=u; if (son[u]==-1) return; getpos(son[u],sp); for (int i=last[u];i!=-1;i=eg[i].next) { int &v=eg[i].go; if (v!=son[u]&&v!=fa[u]) { getpos(v,v); } } }
struct SegTree { struct Node { int lc,rc,cnt; Node(int a=-1,int b=0,int c=0):lc(a),rc(b),cnt(c) {} Node operator +(const Node &R) const { if (!cnt) return R; if (!R.cnt) return *this; return {lc,R.rc,cnt+R.cnt-(rc==R.lc)}; } Node rev() { return {rc,lc,cnt}; } void print() { printf("%d %d %d\n",lc,rc,cnt); } } tr[N<<2]; int flag[N<<2]; #define lson rt<<1,l,m #define rson rt<<1|1,m+1,r void push_up(int rt) { tr[rt]=tr[rt<<1]+tr[rt<<1|1]; } void push_down(int rt) { if (flag[rt]!=-1) { flag[rt<<1]=flag[rt<<1|1]=flag[rt]; tr[rt<<1]=tr[rt<<1|1]={flag[rt],flag[rt],1}; flag[rt]=-1; } } void build(int rt,int l,int r) { flag[rt]=-1; if (l==r) { tr[rt]={col[l],col[l],1}; return; } int m=(l+r)>>1; build(lson); build(rson); push_up(rt); } void update(int rt,int l,int r,int L,int R,int col) { if (L<=l&&r<=R) { tr[rt]={col,col,1}; flag[rt]=col; return; } push_down(rt); int m=(l+r)>>1; if (L<=m) update(lson,L,R,col); if (m<R) update(rson,L,R,col); push_up(rt); } Node getsum(int rt,int l,int r,int L,int R) { if (L<=l&&r<=R) { return tr[rt]; } push_down(rt); int m=(l+r)>>1; Node res; if (L<=m) res=res+getsum(lson,L,R); if (m<R) res=res+getsum(rson,L,R); return res; } void change(int x,int y,int col) { int tx=top[x],ty=top[y]; while (tx!=ty) { if (dep[tx]<dep[ty]) { swap(tx,ty); swap(x,y); } update(1,1,pos,p[tx],p[x],col); x=fa[tx]; tx=top[x]; } if (x==y) return; if (dep[x]>dep[y]) { swap(x,y); } update(1,1,pos,p[son[x]],p[y],col); } int query(int x,int y) { int tx=top[x],ty=top[y]; Node X,Y; while (tx!=ty) { if (dep[tx]<dep[ty]) { Y=getsum(1,1,pos,p[ty],p[y])+Y; y=fa[ty]; ty=top[y]; } else { X=getsum(1,1,pos,p[tx],p[x])+X; x=fa[tx]; tx=top[x]; } } Node ans; if (x==y) { ans=X.rev()+Y; } else { if (dep[x]>dep[y]) { ans=Y.rev()+getsum(1,1,pos,p[son[y]],p[x])+X; } else { ans=X.rev()+getsum(1,1,pos,p[son[x]],p[y])+Y; } } return ans.cnt; } #undef lson #undef rson } mytree;
int main() { int n,m; while (scanf("%d%d",&n,&m)!=EOF) { init(); for (int i=1;i<n;i++) { scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]); addedge(e[i][0],e[i][1]); addedge(e[i][1],e[i][0]); } dfs(1,0,0); getpos(1,1); for (int i=1;i<n;i++) { if (dep[e[i][0]]<dep[e[i][1]]) { swap(e[i][0],e[i][1]); } col[p[e[i][0]]]=e[i][2]; } mytree.build(1,1,pos); while (m--) { char op[9]; int u,v,w; scanf("%s%d%d",op,&u,&v); if (op[0]=='C') { scanf("%d",&w); mytree.change(u,v,w); } else { printf("%d\n",mytree.query(u,v)); } } } return 0; }
|