LA 7578. ACM World Finals 2016 C. Ceiling Function

Posted Planet6174

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LA 7578. ACM World Finals 2016 C. Ceiling Function相关的知识,希望对你有一定的参考价值。

树的同构

#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的主要内容,如果未能解决你的问题,请参考以下文章

LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding

LA 8043. ACM-ICPC World Finals 2017 E. Need for Speed

LA 7150. ACM-ICPC World Finals 2015 A. Amalgamated Artichokes

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

信息解码,ACM/ICPC World Finals 1991

2018 ACM-ICPC World Finals - Beijing