結果

問題 No.1817 Reversed Edges
ユーザー kichi2004_kichi2004_
提出日時 2021-12-28 21:20:32
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,166 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 108,928 KB
実行使用メモリ 20,004 KB
最終ジャッジ日時 2024-04-10 13:29:07
合計ジャッジ時間 4,275 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <vector>

using std::vector;

constexpr int MAX_N = 100000;

struct Solver {
    Solver(int n, vector<vector<int>>& g) : N(n), graph(g), memo(n) {}

    int N;
    vector<vector<int>> graph;
    vector<int> memo;

    int dfs(int num, int difference = 0, int parent = -1) {
      int result = 0;
//      memo[num] = difference;
      for (int next : graph[num]) {
        if (next == parent) continue;
        if (num > next) ++result;
        result += dfs(next, difference + (num > next ? -1 : 1), num);
      }
      return result;
    }

    vector<int> solve() {
      vector<int> result(N);
      for (int i = 0; i < N; ++i) {
        result[i] = dfs(i);
      }
      return std::move(result);
    }
};

int main() {
  int N; std::scanf("%d", &N);
  vector graph(N, vector<int>());
  for (int i = 0; i < N - 1; ++i) {
    int A, B;
    std::scanf("%d%d", &A, &B);
    --A; --B;
    graph[A].push_back(B);
    graph[B].push_back(A);
  }
  Solver solver(N, graph);
  for (int answer : solver.solve()) {
    std::printf("%d\n", answer);
  }
}
0