Description to build up a communications system on n isolated planets. This communication system is a tree that connects N points. The edge of the tree is added by a single line. At some point, the load on an edge is the number of simple paths that are passing through it on the currently connected tree. For example, in, there are now altogether 5 sides. where (3,8) the load on this side is 6, because there are six simple paths 2-3-8,2-3-8-7,3-8,3-8-7,4-3-8,4-3-8-7 passing through (3,8). Now, your task is to dynamically answer the question about the load on some side as you add the side. The first line of input contains two integer n,q, representing the number of planets and the number of operations. The planets numbered starting from 1. The next Q line, each line is one of the following two formats: A x y means an edge is attached between x and Y. Ensure that the X and Y are not connected before. Q x y indicates the load on the edge of the query (x, y). Ensure that there is an edge between x and Y. 1≤n,q≤100000output for each query operation, output the load of the side being queried. Sample Input8 6
A 2 3
A 3 4
A 3 8
A 8 7
A 6 5
Q 3 8Sample Output6
HINT Source
Acknowledgement anonymous Upload
Data structure simple question, we find the answer is min (Siz[x],siz[y]) * (Size[x]-min (Siz[x],siz[y])), where siz[x] is the size of the root tree with X, Size[x] is the size of the tree where x is located. Add an edge x-y when the heuristic is merged, set X to the size of the smaller one, the x is located in the tree violence refactoring, with a dynamic tree to the path of Y to its root all siz values plus size[x]. The time complexity is still O (Mlogn).
#include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #define LC ch[x][0]# Define RC Ch[x][1] #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for ( int i=first[x];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char buffer[buffersize],*head,*tail; inline char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin); tail= (Head=buffer) +l;} return *head++;} inline int read () {int X=0,f=1;char c=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} typedef long LONG Ll;const int Maxn=100010;int n,m,pa[maxn],siz[maxn],ch[maxn][2],s[maxn],add[maxn],pre[maxn],fa[ Maxn];int findset (int x) {return x==pa[x]?x:pa[x]=findset (pa[x]);} void Add (int x,int v) {if (x) add[x]+=v,s[x]+=v;} void pushdown (int x) {if (Add[x]) Add (Lc,add[x]), add (Rc,add[x]), add[x]=0;} void rotate (int x) {int y=pre[x],z=pre[y],d=ch[y][0]==x;ch[y][d^1]=ch[x][d];p re[ch[x][d]]=y;ch[z][ch[z][1]==y]=x;pre[x]=z;ch[x][d]=y;pre[y]=x;} int st[maxn],top;void splay (int x) {for (int i=x;i;i=pre[i]) st[++top]=i;if (top!=1) fa[x]=fa[st[top]],fa[st[top]]=0; while (top) pushdown (st[top--]), while (pre[x]) rotate (x);} void access (int x) {for (int y=0;x;x=fa[x]) {splay (x);p re[ch[x][1]]=0;fa[ch[x][1]]=x;ch[x][1]=y;pre[y]=x;y=x;}} int first[maxn],next[maxn<<1],to[maxn<<1],e;void Addedge (int u,int v) {To[++e]=v;next[e]=first[u];first [U]=e;to[++e]=u;next[e]=first[v];first[v]=e;} void Dfs (int x,int f) {Fa[x]=f;lc=rc=0;pre[x]=0;s[x]=1;ren if (to[i]!=f) DFS (to[i],x), S[x]+=s[to[i]];} void link (int x,int y) {Addedge (x, y), int f1=findset (×), F2=findset (y), if (Siz[f1]>siz[f2]) swap (F1,F2), swap (x, y);p a [F1]=f2;siz[f2]+=siz[f1];access (y); splay (y); ADD (Y,SIZ[F1]);d FS (x, y);} int query (int x) {splay (x); return s[x];} int main () {n=read (); M=read (); Rep (i,1,n) Siz[i]=s[i]=1,pa[i]=i;rep (i,1,m) {char c=getchar (); while (!isalpha (c)) c= Getchar (); if (c== ' A ') Link (read (), read ()); else {iNT X=read (), Y=read (), int sum=min (query (y), query (x));p rintf ("%lld\n", (LL) sum* (Siz[findset (x)]-sum));}} return 0;}
BZOJ4530: [Bjoi2014] Great fusion