結果

問題 No.1153 ねこちゃんゲーム
ユーザー SSRSSSRS
提出日時 2020-08-07 22:32:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,938 bytes
コンパイル時間 2,937 ms
コンパイル使用メモリ 179,516 KB
実行使用メモリ 40,676 KB
最終ジャッジ日時 2023-10-25 02:18:20
合計ジャッジ時間 18,938 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
  for (int i = 0; i < N; i++){
    cout << dp[i] << ' ';
  }
  cout << endl;
  for (int i = 0; i < N; i++){
    cout << dp2[i] << ' ';
  }
  cout << endl;
  int cnt = 0;
  for (int i = 0; i < M; i++){
    if (dp[i] || dp2[i]){
      cnt++;
    }
  }
  if (cnt % 2 == 0){
    cout << -1 << ' ' << -1 << endl;
  } else {
    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