无根树同构

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

一种是,利用拓扑排序,先确定其中一棵树。另一棵树,若拓扑后剩两个节点,则枚举这两个节点为根结点,否则,只需做一次。注意,无根树节点入度应为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. longpo的回文

    啊...比赛的时候输入打错了,结束之后还照着题解把DP部分重构了一遍然而还是WA...样例都没过,然后直接输了-1 明显的DP...而且数据范围这么小,显然怎么搞都可以... 而且这样的回文的DP是很 ...

  2. CentOS 7.0 firewall防火墙关闭firewall作为防火墙,这里改为iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤: 1.先检查是否安装了: iptables service iptables status 2.安装ip ...

  3. jmeter中对于各类时间格式的设置

    最普通的设置为使用 函数助手中的__time, 设置好需要使用的类型,并设置接收参数即可 YMD = yyyyMMdd HMS = HHmmss YMDHMS = yyyyMMdd-HHmmss 第二 ...

  4. ACM_Mystery

    Mystery Time Limit: 2000/1000ms (Java/Others) Problem Description: No Description Input: The first l ...

  5. [转]Linux之ACL权限

    转自:http://www.2cto.com/os/201110/108736.html 引言 前面的内容中,我们讲到传统的权限仅有三种身份(owner,group,others)搭配三种权限(r,w ...

  6. Manacher 学习笔记

    \(\\\) \(Manacher\) 一种常用的字符串算法,用于处理一些回文字符相关的问题. 回文串:从前向后和从后向前输出一致. 回文中心:以这里开始,每次向外左右各扩展一个字符得到的回文串的中心 ...

  7. window.showModalDialog的问题

    通过window.showModalDialog的方式弹出B页面,总报“拒绝访问”的错误,将站点添加到受信任站点可以解决这个问题

  8. 使用 RxJava 的正确姿势

    最近在使用 RxJava 时遇到了一些比较诡异的问题,排查的过程中做了点研究,发现平时大家的用法多多少少都有些问题.有些地方存在隐患,有些用法不够简练,我把这些问题简单做一下分类和总结,供大家参考. ...

  9. JS——缓动动画

    核心思想: (1)相对于匀速移动,盒子每次移动的步长都是变化的,公式:盒子位置=盒子本身位置+(目标位置-盒子本身位置)/10 (2)在盒子位置与目标距离小于10px时,其步长必然是小数,又由于off ...

  10. Git 分支创建

    分支策略:git上始终保持两个分支,master分支与develop分支.master分支主要用于发布时使用,而develop分支主要用于开发使用. 创建master的分支developgit che ...