码迷,mamicode.com
首页 > 其他好文 > 详细

LA 7578. ACM World Finals 2016 C. Ceiling Function

时间:2017-12-31 23:34:01      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:turn   cin   ceil   out   blog   lin   acm   stream   root   

树的同构

#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

标签:turn   cin   ceil   out   blog   lin   acm   stream   root   

原文地址:https://www.cnblogs.com/P6174/p/8159274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!