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

  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
/*
poj2567
*/
#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,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()
	{
		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];}
};
const int maxn=53;
int n;
struct EDGE
{
	int to,nxt;
	void init(int too,int nxtt)
	{
		to=too,nxt=nxtt;
	}
}edge[maxn<<1];
int ek=0,head[maxn],dge[maxn];
bool cut[maxn<<1];
int redge(int e)
{
	return ((e-1)^1)+1;
}
void addEdge(int k,int v)
{
	edge[++ek].init(v,head[k]);
	head[k]=ek;
	++dge[v];
}
bool cmp(const int& a,const int& b)
{
	return a<b;
}
char str[maxn*maxn];
int sk;
char getin()
{
	char r;
	do{r=getchar();}
	while(r!=-1&&r!='\n'&&r!='('&&r!=')'&&(r<48||r>57));
	return r;
}
bool input()
{
	char r=getin();
	if(r==-1)return false;
	for(sk=0;r!='\n'&&r!=-1;r=getin())
		str[++sk]=r;
	return true;
}
int read(int l)
{
	--l;
	int w;char r;
	for(r=str[++l];(r<48||r>57);r=str[++l]);
	for(w=0;r>=48&&r<=57;r=str[++l])w=w*10+r-48;
	return w;
}
bool used[maxn];
void build(int l,int r,int f)
{
	int root=read(l);
	if(!used[root])used[root]=true,++n;
	if(f)
		addEdge(root,f),addEdge(f,root);
	for(int i=l;i<=r;++i)
	{
		if(str[i]=='(')
		{
			int s=1,j;
			for(j=i+1;j<=r&&s;++j)
			{
				if(str[j]=='(')++s;
				if(str[j]==')')--s;
			}
			--j;
			build(i+1,j-1,root);
			i=j;
		}	
	}
}
HEAP<int,maxn,cmp> h;
int prufer[maxn],pk;
void init()
{
	memset(head+1,0,n*sizeof(int));
	memset(dge+1,0,n*sizeof(int));
	ek=0;
	h.clear();
	memset(prufer+1,0,n*sizeof(int));
	pk=0;
	memset(cut+1,0,2*n*sizeof(int));
	n=0;
	memset(used,0,sizeof(used));
}
void solve()
{
	for(int i=1;i<=n;++i)
	{
		if(dge[i]==1)
			h.push(i);
	}
	for(int i=1;i<=n-1;++i)
	{
		int ff=h.top();h.pop();
		for(int i=head[ff];i;i=edge[i].nxt)
		{
			if(!cut[i])
			{
				int v=edge[i].to;
				prufer[++pk]=v;
 				cut[i]=cut[redge(i)]=true;
				--dge[v];
				if(dge[v]==1)h.push(v);
			}
		}
	}
}
void output()
{
	for(int i=1;i<=pk;++i)	
	{
		printf("%d",prufer[i]);
		if(i!=pk)putchar(' ');
	}
	putchar('\n');
}
int main()
{
	freopen("poj2567_1.in","r",stdin);
	//freopen("poj2567.out","w",stdout);
	while(input())
	{
		init();
		build(2,sk-1,0);
		solve();
		output();
	}
	return 0;
}
0%