白衣苍狗

动态树(Link-Cut Tree)

动态树(Link-Cut Tree)

增删无权边+询问连通性

增删无权边+询问到根路径长度

例题:bzoj2002

  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
/*
bzoj2002
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
void read(T& w)
{
	char r;int f=1;
	for(r=getchar();r!='-'&&(r<48||r>57);r=getchar());
	if(r=='-')r=getchar(),f=-1;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
}
template<typename T>
void write(T w,char c=0)
{
	if(w<0)w=-w,putchar('-');
	if(w>9)write(w/10);
	putchar(w%10+48);
	if(c)putchar(c);
}
const int maxn=200003;
const int maxm=100003;
int n,m;
int f[maxn],c[maxn][2],size[maxn];
#define lc(k) c[k][0]
#define rc(k) c[k][1]
bool rev[maxn];
bool isroot(int k)
{
	return lc(f[k])!=k&&rc(f[k])!=k;
}
void update(int k)
{
	size[k]=size[lc(k)]+size[rc(k)]+1;
}
void tagdown(int k)
{
	if(rev[k])
	{
		rev[k]^=1,rev[lc(k)]^=1,rev[rc(k)]^=1;
		Swap(lc(k),rc(k));
	}
}
void rotate(int x,int& k)
{
	int y=f[x],z=f[y],l,r;
	l=int(x!=lc(y));
	r=l^1;
	if(y==k)k=x;
	else
	{
		if(y==lc(z))lc(z)=x;
		else rc(z)=x;
	}
	f[x]=z;f[y]=x;f[c[x][r]]=y;
	c[y][l]=c[x][r];c[x][r]=y;
	update(y);update(x);
}
void splay(int x,int& k)
{
	while(x!=k)
	{
		int y=f[x],z=f[y];
		if(y!=k)
		{
			if(x==lc(y) ^ y==lc(z))
			rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
}

int stk[maxn],top;
void Splay(int x)
{
	int rt;
	for(rt=x;!isroot(rt);rt=f[rt])
		stk[++top]=rt;
	for(tagdown(rt);top;--top)
		tagdown(stk[top]);
	splay(x,rt);
}
void access(int x)
{
	for(int t=0;x;)
	{
		Splay(x);
		rc(x)=t;t=x;x=f[x];
	}
}
void makeroot(int x)
{
	access(x),Splay(x),rev[x]^=1;
}
void link(int x,int y)
{
	makeroot(x),f[x]=y;
	Splay(x);//	???
}
void cut(int x,int y)
{
	makeroot(x);
	access(y),Splay(y);
	lc(y)=f[x]=0;
}
int nxt[maxn];
void input()
{
	read(n);
	for(int i=1,k;i<=n;++i)
	{
		read(k);
		nxt[i]=f[i]=i+k;
		if(i+k>n+1)nxt[i]=f[i]=n+1;
		size[i]=1;		
	}
	size[n+1]=1;
}
void solve()
{
	int cmd,x,y;
	for(read(m);m;--m)
	{
		read(cmd);
		if(cmd==1)
		{
			read(x);++x;
			makeroot(n+1);
			access(x),Splay(x);
			write(size[lc(x)],'\n');
		}
		else if(cmd==2)
		{
			read(x),read(y);++x;
			cut(x,nxt[x]);
			if(x+y>n+1)y=n+1-x;
			link(x,x+y);
			nxt[x]=x+y;
		}
	}
}
int main()
{
	freopen("bzoj2002_1.in","r",stdin);
	//freopen("bzoj2002.out","w",stdout);
	input();
	solve();
	return 0;
}

增删无权边+路径加+加路径最大值

例题:hdu4010

  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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
hdu4010
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
bool read(T& w)
{
	char r;int f=1;
	for(r=getchar();r!=-1&&r!='-'&&(r<48||r>57);r=getchar());
	if(r=='-')r=getchar(),f=-1;
	if(r==-1)return false;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
	return true;
}
template<typename T>
void write(T w,char c=0)
{
	if(w<0)w=-w,putchar('-');
	if(w>9)write(w/10);
	putchar(w%10+48);
	if(c)putchar(c);
}
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}

const int maxn=300003;
const int inf=2147483647;
int n,q;
//Splay
int f[maxn],c[maxn][2],maxv[maxn],w[maxn];
bool rev[maxn];
int add[maxn];
#define lc(k) c[k][0]
#define rc(k) c[k][1]
bool isroot(int k)
{
	return lc(f[k])!=k&&rc(f[k])!=k;
}
void update(int k)
{
	maxv[k]=Max(Max(maxv[lc(k)],maxv[rc(k)]),w[k]);
}
void tagdown(int k)
{
	if(rev[k])
	{
		rev[k]^=1,rev[lc(k)]^=1,rev[rc(k)]^=1;
		Swap(lc(k),rc(k));
	}
	if(add[k])
	{
		if(lc(k))add[lc(k)]+=add[k],maxv[lc(k)]+=add[k],w[lc(k)]+=add[k];
		if(rc(k))add[rc(k)]+=add[k],maxv[rc(k)]+=add[k],w[rc(k)]+=add[k];
		add[k]=0;
	}
}
void rotate(int x,int& k)
{
	int y=f[x],z=f[y];
	int l=int(x!=lc(y));
	int r=l^1;
	if(y==k)k=x;
	else
	{
		if(y==lc(z))lc(z)=x;
		else rc(z)=x;
	}
	f[x]=z,f[y]=x,f[c[x][r]]=y;
	c[y][l]=c[x][r],c[x][r]=y;
	update(y),update(x);
}
void splay(int x,int& k)
{
	while(x!=k)
	{
		int y=f[x],z=f[y];
		if(y!=k)
		{
			if(x==lc(y) ^ y==lc(z))
				rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
}
int stk[maxn],top=0;
void Splay(int x)
{
	int rt;
	for(rt=x;!isroot(rt);rt=f[rt])
		stk[++top]=rt;
	for(tagdown(rt);top;--top)tagdown(stk[top]);
	splay(x,rt);
}
void access(int x)
{
	for(int t=0;x;)
	{
		Splay(x);
		rc(x)=t;
		update(x);
		t=x;x=f[x];
	}
}
void mroot(int x)
{
	access(x),Splay(x),rev[x]^=1;
}
void link(int x,int y)
{
	mroot(x),f[x]=y;
	//Splay(x);
}
void cut(int x,int y)
{
	mroot(x),access(y),Splay(y),lc(y)=f[lc(y)]=0;//should not used lc(y)=f[x]=0, because we should cut y and its parent
	update(y);
}

struct EDGE
{
	int to,nxt;
	void init(int too,int nxtt)
	{
		to=too,nxt=nxtt;
	}
}edge[maxn<<1];
int ek=0;
int head[maxn];
void addEdge(int k,int v)
{
	edge[++ek].init(v,head[k]);
	head[k]=ek;
}
void dfs(int k)
{
	for(int i=head[k];i;i=edge[i].nxt)
	{
		int v=edge[i].to;
		if(v!=f[k])
		{
			f[v]=k;
			dfs(v);
		}
	}
}
void Init()
{
	ek=0;
	maxv[0]=-inf;
	for(int i=0;i<=n;++i)
	{
		f[i]=c[i][0]=c[i][1]=0;
		rev[i]=false;
		add[i]=0;
		head[i]=0;
	}
}
void input()
{
	for(int i=1,x,y;i<=n-1;++i)
	{
		read(x),read(y);
		addEdge(x,y);
		addEdge(y,x);
	}
	for(int i=1;i<=n;++i)
	{
		read(w[i]);
		maxv[i]=w[i];
	}
	dfs(1);
}
int findroot(int x)
{
	access(x),Splay(x);
	while(lc(x))x=lc(x);
	return x;
}
inline bool insame(int x,int y)
{
	return findroot(x)==findroot(y);
}
void solve()
{
	int cmd,x,y,ww;
	bool illegal;
	for(read(q);q;--q)
	{
		illegal=false;
		read(cmd);
		if(cmd==1)
		{
			read(x),read(y);
			if(insame(x,y))illegal=true;
			else link(x,y);
		}
		else if(cmd==2)
		{
			read(x),read(y);
			if(x==y||!insame(x,y))illegal=true;
			else cut(x,y);
		}
		else if(cmd==3)
		{
			read(ww),read(x),read(y);
			if(!insame(x,y))illegal=true;
			else
			{
				mroot(x);
				access(y),Splay(y);				
				w[y]+=ww,maxv[y]+=ww,add[y]+=ww;
			}
		}
		else if(cmd==4)
		{
			read(x),read(y);
			if(!insame(x,y))illegal=true;
			else
			{
				mroot(x);
				access(y),Splay(y);
				write(maxv[y],'\n');
			}

		}
		if(illegal)puts("-1");
	}
}
int main()
{
	freopen("hdu4010_1.in","r",stdin);
	//freopen("hdu4010.out","w",stdout);
	while(read(n))
	{
		Init();
		input();
		solve();
		putchar('\n');
	}
	return 0;
}

无保证增删无权边+xor和+单点修改

例题:luogu3690

  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
/*
luogu3690
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
bool read(T& w)
{
	char r;int f=1;
	for(r=getchar();r!=-1&&r!='-'&&(r<48||r>57);r=getchar());
	if(r=='-')r=getchar(),f=-1;
	if(r==-1)return false;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
	return true;
}
template<typename T>
void write(T w,char c=0)
{
	if(w<0)w=-w,putchar('-');
	if(w>9)write(w/10);
	putchar(w%10+48);
	if(c)putchar(c);
}
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}

const int maxn=300003;
const int inf=2147483647;
int n,q;
//Splay
int f[maxn],c[maxn][2],xorv[maxn],w[maxn];
bool rev[maxn];
int add[maxn];
#define lc(k) c[k][0]
#define rc(k) c[k][1]
bool isroot(int k)
{
	return lc(f[k])!=k&&rc(f[k])!=k;
}
void update(int k)
{
	xorv[k]=xorv[lc(k)]^xorv[rc(k)]^w[k];
}
void tagdown(int k)
{
	if(rev[k])
	{
		rev[k]^=1,rev[lc(k)]^=1,rev[rc(k)]^=1;
		Swap(lc(k),rc(k));
	}
}
void rotate(int x,int& k)
{
	int y=f[x],z=f[y];
	int l=int(x!=lc(y));
	int r=l^1;
	if(y==k)k=x;
	else
	{
		if(y==lc(z))lc(z)=x;
		else rc(z)=x;
	}
	f[x]=z,f[y]=x,f[c[x][r]]=y;
	c[y][l]=c[x][r],c[x][r]=y;
	update(y),update(x);
}
void splay(int x,int& k)
{
	while(x!=k)
	{
		int y=f[x],z=f[y];
		if(y!=k)
		{
			if(x==lc(y) ^ y==lc(z))
				rotate(x,k);
			else rotate(y,k);
		}
		rotate(x,k);
	}
}
int stk[maxn],top=0;
void Splay(int x)
{
	int rt;
	for(rt=x;!isroot(rt);rt=f[rt])
		stk[++top]=rt;
	for(tagdown(rt);top;--top)tagdown(stk[top]);
	splay(x,rt);
}
void access(int x)
{
	for(int t=0;x;)
	{
		Splay(x);
		rc(x)=t;
		update(x);
		t=x;x=f[x];
	}
}
void mroot(int x)
{
	access(x),Splay(x),rev[x]^=1;
}
void link(int x,int y)
{
	mroot(x),f[x]=y;
	//Splay(x);
}
void cut(int x,int y)
{
	mroot(x),access(y),Splay(y),lc(y)=f[x]=0;//should not used lc(y)=f[x]=0, because we should cut y and its parent
	update(y);
}


void input()
{
	read(n),read(q);
	for(int i=1;i<=n;++i)
	{
		read(w[i]);
		xorv[i]=w[i];
	}
}
int findroot(int x)
{
	access(x),Splay(x);
	while(lc(x))x=lc(x);
	return x;
}
inline bool insame(int x,int y)
{
	return findroot(x)==findroot(y);
}
bool islinked(int x,int y)
{
	mroot(x);
	access(y),Splay(y);
	return rc(x)==0&&x==lc(y);
}
void solve()
{
	int cmd,x,y,ww;
	while(q--)
	{
		read(cmd),read(x),read(y);
		if(cmd==0)
		{
			mroot(x);
			access(y),Splay(y);
			write(xorv[y],'\n');
		}
		else if(cmd==1)
		{
			if(!insame(x,y))link(x,y);
		}
		else if(cmd==2)
		{
			if(islinked(x,y))cut(x,y);
		}
		else if(cmd==3)
		{
			Splay(x);
			w[x]=y;
			update(x);
		}
	}
}
int main()
{
	freopen("luogu3690_2.in","r",stdin);
	//freopen("luogu3690.out","w",stdout);
	input();
	solve();
	return 0;
}

参考

http://blog.csdn.net/saramanda/article/details/55253627

动态规划——状态压缩

动态规划——状态压缩

描述

将状态用二进制表示,直接使用数组下标。

例题:hdu3311

斯坦纳树+SPFA+状压DP。

  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
/*
hdu3311
斯坦纳树+SPFA+状压DP 

二进制枚举子集:

for (int i = s; i; i = (i - 1) &s)

i - 1使得最后一个1变成0,这个1之后的0全部变成1,
&s将原来是0的位变回0,原来是1的位就不断变成0 
 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
inline void Minto(T& a,const T& b){if(b<a)a=b;}
template<typename T>
bool read(T& w)
{
	char r;int f=1;
	for(r=getchar();r!=-1&&r!='-'&&(r<48||r>57);r=getchar());
	if(r=='-')r=getchar(),f=-1;
	if(r==-1)return false;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
	return true;
}
template<typename T>
void write(T w,char c=0)
{
	if(w<0)w=-w,putchar('-');
	if(w>9)write(w/10);
	putchar(w%10+48);
	if(c)putchar(c);
}
template<typename T,int size>
struct QUE
{
	T ary[size+1];
	int f,t,cnt;
	void clear(){f=t=cnt=0;}
	bool empty(){return cnt==0;}
	void push(const T& w){ary[t++]=w,++cnt;if(t==size)t=0;}
	void pop(){++f,--cnt;if(f==size)f=0;}
	T front(){return ary[f];}
};
const int maxn=13;
const int maxm=1003;
const int maxp=5003;
const int maxs=1<<6;
const int inf=2147483647;
int n,m,p;
struct EDGE
{
	int to,nxt,w;
	void init(int too,int nxtt,int ww)
	{
		to=too,nxt=nxtt,w=ww;
	}
}edge[(maxn+maxm+maxp)<<1];
int ek,head[maxn+maxm];
inline void addEdge(int k,int v,int w)
{
	edge[++ek].init(v,head[k],w);
	head[k]=ek;
}
int full;
void input()
{
	full=(1<<(n+1))-1;
	ek=0;
	memset(head,0,(n+m+1)*sizeof(int));
	for(int i=1,x;i<=n+m;++i)
	{
		read(x);
		addEdge(0,i,x);
		addEdge(i,0,x);
	}
	for(int i=1,a,b,c;i<=p;++i)
	{
		read(a),read(b),read(c);
		addEdge(a,b,c);
		addEdge(b,a,c);
	}
}
int d[maxn+maxm][maxn+maxm];
bool in[maxn+maxm];
QUE<int,maxn+maxm> q;
void SPFA()
{
	for(int k=0;k<=n+m;++k)
	{
		for(int j=0;j<=n+m;++j)
			d[k][j]=inf;
		d[k][k]=0;
		q.clear();
		q.push(k);
		in[k]=true;
		while(!q.empty())
		{
			int ff=q.front();q.pop();in[ff]=false;
			for(int i=head[ff];i;i=edge[i].nxt)
			{
				int v=edge[i].to;
				if(d[k][ff]!=inf&&d[k][ff]+edge[i].w<d[k][v])
				{
					d[k][v]=d[k][ff]+edge[i].w;
					if(!in[v])
						in[v]=true,q.push(v);
				}
			}
		}
	}
}
int f[maxn+maxm][maxs],bit[maxn];
void SteinerTree()
{
	for(int i=0;i<=n+m;++i)
		for(int j=0;j<=full;++j)
			f[i][j]=inf;
	for(int i=0;i<=n;++i)
		bit[i]=1<<i,f[i][bit[i]]=0;
	for(int i=0;i<=n;++i)
		for(int j=0;j<=n+m;++j)
			f[j][bit[i]]=d[i][j];
	for(int i=0;i<=full;++i)//枚举状态 
	if(i&(i-1))
	{
		for(int j=0;j<=n+m;++j)//枚举根 
		{
			for(int k=i;k;k=(k-1)&i)//枚举子集 
				if(f[j][k]!=inf&&f[j][i-k]!=inf)
					Minto(f[j][i],f[j][k]+f[j][i-k]);
		}
		for(int j=0;j<=n+m;++j)//枚举另一个点 
			for(int k=0;k<=n+m;++k)//枚举另一个点 
				if(f[k][i]!=inf&&d[j][k]!=inf)
					Minto(f[j][i],f[k][i]+d[j][k]);
	}
}
int main()
{
	freopen("hdu3311_1.in","r",stdin);
	//freopen("hdu3311.out","w",stdout);
	while(read(n),read(m),read(p))
	{
		input();
		SPFA();
		SteinerTree();
		write(f[0][full],'\n');
	}
	return 0;
}

四分树

四分树

例题:uva297

 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
/*
uva297
the std is wrong, str should be larger
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int t;
const int maxa=33;
const int maxn=1033;
char s[maxn*maxn];
bool pic[maxa][maxa];
int cnt;
void draw(char* s,int& pos,int x,int y,int a)
{
	++pos;
	if(s[pos]=='p')
	{
		draw(s,pos,x,y+a/2,a/2);
		draw(s,pos,x,y,a/2);
		draw(s,pos,x+a/2,y,a/2);
		draw(s,pos,x+a/2,y+a/2,a/2);
	}
	else if(s[pos]=='f')
	{
		for(int i=x;i<=x+a-1;++i)
			for(int j=y;j<=y+a-1;++j)
				if(!pic[i][j])
					pic[i][j]=true,++cnt;
	}
}
int main()
{
	freopen("uva297_2.in","r",stdin);
	freopen("uva297.out","w",stdout);
	for(scanf("%d\n",&t);t;--t)
	{
		memset(pic,0,sizeof(pic));
		cnt=0;
		for(int i=1;i<=2;++i)
		{
			scanf("%s",s);
			int p=-1;
			draw(s,p,1,1,32);
		}
		printf("There are %d black pixels.\n",cnt);
	}
	
	return 0;
}

Prufer序列

Prufer序列

概念

Cayley定理

不同的n节点标号树的数量为n^(n-2)

性质

prufer序列中某个编号出现的次数+1

Prufer转树

例题:poj2568

  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
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T,int size,bool comp(const T& a,const T& b)>
struct HEAP
{
	T ary[size+1];
	int f;
	void clear(){f=0;}
	bool empty(){return f==0;}
	void push(const T& w)
	{
		ary[++f]=w;
		for(int k=f;k!=1&&comp(ary[k],ary[k/2]);k/=2)
			Swap(ary[k],ary[k/2]);
	}
	void pop()
	{
		if(empty())return;
		ary[1]=ary[f--];
		for(int k=1,son=k*2;son<=f;k=son,son=k*2)
		{
			if(son+1<=f&&comp(ary[son+1],ary[son]))
				++son;
			if(comp(ary[son],ary[k]))
				Swap(ary[son],ary[k]);
			else break;
		}
	}
	T top(){return ary[1];}
};
bool cmp(const int& a,const int& b)
{
	return a<b;
}
const int maxn=1003;
int n;
int nxt[maxn],to[maxn];
int ek=0,head[maxn],dge[maxn];
void addEdge(int x,int y)
{
    nxt[++ek] = head[x];
    to[ek] = y;
    head[x] = ek;
}
char s[maxn];
int prufer[maxn],pk;
void getPrufer()
{
	pk=n=0;
	memset(dge,0,sizeof(dge));
    char *p = s;
    while(*p != '\0')
    {
        sscanf(p,"%d",&prufer[++pk]);
       	if(prufer[pk]>n)n=prufer[pk];
        ++p,++p;
        if(prufer[pk]>9)++p;
    }
}
HEAP<int,maxn,cmp> h;
void solve()
{
	memset(head,0,sizeof(head));
	ek=0;
    for(int i = 1; i <= pk; ++i)
        ++dge[prufer[i]];
   	h.clear();
    for(int i = 1; i <= n; ++i)
        if(!dge[i])h.push(i);
    for(int i = 1; i <= pk; ++i)
    {
        int x = prufer[i];
        int y = h.top();h.pop();
        addEdge(x,y);
        addEdge(y,x);
        if(!--dge[x])h.push(x);
    }
}

void output(int k,int f)
{
    if(k != 1)	putchar(' ');
    putchar('(');
    printf("%d",k);
    for(int i = head[k]; i; i = nxt[i])
    {
        if(to[i]!=f)
        	output(to[i],k);
    }
    putchar(')');
}

int main()
{
	freopen("poj2568_1.in","r",stdin);
	//freopen("poj2568.out","w",stdout);
    while(gets(s) != NULL)
    {
		getPrufer();
        solve();
        output(1,0);
        puts("");
        memset(s,0,sizeof(s));
    }
    return 0;
}

树转Prufer

例题:poj2567

最近公共祖先(LCA)

最近公共祖先(LCA)

Tarjan算法

详见Tarjan——强连通分量&最近公共祖先(LCA)

倍增算法

  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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
template<typename T>
void read(T& w)
{
    char r;
    for(r=getchar();r<48||r>57;r=getchar());
    for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
}
template<typename T>
inline void write( T w)
{
    if(w<0)
    {
        putchar('-');
        w=-w;
    }
    if(w>9)
        write(w/10);
    putchar(char(w%10+48));

}
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
const int maxn=500003;
const int maxm=500003;
int n,m,s;
struct EDGE
{
    int to,nxt;
}edge[maxm<<1];
int ek=0;
struct node
{
    int edge1,d,f;
}tree[maxn];
inline void addEdge(int from,int too)
{
    ++ek;
    edge[ek].to=too;
    edge[ek].nxt=tree[from].edge1;
    tree[from].edge1=ek;
}
const int max2=20;
int up[maxn][max2+1];
void dfs(int k)
{
    #define now tree[k]
    for(int i=now.edge1;i;i=edge[i].nxt)
    {
        int v=edge[i].to;
        #define son tree[v]
        if(son.d==0)
        {
            son.d=now.d+1;
            son.f=k;
            dfs(v);
        }
        #undef son
    }
    #undef now
}
void initLCA()
{
    for(int i=1;i<=n;++i)
    {
        up[i][0]=tree[i].f;
    }
    for(int k=1;k<=max2&&((1<<k)<=n);++k)
    {
        for(int i=1;i<=n;++i)
        {
            up[i][k]=up[up[i][k-1]][k-1];
        }
    }
}
int getLCA(int a,int b)
{
    #define ta tree[a]
    #define tb tree[b]
    if(ta.d<tb.d)Swap(a,b);
    for(int k=max2;k>=0;--k)
    {
        if(up[a][k])
        if(tree[up[a][k]].d>=tb.d)
            a=up[a][k];
    }
    if(a==b)return a;
    for(int k=max2;k>=0;--k)
    {
        if(up[a][k]&&up[b][k])
        if(up[a][k]!=up[b][k])
            a=up[a][k],b=up[b][k];
    }
    return ta.f;
}
void input()
{
    read(n),read(m),read(s);
    int x,y;
    for(int i=1;i<=n-1;++i)
    {
        read(x),read(y);
        addEdge(x,y);
        addEdge(y,x);
    }
}
void solve()
{
    int x,y;
    for(int i=1;i<=m;++i)
    {
        read(x),read(y);
            write(getLCA(x,y));
            putchar('\n');
    }
}
int main()
{
    input();
    tree[s].d=1;
    dfs(s);
    initLCA();
    solve();
    return 0;
}

莫队算法

莫队算法

普通莫队

例题:bzoj2038

  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
/*
bzoj2038
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
template<typename T>
void read(T& w)
{
	char r;int f=1;
	for(r=getchar();r!='-'&&(r<48||r>57);r=getchar());
	if(r=='-')r=getchar(),f=-1;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;	
}
template<typename T>
T gcd(T a,T b)
{
	return b==0?a:gcd(b,a%b);
}
const int maxn=50003;
const int maxm=50003;
typedef long long lint;
int n,m,c[maxn],len,pos[maxn];
lint d[maxm];
struct Q
{
	int l,r,id;
	inline void getin(int idd)
	{
		read(l),read(r);
		id=idd;
		d[id]=r-l+1;
		d[id]=d[id]*(d[id]-1);
	}
}q[maxm];
inline bool cmp(const Q& a,const Q& b)
{
	if(pos[a.l]==pos[b.l])return a.r<b.r;
	return a.l<b.l;
}
void input()
{
	read(n),read(m);
	len=int(sqrt(n));
	for(int i=1;i<=n;++i)
		read(c[i]),pos[i]=(i-1)/len+1;;
	for(int i=1;i<=m;++i)
		q[i].getin(i);

	sort(q+1,q+m+1,cmp);
}

int cnt[maxn];
lint ans[maxm];
inline void update(lint& tans,int& s,int add)
{
	tans-=s*(s-1);
	s+=add;
	tans+=s*(s-1);
}
void Mo()
{
	//memset(cnt+1,0,n*sizeof(int));
	/*
	int l=q[1].l,r=q[1].r;	
	for(int k=l;k<=r;++k)
		++cnt[c[k]];
	for(int k=1;k<=n;++k)
		ans[q[1].id]+=cnt[k]*(cnt[k]-1);
	*/
	lint tans=0;
	for(int i=1,l=1,r=0;i<=m;++i)
	{
		for(;l<=q[i].l-1;++l)
			update(tans,cnt[c[l]],-1);
		for(;l>=q[i].l+1;--l)
			update(tans,cnt[c[l-1]],1);
		for(;r<=q[i].r-1;++r)
			update(tans,cnt[c[r+1]],1);
		for(;r>=q[i].r+1;--r)
			update(tans,cnt[c[r]],-1);
		ans[q[i].id]=tans;
	}
}

void output()
{
	for(int i=1;i<=m;++i)
	{
		lint g=gcd(ans[i],d[i]);
		if(!ans[i]||!d[i])ans[i]=0,d[i]=1;
		else ans[i]/=g,d[i]/=g;
		printf("%lld/%lld\n",ans[i],d[i]);
	}
}
int main()
{
	freopen("bzoj2038_t.in","r",stdin);
	freopen("bzoj2038.out","w",stdout);
	input();
	Mo();
	output();
	return 0;
}

带修改莫队

例题:bzoj2120

快速傅里叶变换(FFT)

快速傅里叶变换

多项式乘法

例题:luogu3803

  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
/*
luogu3803
FFT
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
template<typename T>
void read(T& w)
{
	char rev;int f=1;
	for(rev=getchar();(rev<48||rev>57)&&rev!='-';rev=getchar());
	if(rev=='-')rev=getchar(),f=-1;
	for(w=0;rev>=48&&rev<=57;rev=getchar())w=w*10+rev-48;
	w*=f;
}
template<typename T>
void write(T w,char c=0)
{
	if(w<0)putchar('-'),w=-w;
	if(w>9)write(w/10);
	putchar(w%10+48);
	if(c)putchar(c);
}
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
const double pi=3.14159265358979323846264338;
template<typename T>
struct COMPLEX
{
	T a,b;
	COMPLEX(){a=b=0;}
	COMPLEX(T aa,T bb):a(aa),b(bb){}
	T& real(){return a;}
	T& imag(){return b;}
};

template<typename T>
COMPLEX<T> operator+(const COMPLEX<T>& a,const COMPLEX<T>& b){return COMPLEX<T>(a.a+b.a,a.b+b.b);}
template<typename T>
COMPLEX<T> operator-(const COMPLEX<T>& a,const COMPLEX<T>& b){return COMPLEX<T>(a.a-b.a,a.b-b.b);}
template<typename T>
COMPLEX<T> operator*(const COMPLEX<T>& a,const COMPLEX<T>& b){return COMPLEX<T>(a.a*b.a-a.b*b.b,a.a*b.b+a.b*b.a);}
template<typename T>
COMPLEX<T> operator*=(COMPLEX<T>& a,const COMPLEX<T>& b){a=COMPLEX<T>(a.a*b.a-a.b*b.b,a.a*b.b+a.b*b.a);}
typedef COMPLEX<double> C;
const int maxn=2*int(1e6+3e5);
int n,m,l,lg,rev[maxn];
C a[maxn],b[maxn],ans[maxn];

void FFT(C* A,int f=1)
{
	for(int i=0;i<=lg-1;++i)
		if(i<rev[i])Swap(A[i],A[rev[i]]);
	for(int i=1;i<=lg-1;i<<=1)
	{
		C wn(cos(pi/i),f*sin(pi/i));
		for(int p=i<<1,j=0;j<=lg-1;j+=p)
		{
			C w(1,0);
			for(int k=0;k<=i-1;++k,w*=wn)
			{
				C u=A[j+k],v=w*A[j+k+i];
				A[j+k]=u+v;
				A[j+k+i]=u-v;
			}
		}
	}
}
#define IFFT(A) FFT(A,-1)
void input()
{
	read(n),read(m);
	for(int i=0;i<=n;++i)read(a[i].real());
	for(int i=0;i<=m;++i)read(b[i].real());
}
void initRev()
{
	for(lg=1;lg<=n+m;lg<<=1)++l;//l=log2(n+m)+1,n=2^l
	for(int i=0;i<=lg-1;++i)
		rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));//¶þ½øÖÆÄæÐòת»» 
}
void solve()
{
	FFT(a),FFT(b);
	for(int i=0;i<=lg;++i)
		ans[i]=a[i]*b[i];
	IFFT(ans);
	for(int i=0;i<=n+m;++i)
		ans[i].real()/=lg;
}
void output()
{
	for(int i=0;i<=n+m;++i)
		write(int(ans[i].real()+0.5),i==n+m?'\n':' ');
}
int main()
{
	freopen("luogu3803_1.in","r",stdin);
	//freopen("luogu3803.out","w",stdout);
	input();
	initRev();
	solve();
	output();
	return 0;
}

快速高精乘

例题:luogu1919

差分约束

#差分约束 ##模型 将不等式条件化为:

x1-x2<=v1 x2-x3<=v2 … xi-xj<=vk

求取xi-xj最大值,最小值,或判断该不等式组解的存在性

##做法 对每个x建点 对xi-xj<=vk,建立xj->xi有向边,边权为vk 求max{xi-xj}:xj->xi最短路 求min{xi-xj}:xj->xi最长路 求该差分约束系统解的存在性:

平衡树——Treap&Splay

平衡树——Treap&Splay

平衡树学习总结 2016-12-29

概述

朴素的BST有时会退化成一条链,从而使二分查找的效率大大降低,为了防止这种情况的发生,平衡树应运而生。

顾名思义,平衡树的各个子树的深度应该相等或者相似。

两种基本操作:左旋&右旋 zig()&zag()

保证平衡树仍然满足BST的性质,但能够调换节点在树中的相对位置。

其余操作: insert(),delete(),findKth(),findRank(),precursor()//pre(),successor()//suc()

两种常用的平衡树:

1. Treap =Tree+heap

靠随机化减小退化的概率。

1
2
3
4
5
6
7
8
struct node
{
	int v,fix,l,r,weight,size;
	node()
	{
		v=fix=l=r=weight=size=0;
	}
}treap[100005];
插入:

插入时由随机数决定改变该节点的位置。

0%