树的同构
#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;
}