题解:
毒瘤题......首先bfs跑一遍图,生成一棵树,则只有一条边没有用上而且这条边链接的两个点在该树上的深度差为<=1,即深度差为-1,0,1三种情况(因为深度差大于2的话,bfs就会选这跳边当做树边了)。
做完bfs序后就可以直接上线段树了,因为每一层的数刚好是连续了,分情况讨论,k=0,k=1的情况比较好写,但k=2的时候情况很多。。。。
- 特殊边的点跟查找的u相连。然后分深度-1,0,1三种情况。
- 特殊边的点跟查找的u的父亲节点相连。然后分深度-1,0,1三种情况。
- 特殊边的点跟查找的u儿子节点相连。然后分深度-1,0,1三种情况。
总之要考虑重复的情况比较多。
#include<bits/stdc++.h>
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
ll sum[maxn<<2],mark[maxn<<2];
vector <int > G[maxn];
int fa[maxn],pos[maxn];
int head,tail,q[maxn];
int minl[maxn],maxr[maxn];
int n,m,x,y,posx,posy;
bool vis[maxn];
char s[21];
void bfs()
{
memset(vis,0,sizeof(vis));
head = 0; tail = 1;
q[tail] = 1;
pos[1] = tail;
fa[0] = fa[1] = 0;
vis[1] = 1;
while(head<tail){
int u = q[++head];
minl[u] = tail+1;
for(int v : G[u]){
if(!vis[v]){
vis[v] = 1;
fa[v] = u;
q[++tail] = v;
pos[v] = tail;
}
}
maxr[u] = tail;
}
minl[0] = 1; maxr[0] = 0;
}
void getcircle()
{
x = y = 0;
for(int i = 1; i <= n; i++){
for(int v : G[i]){
if(pos[v] < pos[i] && pos[v] != pos[fa[i]]){
x = i;
posx = pos[x];
y = v;
posy = pos[y];
return;
}
}
}
}
void pushup(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt, int len)
{
if(!mark[rt]) return;
sum[rt<<1] += mark[rt]*(len - (len>>1) );
sum[rt<<1|1] += mark[rt]*(len>>1);
mark[rt<<1] += mark[rt];
mark[rt<<1|1] += mark[rt];
mark[rt] = 0;
}
void update(int L, int R, int c, int l, int r, int rt)
{
if(L > R) return;
if(L <= l && r <= R){
sum[rt] += (r-l+1)*c;
mark[rt] += c;
return;
}
pushdown(rt,r-l+1);
int mid = (l+r)>>1;
if(L <= mid) update(L,R,c,lson);
if(mid < R) update(L,R,c,rson);
pushup(rt);
}
ll query(int L, int R, int l, int r, int rt)
{
if(L>R) return 0;
if(L <= l && r <= R){
return sum[rt];
}
pushdown(rt,r-l+1);
int mid = (l+r)>>1;
ll ret = 0;
if(L <= mid) ret += query(L,R,lson);
if(mid < R) ret += query(L,R,rson);
return ret;
}
void work_update(int u, int k, int val)
{
if(k == 0){
update(pos[u],pos[u],val,1,n,1);
}
else if( k == 1){
int f = fa[u];
update(pos[u],pos[u],val,1,n,1);
if(f) update(pos[f],pos[f],val,1,n,1);
update(minl[u],maxr[u],val,1,n,1);
if(u == x) update(pos[y],pos[y],val,1,n,1);
if(u == y) update(pos[x],pos[x],val,1,n,1);
}
else{
int f = fa[u],ff = 0,sonl = 0,sonr = 0;
if(f) update(pos[f],pos[f],val,1,n,1);
update(minl[u],maxr[u],val,1,n,1);
ff = fa[f];
if(minl[u] <= maxr[u]){
sonl = q[minl[u]];
sonr = q[maxr[u]];
}
if(ff) update(pos[ff],pos[ff],val,1,n,1);
if(f) update(minl[f],maxr[f],val,1,n,1);
if(!f) update(pos[u],pos[u],val,1,n,1);
if(sonl && sonr) update(minl[sonl],maxr[sonr],val,1,n,1);
if(f == x) update(pos[y],pos[y],val,1,n,1);
if(f == y) update(pos[x],pos[x],val,1,n,1);
if(u == x ){
if(fa[y]!=f){
update(pos[y],pos[y],val,1,n,1);
if(fa[y]!=ff && (pos[fa[y]] < minl[f] || pos[fa[y]] > maxr[f])) update(pos[fa[y]],pos[fa[y]],val,1,n,1);
}
update(minl[y],maxr[y],val,1,n,1);
}
if(u == y){
if(fa[x] != f){
update(pos[x],pos[x],val,1,n,1);
if(fa[x]!=ff && (pos[fa[x]] < minl[f] || pos[fa[x]] > maxr[f])) update(pos[fa[x]],pos[fa[x]],val,1,n,1);
}
update(minl[x],maxr[x],val,1,n,1);
}
if(minl[u] <= posx && posx <= maxr[u] && (posy < minl[u] || posy > maxr[u]) && (posy < minl[f] || posy > maxr[f]) && (posy < minl[sonl] || posy > maxr[sonr]))
update(pos[y],pos[y],val,1,n,1);
if(minl[u] <= posy && posy <= maxr[u] && (posx < minl[u] || posx > maxr[u]) && (posx < minl[f] || posx > maxr[f]) && (posx < minl[sonl] || posx > maxr[sonr]))
update(pos[x],pos[x],val,1,n,1);
}
}
void work_query(int u,int k)
{
ll ans = 0;
if(k == 0){
ans += query(pos[u],pos[u],1,n,1);
}
else if( k == 1){
int f = fa[u];
ans += query(pos[u],pos[u],1,n,1);
if(f) ans += query(pos[f],pos[f],1,n,1);
ans += query(minl[u],maxr[u],1,n,1);
if(u == x) ans +=query(pos[y],pos[y],1,n,1);
if(u == y) ans +=query(pos[x],pos[x],1,n,1);
}
else{
int f = fa[u], ff = 0, sonl = 0, sonr = 0;
if(f) ans += query(pos[f],pos[f],1,n,1);
ans += query(minl[u],maxr[u],1,n,1);
ff = fa[f];
if(minl[u] <= maxr[u]){
sonl = q[minl[u]];
sonr = q[maxr[u]];
}
if(ff) ans += query(pos[ff],pos[ff],1,n,1);
if(f) ans += query(minl[f],maxr[f],1,n,1);
if(!f) ans += query(pos[u],pos[u],1,n,1);
if(sonl && sonr) ans += query(minl[sonl],maxr[sonr],1,n,1);
if(f == x) ans += query(pos[y],pos[y],1,n,1);
if(f == y) ans += query(pos[x],pos[x],1,n,1);
if(u == x ){
if(fa[y]!=f){
ans += query(pos[y],pos[y],1,n,1);
if(fa[y]!=ff && (pos[fa[y]] < minl[f] || pos[fa[y]] > maxr[f])) ans += query(pos[fa[y]],pos[fa[y]],1,n,1);
}
ans += query(minl[y],maxr[y],1,n,1);
}
if(u == y ){
if(fa[x]!=f){
ans += query(pos[x],pos[x],1,n,1);
if(fa[x]!=ff && (pos[fa[x]] < minl[f] || pos[fa[x]] > maxr[f])) ans += query(pos[fa[x]],pos[fa[x]],1,n,1);
}
ans += query(minl[x],maxr[x],1,n,1);
}
if(minl[u] <= posx && posx <= maxr[u] && (posy < minl[u] || posy > maxr[u]) && (posy < minl[f] || posy > maxr[f]) && (posy < minl[sonl] || posy > maxr[sonr]))
ans += query(pos[y],pos[y],1,n,1);
if(minl[u] <= posy && posy <= maxr[u] && (posx < minl[u] || posx > maxr[u]) && (posx < minl[f] || posx > maxr[f]) && (posx < minl[sonl] || posx > maxr[sonr]))
ans += query(pos[x],pos[x],1,n,1);
}
printf("%lld\n",ans);
}
void work()
{
int u,k,val;
memset(mark,0,sizeof(mark));
memset(sum,0,sizeof(sum));
scanf("%d",&m);
for(int i = 1; i <= m; i++){
scanf("%s",s);
if(s[0] == 'M'){
scanf("%d%d%d",&u,&k,&val);
work_update(u,k,val);
}
else{
scanf("%d%d",&u,&k);
work_query(u,k);
}
}
}
void solve()
{
bfs();
getcircle();
work();
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T;
scanf("%d",&T);
while(T--){
int u,v;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
G[i].clear();
for(int i = 1; i <= n; i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
solve();
}
return 0;
}