poj 1635 Subway tree systems
来源:互联网 发布:淘宝付费流量占比 编辑:程序博客网 时间:2024/05/30 23:20
/*
Poj 1635
无回路的图,选定根节点,即可确定一棵树。一棵树统计每个节点的子节点个数,
每个节点的深度,最后给每个节点排序,即可当成树的最小表示。最小表示相同的树同构。
*/
#include <iostream>
using namespace std;
const int maxn=3005;
struct node
{
int depth,son;
}tree[maxn],tree2[maxn];
int CMP(const void* a,const void* b)
{
node* p=(node*)a;
node* q=(node*)b;
if(p->depth==q->depth) return p->son-q->son;
else return p->depth-q->depth;
}
/*
无回路的图,选定根节点,即可确定一棵树。一棵树统计每个节点的子节点个数,每个节点的深度,最后排序,即可当成树的最小表示。最小表示相同的树同构。
*/
int min_pre(char str[],node result[])
{
int node_num=1,now=0;
int father[maxn];
father[0]=result[0].son=result[0].depth=0;
for(int i=0;str[i];i++)
{
if(str[i]=='0')
{
father[node_num]=now;
result[node_num].depth=result[father[node_num]].depth+1;
result[node_num].son=0;
now=node_num++;
}
else
{
result[father[now]].son+=result[now].son+1;
now=father[now];
}
}
qsort(result,node_num,sizeof(result[0]),CMP);
return node_num;
}
int main()
{
char str[maxn];
int T;
scanf("%d",&T);
for(int t=0;t<T;t++)
{
scanf("%s",str);
int num=min_pre(str,tree);
scanf("%s",str);
int num2=min_pre(str,tree2);
if(num!=num2)
{
printf("different/n");
continue;
}
int i;
for(i=0;i<num;i++)
{
if(tree[i].depth!=tree2[i].depth||tree[i].son!=tree2[i].son)
{
printf("different/n");
break;
}
}
if(i>=num) printf("same/n");
}
return 0;
}
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- poj 1635 Subway tree systems
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems (BSOJ 1092)
- poj 1635 Subway tree systems 判断是否是同构树
- Poj 1635 Subway tree systems (树的最小表示)
- poj 1635 Subway tree systems(树的同构,经典)
- Subway tree systems - POJ 1635 树的最小表示法
- poj 1635 Subway tree systems (树同构)
- poj1635 - Subway tree systems
- POJ1635 Subway tree systems
- poj-1635 Subway tree systems(判断两个有根树是否同构)-哈希法
- Java中逻辑与,逻辑或,按位与,按位或的区分
- poj 2226 Muddy Fields
- ISG FootBalls Live
- poj 1695 Magazine Delivery
- poj 2155 Matrix
- poj 1635 Subway tree systems
- Java运算符优先级
- 0829冲刺第五天
- 【noi2005】维护数列
- poj 1523 SPF
- 如何解决2003共享需要密码问题?
- 无题札记(一)
- 杜甫写的有关风雨的古诗有哪些
- 编译程序与操作系统的关系