BZOJ4337: [BJOI2015] 树的同构(Hash)
来源:互联网 发布:不得不说英文软件 编辑:程序博客网 时间:2024/05/22 11:42
传送门
题意:
给出n颗大小为m的树,对于两个树T1和T2,如果能够把树T1的所有点重新标号,使得树T1和树T2完全相同,那么这两个树是同构的。也就是说,它们具有相同的形态。
现在,给你M个有根树,请你把它们按同构关系分成若干个等价类。
题解:Hash
首先按照找欧拉序的方法找出树的括号序列。
树的Hash要保证形态相同的Hash值相同,可以先往子树Hash值大的走,这样可以保证一个形态的树Hash值唯一。
这道题因为
#include<bits/stdc++.h>using namespace std;struct IO{ streambuf *ib,*ob; inline void init(){ ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); ib=cin.rdbuf();ob=cout.rdbuf(); } inline int read(){ char ch=ib->sbumpc();int i=0,f=1; while(!isdigit(ch)){if(ch=='-')f=-1;ch=ib->sbumpc();} while(isdigit(ch)){i=(i<<1)+(i<<3)+ch-'0';ch=ib->sbumpc();} return i*f; } inline void W(int x){ static int buf[50]; if(!x){ob->sputc('0');ob->sputc('\n');return;} if(x<0){ob->sputc('-');x=-x;} while(x){buf[++buf[0]]=x%10;x/=10;} while(buf[0]){ob->sputc(buf[buf[0]--]+'0');} ob->sputc('\n'); }}io;const int Maxn=60;int n,m,mx,sze[Maxn],ans[Maxn];vector<int>G;vector<int>edge[Maxn];typedef pair<string,int> pii;vector<pii>S;inline void calcG(int now,int f){ sze[now]=1;int mxnow=0; for(int e=edge[now].size()-1;e>=0;e--){ int v=edge[now][e]; if(v==f)continue; calcG(v,now); sze[now]+=sze[v]; mxnow=max(mxnow,sze[v]); } mxnow=max(mxnow,m-sze[now]); if(mxnow==mx)G.push_back(now); else if(mxnow<mx)G.clear(),G.push_back(now),mx=mxnow;}inline string calc(int now,int f,string s){ s+='a'; vector<string>T; for(int e=edge[now].size()-1;e>=0;e--){ int v=edge[now][e]; if(v==f)continue; T.push_back(calc(v,now,"")); } if(T.end()!=T.begin())sort(T.begin(),T.end()); for(int e=T.size()-1;e>=0;e--)s+=T[e]; s+='b'; return s;}inline bool cmp(const pii &a,const pii &b){ return a.first<b.first||(a.first==b.first&&a.second<b.second);}int main(){ io.init(); n=io.read(); for(int tt=1;tt<=n;tt++){ m=io.read(); for(int i=1;i<=m;i++)edge[i].clear(); for(int i=1;i<=m;i++){ int x=io.read();if(!x)continue; edge[x].push_back(i);edge[i].push_back(x); } mx=m;G.clear(); calcG(1,0); for(int e=G.size()-1;e>=0;e--){ S.push_back(make_pair(calc(G[e],0,""),tt)); } } sort(S.begin(),S.end(),cmp); for(int e=0;e<S.size();e++){ if(ans[S[e].second])continue; if(!e||S[e].first!=S[e-1].first)ans[S[e].second]=S[e].second; else ans[S[e].second]=ans[S[e-1].second]; } for(int i=1;i<=n;i++)io.W(ans[i]);}
阅读全文
0 0
- BZOJ4337: [BJOI2015] 树的同构(Hash)
- BZOJ4337(BJOI2015)[树的同构]--树Hash
- bzoj4337: BJOI2015 树的同构 树hash
- 【bzoj4337】【BJOI2015】【树的同构】【hash】
- 【Hash】bzoj4337 BJOI2015树的同构
- 【Hash】BZOJ4337(BJOI2015)[树的同构]题解
- 【BJOI2015】【BZOJ4337】树的同构
- bzoj4337 BJOI2015 树的同构
- [BZOJ4337 BJOI2015 树的同构]树哈希
- 【树哈希】BZOJ4337 BJOI2015 树的同构
- 2017.10.6 BJOI2015 bzoj4337 树的同构
- bzoj4337树的同构(树hash)
- bzoj 4337: BJOI2015 树的同构 (树hash)
- BZOJ 4337 BJOI2015 树的同构 Hash
- BZOJ 4337: BJOI2015 树的同构|Hash
- [树hash]BZOJ 4337——BJOI2015 树的同构
- 【BZOJ】4337 BJOI2015 树的同构 树hash
- 4337: BJOI2015 树的同构
- JSP的9个内置对象
- 初学python一些会用到的零件1【小甲鱼】
- QSS灰色皮肤
- 多线程Object之wait、notify
- root手机实现自动签到等自动化功能
- BZOJ4337: [BJOI2015] 树的同构(Hash)
- 剑指offer:二叉树中和为某一值的路径
- 图论题目集
- QSS海军蓝色皮肤
- SVN的安装和使用
- 文章标题
- 工厂模式浅述
- virtualbox中ubuntu和windows共享文件夹设置
- @Temporal使用