LA 7578. ACM World Finals 2016 C. Ceiling Function
阿新 • • 發佈:2018-01-01
++ 樹的同構 acm iso pan eno func phi log
樹的同構
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct treenode {
int val;
treenode *lc, *rc;
treenode(int val = 0): val(val), lc(NULL), rc(NULL) {}
}tree[50][20];
int i, cnt, res;
void add_node(treenode *node) {
if (node->val < res)
if (node->lc == NULL)
node->lc = &(tree[i][++cnt] = treenode(res));
else
add_node(node->lc);
else
if (node->rc == NULL)
node->rc = &(tree[i][++cnt] = treenode(res));
else
add_node(node->rc);
}
bool isomorphism(treenode *t1, treenode *t2) { //兩棵樹是否同構
if (t1 == NULL && t2 == NULL)
return true;
if (t1 == NULL || t2 == NULL)
return false;
if (isomorphism(t1->lc, t2->lc) && isomorphism(t1->rc, t2->rc))
return true;
return false;
}
int main() {
int n, k;
cin >> n >> k;
int sum = n;
for (i = 0; i < n; ++i) {
cnt = 0;
for (int j = 0; j < 20; ++j)
tree[i][j] = treenode();
treenode *root = &tree[i][0 ];
cin >> root->val;
for (int j = 1; j < k; ++j) {
cin >> res;
add_node(root);
}
for (int j = 0; j < i; ++j)
if (isomorphism(root, &tree[j][0])) {
--sum;
break;
}
}
cout << sum << endl;
return 0;
}
LA 7578. ACM World Finals 2016 C. Ceiling Function