[zoj3990]Tree Equation
记$dep(T)$为树$T$的深度(根节点深度为0),则有$\begin{cases}dep(A+B)=\max(dep(A),dep(B))\\dep(A\cdot B)=dep(A)+dep(B)\end{cases}$
考虑$C$中最深的点,对其来源于$AX$还是$BY$分类讨论(不妨假设是前者),再取出其深度为$dep(A)$的祖先,那么$X$即只能取该祖先的子树
确定$X$后,求出$AX$并在$C$中去掉,再类似地求出$Y$并判定即可
过程中需要实现一个树同构的判定,简单哈希即可
时间复杂度为$o(n\log n)$,可以通过
(实现可能略微有一些繁琐,由于无法提交并不保证代码正确,仅供参考)

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define mod 998244353
5 #define ll long long
6 int t,num[11],seed[N];
7 map<int,vector<int> >mat;
8 map<int,vector<int> >::iterator it;
9 int read(){
10 int x=0;
11 char c=getchar();
12 while ((c<'0')||(c>'9'))c=getchar();
13 while ((c>='0')&&(c<='9')){
14 x=x*10+c-'0';
15 c=getchar();
16 }
17 return x;
18 }
19 void write(int x,char c='\0'){
20 while (x){
21 num[++num[0]]=x%10;
22 x/=10;
23 }
24 if (!num[0])putchar('0');
25 while (num[0])putchar(num[num[0]--]+'0');
26 putchar(c);
27 }
28 struct Tree{
29 int n,rt,mx,fa[N],dep[N],sz[N],f[N];
30 vector<int>v[N];
31 bool operator == (const Tree &T)const{
32 return (sz[rt]==T.sz[T.rt])&&(f[rt]==T.f[T.rt]);
33 }
34 void Read(){
35 for(int i=1;i<=n;i++)v[i].clear();
36 for(int i=1;i<=n;i++){
37 int x=read();
38 if (!x)rt=i;
39 else v[x].push_back(i);
40 }
41 }
42 void Write(){
43 for(int i=1;i<n;i++)write(fa[i],' ');
44 write(fa[n],'\n');
45 }
46 void dfs(int k,int s){
47 dep[k]=s,sz[k]=f[k]=1;
48 for(int i=0;i<v[k].size();i++){
49 fa[v[k][i]]=k,dfs(v[k][i],s+1);
50 sz[k]+=sz[v[k][i]];
51 f[k]=(f[k]+(ll)f[v[k][i]]*seed[sz[v[k][i]]])%mod;
52 }
53 }
54 void build(){
55 fa[rt]=0,dfs(rt,0);
56 mx=0;
57 for(int i=1;i<=n;i++)mx=max(mx,dep[i]);
58 }
59 void get(Tree &T,int k,int f){
60 int id=++T.n;
61 T.v[id].clear();
62 if (f)T.v[f].push_back(id);
63 for(int i=0;i<v[k].size();i++)get(T,v[k][i],id);
64 }
65 }A,B,C,X,Y,T,T0;
66 void mul(Tree &A,Tree &B,Tree &T){
67 T.n=A.n*B.n,T.rt=(A.rt-1)*B.n+1;
68 for(int i=1;i<=T.n;i++)T.v[i].clear();
69 for(int i=1;i<=A.n;i++){
70 for(int j=0;j<A.v[i].size();j++)T.v[(i-1)*B.n+1].push_back((A.v[i][j]-1)*B.n+1);
71 for(int j=1;j<=B.n;j++)
72 for(int k=0;k<B.v[j].size();k++)T.v[(i-1)*B.n+j].push_back((i-1)*B.n+B.v[j][k]);
73 }
74 }
75 bool dec(Tree &A,Tree &B,Tree &T){
76 mat.clear();
77 for(int i=0;i<A.v[A.rt].size();i++)mat[A.f[A.v[A.rt][i]]].push_back(A.v[A.rt][i]);
78 T.n=T.rt=1,T.v[1].clear();
79 for(int i=0;i<B.v[B.rt].size();i++){
80 int x=B.f[B.v[B.rt][i]];
81 if (mat[x].empty())return 0;
82 mat[x].pop_back();
83 }
84 for(it=mat.begin();it!=mat.end();it++){
85 vector<int>v=(*it).second;
86 for(int i=0;i<v.size();i++)A.get(T,v[i],1);
87 }
88 return 1;
89 }
90 bool calc(){
91 int pos;
92 for(int i=0;i<=C.n;i++)
93 if (C.dep[i]==C.mx)pos=i;
94 while (C.dep[pos]!=A.mx)pos=C.fa[pos];
95 X.n=0,X.rt=1,C.get(X,pos,0),X.build();
96 if ((ll)A.n*X.n>C.n)return 0;
97 mul(A,X,T),T.build();
98 if (!dec(C,T,T0))return 0;
99 T0.build();
100 for(int i=0;i<=T0.n;i++)
101 if (T0.dep[i]==T0.mx)pos=i;
102 while (T0.dep[pos]!=B.mx)pos=T0.fa[pos];
103 Y.n=0,Y.rt=1,T0.get(Y,pos,0),Y.build();
104 if ((ll)B.n*Y.n!=T0.n)return 0;
105 mul(B,Y,T),T.build();
106 return T==T0;
107 }
108 int main(){
109 srand(time(0));
110 for(int i=0;i<N;i++)
111 for(int j=0;j<60;j++)seed[i]=((seed[i]<<1)+rand()%2)%mod;
112 t=read();
113 while (t--){
114 A.n=read(),B.n=read(),C.n=read();
115 A.Read(),B.Read(),C.Read();
116 A.build(),B.build(),C.build();
117 if (calc()){
118 write(X.n,' '),write(Y.n,'\n');
119 X.Write(),Y.Write();
120 continue;
121 }
122 swap(A,B);
123 if (!calc())printf("Impossible\n");
124 else{
125 write(Y.n,' '),write(X.n,'\n');
126 Y.Write(),X.Write();
127 }
128 }
129 return 0;
130 }
[zoj3990]Tree Equation的更多相关文章
- 2017 CCPC Qinhuangdao Site
A. Balloon Robot 假设机器人$0$时刻位于$0$号位置,那么每个气球所需的时间为$(s_a-b)\bmod m$. 将所有气球按这个时间排序,枚举每个气球的时间作为偏移量,得出最优解即 ...
- HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Tree - Information Theory
This will be a series of post about Tree model and relevant ensemble method, including but not limit ...
- HDUOJ----2489 Minimal Ratio Tree
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)
Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
随机推荐
- Redis的一些常用命令
查看所有键 keys * 首先先向数据库中插入一些键值对 演示keys *命令 keys *查询所有键的方式是遍历数据库中的键,其时间复杂度为O(n),如果数据库的数量一旦过大,其效率就大大降低,因此 ...
- 基于python的pixiv爬虫
基于python的pixiv爬虫 1.目标 在和朋友吹逼过程中,聊到qq群机器人,突发奇想动手做一个p站每日推荐色图的色图机,遂学习爬虫. 目标: 批量下载首页推荐色图. 由于对qq机器人不熟,先利用 ...
- FastAPI 学习之路(三十二)创建数据库
在大型的web开发中,我们肯定会用到数据库操作,那么FastAPI也支持数据库的开发,你可以用 PostgreSQL MySQL SQLite Oracle 等 本文用SQLite为例.我们看下在fa ...
- Vue Router 常见问题(push报错、push重复路由刷新)
Vue Router 常见问题 用于记录工作遇到的Vue Router bug及常用方案 router.push报错,Avoided redundant navigation to current l ...
- BUAA-OO-最后单元总结
BUAA-OO-最后单元总结 经过一学期的魔鬼"折磨"后,OO课程终于要结束了!总体来说我对于作业的总体完成情况还是比较满意的,希望最后可以取得一个理想成绩. 一.第四单元架构设计 ...
- Noip模拟52 2021.9.13
T1 异或 比较稳的切掉 观察数据范围,无法线性筛啥的,根号复杂度也会死,于是只能考虑$log$级 然后打表 发现当$n$为$2^i$时的答案是一个可递归数列: $1,3,7,15,31,63,127 ...
- IDA*、剪枝、较难搜索、扫描——DNA sequence HDU - 1560
万恶之源 翻译 题意就是给出N个DNA序列,要求出一个包含这n个序列的最短序列是多长 这是一道搜索题,为什么呢?从样例可以感受到,我们应该从左往右"扫描",从n个DNA序列中取出某 ...
- VirtualBox Share Folder
转载:https://www.cnblogs.com/Dennis-mi/articles/5896586.html 使用virtualbox最方便的host-guest交换文件方案莫过于共享文件夹功 ...
- Python matplotlib pylab 画张图
from pylab import * w1 = 1 w2 = 25 fs = 18 y = np.arange(-2,2,0.001) x = w1*y*log(y)-1.0/w2*exp(-(w2 ...
- word-ladder leetcoder C++
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...