[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 ...
随机推荐
- 关于突破 SESSION 0 隔离场景发现的一些问题
0x00 Tricks 0x01 用ZwCreateThreadEx 在 Windows 10 下直接通过管理员权限+SeDebugPrivilege启用. 0x02 用CreateRemoteThr ...
- uniapp小程序迁移到TS
uniapp小程序迁移到TS 我一直在做的小程序就是 山科小站 也已经做了两年了,目前是用uniapp构建的,在这期间也重构好几次了,这次在鹅厂实习感觉受益良多,这又得来一次很大的重构,虽然小程序功能 ...
- 洛谷3769[CH弱省胡策R2]TATT (KDTree)(四维LIS)
真是一个自闭的题目(调了一个上午+大半个下午) 从\(WA\)到\(WA+TLE\)到\(TLE\)到\(AC\) 真的艰辛. 首先,这个题,我们可以考虑直接上四维KDTree来解决. 对于kdtre ...
- SpringBoot入门01-环境部署
随笔目录: 环境准备 创建过程 编码试行 环境准备 如果编辑器是还没有配置过相关环境的,在用SpringBoot开发项目的时候,需要先环境,spring boot官网建议的开发工具是: STS或Ide ...
- SpringCloud升级之路2020.0.x版-28.OpenFeign的生命周期-进行调用
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 接下来,我们开始分析 OpenFeign 同步环境下的生命周期的第二部分,使用 Synch ...
- Go语言核心36讲(Go语言进阶技术二)--学习笔记
08 | container包中的那些容器 我们在上次讨论了数组和切片,当我们提到数组的时候,往往会想起链表.那么 Go 语言的链表是什么样的呢? Go 语言的链表实现在标准库的container/l ...
- 第五课第四周笔记3:Multi-Head Attention多头注意力
Multi-Head Attention多头注意力 让我们进入并了解多头注意力机制. 符号变得有点复杂,但要记住的事情基本上只是你在上一个视频中学到的自我注意机制的四个大循环. 让我们看一下每次计算自 ...
- UI自动化测试之Airtest
官方文档: https://airtest.doc.io.netease.com/ 本文我们讲解下Airtest的使用,主要学习目标有以下几点: (1)认识Airtest (2)了解Airtest能做 ...
- [对对子队]会议记录4.16(Scrum Meeting7)
今天已完成的工作 何瑞 工作内容:完成成本和分数系统 相关issue:实现成本和分数系统的逻辑 相关签入:4.16签入1 吴昭邦 工作内容:对接流水线和成本和分数系统 相关issu ...
- Spring Cloud Alibaba 介绍及工程准备
简介 SpringCloud Alibaba是阿里巴巴集团开源的一套微服务架构解决方案. 微服务架构是为了更好的分布式系统开发,将一个应用拆分成多个子应用,每一个服务都是可以独立运行的子工程.其中涵盖 ...