题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3779
丰富下博客的文章
其实是看着commonc学长的题解写的
我就不说什么了つ﹏⊂ 学长讲的非常棒
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
#include<bits/stdc++.h> #define MAXN 100010 typedef double db; using namespace std; inline unsigned int rd(){ unsigned int x=0,y=1;char c=getchar(); while(!isdigit(c)){if(c=='-')y=-y;c=getchar();} while(isdigit(c))x=x*10+c-'0',c=getchar(); return x*y; } struct node{ unsigned int l,r,len; unsigned int sum,lazy; }tree[MAXN*4]; unsigned int n,m; vector<unsigned int>v[MAXN]; unsigned int sz[MAXN],hson[MAXN],ffa[MAXN],deep[MAXN],tp[MAXN],tot,dfn[MAXN],fan[MAXN]; unsigned int fa[MAXN],son[MAXN][2],L[MAXN],R[MAXN],root=1,rev[MAXN]; void dfs1(unsigned int x){ sz[x]=1; for(unsigned int i=0; i<v[x].size(); i++){ unsigned int to=v[x][i]; if(to==fa[x])continue; deep[to]=deep[x]+1; ffa[to]=fa[to]=x; dfs1(to); sz[x]+=sz[to]; if(sz[to]>sz[hson[x]])hson[x]=to; } } void dfs2(unsigned int x,unsigned int tt){ tot++; dfn[x]=tot; fan[tot]=x; tp[x]=tt; if(hson[x])dfs2(hson[x],tt); for(unsigned int i=0; i<v[x].size(); i++){ unsigned int to=v[x][i]; if(to==fa[x] || to==hson[x])continue; dfs2(to,to); } } inline void pushup1(unsigned int x){ tree[x].sum=tree[x*2].sum+tree[x*2+1].sum; } inline void pushdown1(unsigned int x){ if(!tree[x].lazy)return; tree[x*2].lazy+=tree[x].lazy; tree[x*2+1].lazy+=tree[x].lazy; tree[x*2].sum+=tree[x*2].len*tree[x].lazy; tree[x*2+1].sum+=tree[x*2+1].len*tree[x].lazy; tree[x].lazy=0; } void build(unsigned int x,unsigned int l,unsigned int r){ tree[x].l=l; tree[x].r=r; tree[x].len=r-l+1; if(l==r){ tree[x].sum=deep[fan[l]]; return; } unsigned int mid=(l+r)/2; build(x*2,l,mid); build(x*2+1,mid+1,r); pushup1(x); } unsigned int Get(unsigned int x,unsigned int y){ while(tp[x]!=tp[y]){ if(ffa[tp[y]]==x)return tp[y]; y=ffa[tp[y]]; } return fan[dfn[x]+1]; } void change(unsigned int x,unsigned int l,unsigned int r,unsigned int k){ if(tree[x].l==l && tree[x].r==r){ tree[x].lazy+=k; tree[x].sum+=tree[x].len*k; return; } pushdown1(x); unsigned int mid=(tree[x].l+tree[x].r)/2; if(r<=mid)change(x*2,l,r,k); else if(l>mid)change(x*2+1,l,r,k); else change(x*2,l,mid,k),change(x*2+1,mid+1,r,k); pushup1(x); } void Change(unsigned int x,unsigned int k){ if(x==root)change(1,1,n,k); else if(dfn[x]<=dfn[root] && dfn[x]+sz[x]>=dfn[root]+sz[root]){ x=Get(x,root); if(dfn[x]>1)change(1,1,dfn[x]-1,k); if(dfn[x]+sz[x]-1<n)change(1,dfn[x]+sz[x],n,k); } else change(1,dfn[x],dfn[x]+sz[x]-1,k); } void pushup(unsigned int x){ if(!son[x][0])L[x]=x; else L[x]=L[son[x][0]]; if(!son[x][1])R[x]=x; else R[x]=R[son[x][1]]; } void Rev(unsigned int x){ if(!x)return; swap(son[x][0],son[x][1]); swap(L[x],R[x]); rev[x]^=1; } void pushdown(unsigned int x){ if(rev[x]){ Rev(son[x][0]); Rev(son[x][1]); rev[x]=false; } } bool isroot(unsigned int x){ return son[fa[x]][0]!=x && son[fa[x]][1]!=x; } inline bool is_right(unsigned int x){ return son[fa[x]][1]==x; } inline void Rotate(unsigned int x){ unsigned int f=fa[x],ff=fa[f],which=is_right(x); if(!isroot(f))son[ff][son[ff][1]==f]=x; son[f][which]=son[x][which^1]; fa[son[f][which]]=f;fa[f]=x; son[x][which^1]=f;fa[x]=ff; pushup(f);pushup(x); } void getdown(unsigned int x){ if(!isroot(x))getdown(fa[x]); pushdown(x); } inline void splay(unsigned int x){ getdown(x); while(!isroot(x)){ unsigned int f=fa[x]; if(!isroot(f))Rotate((is_right(x)==is_right(f)?f:x)); Rotate(x); } } void access(unsigned int x){ unsigned int t=0; while(x){ splay(x); if(son[x][1])Change(L[son[x][1]],1); son[x][1]=t; if(t)Change(L[t],-1); pushup(x); t=x; x=fa[x]; } } void makeroot(unsigned int x){ access(x); splay(x); swap(son[x][0],son[x][1]); swap(L[x],R[x]); rev[x]^=1; } unsigned int query(unsigned int x,unsigned int l,unsigned int r){ if(tree[x].l==l && tree[x].r==r)return (unsigned int)tree[x].sum; pushdown1(x); unsigned int mid=(tree[x].l+tree[x].r)/2; if(r<=mid)return query(x*2,l,r); else if(l>mid)return query(x*2+1,l,r); else return query(x*2,l,mid)+query(x*2+1,mid+1,r); } db Query(unsigned int x){ db ret=0; if(x==root)return query(1,1,n)*1.0/n; else if(dfn[x]<=dfn[root] && dfn[x]+sz[x]>=dfn[root]+sz[root]){ x=Get(x,root); if(dfn[x]>1)ret+=query(1,1,dfn[x]-1); if(dfn[x]+sz[x]-1<n)ret+=query(1,dfn[x]+sz[x],n); return ret*1.0/(n-sz[x]); } else return query(1,dfn[x],dfn[x]+sz[x]-1)*1.0/sz[x]; } int main(){ unsigned int i,x,y; n=rd(),m=rd(); for(i=1; i<n; i++){ x=rd(),y=rd(); v[x].push_back(y); v[y].push_back(x); } dfs1(1); dfs2(1,1); for(i=1; i<=n; i++)L[i]=R[i]=i; build(1,1,n); char op[21]; while(m--){ scanf("%s",op); x=rd(); if(op[2]=='L')access(x); else if(op[2]=='C')makeroot(x),root=x; else printf("%.10lf\n",Query(x)+1.0); } return 0; } |