提出 #813534


ソースコード 拡げる

#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
#include <algorithm>

struct Node {
  std::vector<int> to;
  std::vector<std::size_t> cost;
  bool done = false;
};

int main () {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::string line;
  std::getline(std::cin, line);

  char * p;
  int N = std::strtol(line.data(), &p, 10);
  std::size_t X = std::strtol(p, NULL, 10);

  std::vector<Node> node(N);
  for (int i = 0; i < N - 1; ++i) {
    std::getline(std::cin, line);
    int s = std::strtol(line.data(), &p, 10) - 1;
    int e = std::strtol(p, &p, 10) - 1;
    std::size_t x = std::strtoll(p, &p, 10);
    node[s].to.push_back(e);
    node[s].cost.push_back(x);
    node[e].to.push_back(s);
    node[e].cost.push_back(x);
  }

  int root = 0;
  for (int i = 0; i < N; ++i) {
    if (node[i].to.size() == 1) {
      root = i;
      break;
    }
  }

  std::vector<std::size_t> value(N, 0);
  std::vector<int> stack;
  stack.reserve(N / 4);

  stack.push_back(root);
  while (stack.size() > 0) {
    int i = stack.back();
    stack.pop_back();
    for (int j = 0; j < node[i].to.size(); ++j) {
      int k = node[i].to[j];
      if (!node[k].done) {
        value[k] = value[i] ^ node[i].cost[j];
        stack.push_back(k);
      }
    }
    node[i].done = true;
  }

  std::sort(value.begin(), value.end());
  std::unordered_map<std::size_t, int> unique;

  auto first = value.begin();
  auto last = value.end();

  for (;first < last; ++first) {
    unique[*first] += 1;
  }

  int ret = 0;
  if (X == 0) {
    for (auto const & p : unique) {
      if (p.second >= 1) {
        int n = 2 * p.second;
        ret += n * (n - 1) / 2;
      }
    }
    std::cout << ret / 2 << std::endl;
    return 0;
  }

  for (auto first = unique.begin(), last = unique.end(); first != last; ++first) {
    std::size_t x1 = first->first;
    std::size_t x2 = (X ^ x1);
    auto it = unique.find(x2);
    if (it != unique.end()) {
      ret += first->second * it->second;
    }
  }

  std::cout << (ret / 2) << std::endl;
}

提出情報

提出日時
問題 C - エックスオア多橋君
ユーザ toufu12345
言語 C++14 (Clang++ 3.4)
得点 0
コード長 2174 Byte
結果 WA
実行時間 186 ms
メモリ 18052 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 100
結果
AC × 3
AC × 25
WA × 2
セット名 テストケース
Sample subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt
ケース名 結果 実行時間 メモリ
subtask0_sample_01.txt AC 28 ms 860 KiB
subtask0_sample_02.txt AC 27 ms 924 KiB
subtask0_sample_03.txt AC 29 ms 928 KiB
subtask1_01.txt AC 27 ms 1040 KiB
subtask1_02.txt WA 28 ms 988 KiB
subtask1_03.txt AC 184 ms 18048 KiB
subtask1_04.txt AC 186 ms 18052 KiB
subtask1_05.txt AC 185 ms 18048 KiB
subtask1_06.txt WA 106 ms 13472 KiB
subtask1_07.txt AC 164 ms 13916 KiB
subtask1_08.txt AC 169 ms 13916 KiB
subtask1_09.txt AC 143 ms 14820 KiB
subtask1_10.txt AC 147 ms 14816 KiB
subtask1_11.txt AC 30 ms 1044 KiB
subtask1_12.txt AC 27 ms 988 KiB
subtask1_13.txt AC 134 ms 14044 KiB
subtask1_14.txt AC 147 ms 14044 KiB
subtask1_15.txt AC 39 ms 2524 KiB
subtask1_16.txt AC 40 ms 2524 KiB
subtask1_17.txt AC 39 ms 2524 KiB
subtask1_18.txt AC 39 ms 2528 KiB
subtask1_19.txt AC 38 ms 2516 KiB
subtask1_20.txt AC 37 ms 2572 KiB
subtask1_21.txt AC 39 ms 2520 KiB
subtask1_22.txt AC 37 ms 2520 KiB
subtask1_23.txt AC 41 ms 2644 KiB
subtask1_24.txt AC 41 ms 2524 KiB