无根树同构

有两种方法,一种是确定其中一棵树,另一棵树枚举根节点。

一种是,利用拓扑排序,先确定其中一棵树。另一棵树,若拓扑后剩两个节点,则枚举这两个节点为根结点,否则,只需做一次。注意,无根树节点入度应为1。

 #include <iostream>
#include <cstdio>
using namespace std; const int N=;
const int leaf_hash=;
const int pt=;
const int qt=; struct {
int u,v;
int next;
}edge[N];
int head[N];
bool vis[N];
int top,n,tot; void addedge(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} int hash(int u){
vis[u]=true;
int sum=; bool flag=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){
flag=true;
sum=sum*(pt^hash(v))%qt;
}
}
if(flag) return sum;
else return leaf_hash;
} int main(){
int T,i,u,v; int A,B,a,b;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(head,-,sizeof(head));
tot=;
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
memset(vis,false,sizeof(vis));
A=hash();
memset(head,-,sizeof(head));
tot=;
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
for(i=;i<=n;i++){
memset(vis,false,sizeof(vis));
B=hash(i);
if(A==B){
break;
}
}
if(i<=n) puts("same");
else puts("different");
}
return ;
}

ustc 1117的更多相关文章

  1. csuoj 1117: 网格中的三角形

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1117 1117: 网格中的三角形 Time Limit: 3 Sec  Memory Limit: ...

  2. 51nod 1117 聪明的木匠 (贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 跟挑战程序书上例题一样,将要切割的n断木板,分别对应二叉树树的叶子 ...

  3. NEUOJ 1117: Ready to declare(单调队列)

    1117: Ready to declare 时间限制: 1 Sec  内存限制: 128 MB 提交: 358  解决: 41 [提交][状态][pid=1117" style=" ...

  4. USTC 1119 graph 图的同构

    USTC 1119 图的同构的严格定义可以参考离散数学:The simple graphs G1=(V1,E1) and G2=(V2,E2)are isomorphic if there exist ...

  5. 最新eclipse国内镜像站,比ustc等站点资源新。

    http://mirrors.neusoft.edu.cn/ 东软信息学院的镜像站,上面可以看到同步时间和状态很不错. 之前为了找最新的镜像站下载babel_language_packs r0.15. ...

  6. centos6.5 yum update 报错Couldn't resolve host 'centos.ustc.edu.cn'

    异常信息 [root@localhost ~]# yum -y update Loaded plugins: fastestmirror, refresh-packagekit, security S ...

  7. 【Codeforces Round 1117】Educational Round 60

    Codeforces Round 1117 这场比赛做了\(A\).\(B\).\(C\).\(D\).\(E\),\(div.2\)排名\(31\),加上\(div.1\)排名\(64\). 主要是 ...

  8. 当 1117 遇到 MLCC 后

    当 1117 遇到 MLCC 后 AMS1117 很多人用过吧,但是当大容量的 MLCC 时会出现什么呢? 会出现问题,而且严重的问题,输出纹波会变大,会自激,会有声音出来. 这是很多工程师没有注意的 ...

  9. 51NOD 1117 聪明的木匠

    来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 挑战原题吧  大概 每次挑选最小的两个,合起来 #inclu ...

随机推荐

  1. $tsinsenA1067$

    \(problem\) 这种题目需要一个定理 \(a[1]+a[2]+a[3]+a[4]...=(a[1]%mod)+...\) 本人出奇的懒 然后 动态规划?(恰似枚举) #include < ...

  2. 【年终糖果计划】跟风领一波糖果 candy.one 领取教程

    糖果领取网址(较为稳定):https://candy.one/i/1474564 用微信和QQ打开的朋友请复制到其他浏览器打开 糖果领取网址(较为稳定):https://candy.one/i/147 ...

  3. Intent的调用

    //Intent  intent=new Intent();//intent.setClass(MainActivity.this, GPSService.class);//以上二条可以合并成如下一条 ...

  4. Failed to resolve com.android.support:support-annotations 26.0.1

    所有当前版本的Google库都存放在 Google的Maven repository (maven.google.com),不在旧的offline-capable support repositori ...

  5. 回顾Google IO 2016 -Keynote【图解】

    Google IO大会倒计时进行中~~ 两名演奏者在使用高空“古筝”. 最后5秒倒计时~~~~全场轰动~ 倒计时结束,IO大会正式开始.屏幕中,一个人把纯白的唱片放入唱片机中. 然后欢快的音乐响起,台 ...

  6. iOS错误报告中关于崩溃地址的分析

    http://blog.csdn.net/gaoyp/article/details/46912753 一.错误报告中的三种地址:stack addressload addresssymbol add ...

  7. appium 使用send_keys方法时报错: driver.find_element_by_id("com.hmkx.zgjkj:id/layout_search_bar_input").send_keys("123")

    新手 使用send_keys方法时一直报错,上网查这个方法的用法,看着大家都是这么写的啊,后来直接搜索 报错信息,搜索结果的针对性就清楚多了. 原来是seleium版本太高导致的问题. 可以先在cmd ...

  8. S3C2440中断

    韦东山老师一期中断课程学习: 总结: 程序启动后工作流程,程序从0地址开始执行Reset  --> 重定位  -->ldr pc,=main [绝对跳转到SDRAM中执行main()函数] ...

  9. python round()模块

    Python3的round()函数四舍五入取整时,采用最近偶数原则 >>> round(1.5)2>>> round(2.5)2>>> round ...

  10. 1 TaskQueue 实现Task 队列

    class Program { static void Main(string[] args) { List<Person> list = new List<Person>() ...