結果

問題 No.1153 ねこちゃんゲーム
ユーザー SSRSSSRS
提出日時 2020-08-07 22:34:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,808 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 179,272 KB
実行使用メモリ 40,772 KB
最終ジャッジ日時 2023-10-25 03:11:35
合計ジャッジ時間 28,539 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,348 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 381 ms
24,340 KB
testcase_11 RE -
testcase_12 WA -
testcase_13 AC 371 ms
24,340 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 WA -
testcase_22 RE -
testcase_23 RE -
testcase_24 WA -
testcase_25 AC 363 ms
24,340 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 409 ms
40,708 KB
testcase_29 WA -
testcase_30 AC 402 ms
39,388 KB
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 255 ms
23,200 KB
testcase_34 WA -
testcase_35 AC 255 ms
23,200 KB
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 WA -
testcase_40 RE -
testcase_41 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
void dfs1(vector<bool> &dp, vector<vector<int>> &c, int v = 0){
  for (int w : c[v]){
    dfs1(dp, c, w);
    if (!dp[w]){
      dp[v] = true;
    }
  }
}
void dfs2(vector<bool> &dp2, vector<bool> &dp, vector<int> &p, vector<vector<int>> &c, int v = 0){
  if (v != 0){
    if (!dp2[p[v]]){
      dp2[v] = true;
    }
  }
  if (!dp2[v]){
    int cnt = 0;
    for (int w : c[v]){
      if (!dp[w]){
        cnt++;
      }
    }
    for (int w : c[v]){
      if (cnt >= 2 || cnt == 1 && dp[w]){
        dp2[w] = true;
      }
    }
  }
  for (int w : c[v]){
    dfs2(dp2, dp, p, c, w);
  }
}
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  for (int i = 0; i < M; i++){
    cin >> A[i];
    A[i]--;
  }
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (int w : E[v]){
      if (p[v] != w){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  vector<bool> dp(N, false);
  dfs1(dp, c);
  vector<bool> dp2(N, false);
  dfs2(dp2, dp, p, c);
  int cnt = 0;
  for (int i = 0; i < M; i++){
    if (dp[A[i]] || dp2[A[i]]){
      cnt++;
    }
  }
  if (cnt % 2 == 0){
    cout << -1 << ' ' << -1 << endl;
  } else {
    assert(false);
    for (int i = 0; i < M; i++){
      if (dp[A[i]]){
        for (int j : c[A[i]]){
          if (!dp[j]){
            cout << i + 1 << ' ' << j + 1 << endl;
            break;
          }
        }
        break;
      } else if (dp2[A[i]]){
        cout << i + 1 << ' ' << p[A[i]] + 1 << endl;
        break;
      }
    }
  }
}
0