ZOJ3602 Count the Trees STL 树的同构
来源:互联网 发布:mysql dba视频教程 编辑:程序博客网 时间:2024/06/05 05:45
题意:求两棵二叉树中同构的子树对。
做法:首先透彻什么叫二叉树的子树!!然后,不要贸然就用hash,二叉的独特性质使这道题可以很好的用map解决,记得记得!!
最后,最重要的是ZOJ中可能只能识别%lld
#include<utility>#include<cstdio>#include<map>#define LMT 100005#define LL long long//二叉树同构要分左右的//哈希还是少用比较好啊//map库的作用using namespace std;pair<int,int>node1[LMT],node2[LMT];map< pair<int,int>,int>mp;map<int,int>mg;int cnt;int h1[LMT],h2[LMT];int gethash(pair<int,int> x){ if(mp.find(x)!=mp.end())return mp[x]; else { mp[x]=cnt; cnt+=3; return cnt-3; }}void dfs(int u,pair<int,int> *node,int *h){ int l=node[u].first,r=node[u].second; if(l>0)dfs(l,node,h); else h[l]=-1; if(r>0)dfs(r,node,h); else h[r]=-1; h[u]=gethash(make_pair(h[l],h[r]));}int main(){int T,n,m;LL ans;scanf("%d",&T);while(T--){int u,v;mp.clear();scanf("%d%d",&n,&m);cnt=0;for(int i=1;i<=n;i++){scanf("%d%d",&u,&v);node1[i]=make_pair(u,v);}dfs(1,node1,h1);for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);node2[i]=make_pair(u,v);}dfs(1,node2,h2);mg.clear();for(int i=1;i<=m;i++)mg[h2[i]]++;ans=0;for(int i=1;i<=n;i++)ans+=(LL)mg[h1[i]];printf("%lld\n",ans);}return 0;}
- ZOJ3602 Count the Trees STL 树的同构
- zoj 3602 Count the Trees 树的同构
- ZOJ 3602 Count the Trees 树的同构 (哈希)
- ZOJ--3602--Count the Trees【DFS+Hash】树的同构
- CSU-1837 Tree Count(树的同构)
- ZOJ 3602Count the Trees(二叉树)
- UVa10007 - Count the Trees
- Count the Trees
- Count the Trees
- Codeforces 902 C.Hashing Trees 树的同构
- hdu 1131 Count the Trees
- hdu 1131 Count the Trees
- ZOJ 3602Count the Trees
- ZOJ 3602 Count the Trees
- UVA - 10007 Count the Trees
- hdu-1131 Count the trees
- UVa 10007 - Count the Trees
- ZOJ 3602 Count the Trees
- KEIL中为自己的关键字着色
- 用java.net.Socket实现点对点的文件传输
- 文件传输协议FTP
- Oracle索引原理
- 关于ffmpeg录制图像是黑白色
- ZOJ3602 Count the Trees STL 树的同构
- 软件测试总结(十)
- POJ 1003解题报告
- TDD(Test-Driven Development)
- iPhone5和4的屏幕兼容
- LAYER_NODE_FUNC 作用
- Freebsd 修改分区
- 单链表的设计与实现
- FPGA的基础知识