結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー SSRSSSRS
提出日時 2022-06-17 21:40:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 889 ms / 4,000 ms
コード長 2,187 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 172,324 KB
実行使用メモリ 82,116 KB
最終ジャッジ日時 2024-04-17 13:16:21
合計ジャッジ時間 22,979 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 15 ms
5,376 KB
testcase_09 AC 23 ms
5,376 KB
testcase_10 AC 23 ms
6,016 KB
testcase_11 AC 28 ms
6,016 KB
testcase_12 AC 18 ms
5,376 KB
testcase_13 AC 516 ms
32,200 KB
testcase_14 AC 554 ms
36,328 KB
testcase_15 AC 585 ms
48,200 KB
testcase_16 AC 327 ms
43,684 KB
testcase_17 AC 538 ms
37,700 KB
testcase_18 AC 451 ms
49,052 KB
testcase_19 AC 519 ms
57,132 KB
testcase_20 AC 479 ms
45,324 KB
testcase_21 AC 488 ms
43,712 KB
testcase_22 AC 558 ms
46,140 KB
testcase_23 AC 800 ms
60,448 KB
testcase_24 AC 833 ms
60,432 KB
testcase_25 AC 836 ms
60,500 KB
testcase_26 AC 831 ms
60,444 KB
testcase_27 AC 764 ms
60,568 KB
testcase_28 AC 803 ms
60,420 KB
testcase_29 AC 809 ms
60,480 KB
testcase_30 AC 850 ms
60,428 KB
testcase_31 AC 794 ms
60,432 KB
testcase_32 AC 831 ms
60,428 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 580 ms
45,532 KB
testcase_35 AC 856 ms
82,116 KB
testcase_36 AC 683 ms
55,756 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 342 ms
5,376 KB
testcase_39 AC 889 ms
82,108 KB
testcase_40 AC 706 ms
52,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 18;
void dfs(vector<vector<int>> &E, vector<int> &d, vector<int> &p, vector<vector<int>> &c, vector<int> &in, int v){
  in.push_back(v);
  for (int w : E[v]){
    if (d[w] == -1){
      d[w] = d[v] + 1;
      p[w] = v;
      c[v].push_back(w);
      dfs(E, d, p, c, in, w);
    }
  }
}
int main(){
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> E(N);
  for (int i = 0; i < M; 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);
  vector<int> d(N, -1);
  vector<int> imos(N, 0);
  vector<int> in;
  for (int i = 0; i < N; i++){
    if (d[i] == -1){
      d[i] = 0;
      if (i > 0){
        p[i] = 0;
        c[0].push_back(i);
        d[i] = 1;
        imos[i]++;
        imos[0]--;
      }
      dfs(E, d, p, c, in, i);
    }
  }
  for (int i = 0; i < N; i++){
    for (int j : E[i]){
      if (d[j] < d[i] - 1){
        imos[i]++;
        imos[j]--;
      }
    }
  }
  reverse(in.begin(), in.end());
  for (int v : in){
    if (v != 0){
      imos[p[v]] += imos[v];
    }
  }
  vector<vector<int>> pp(LOG, vector<int>(N, -1));
  pp[0] = p;
  vector<vector<int>> sum(LOG, vector<int>(N, 0));
  sum[0] = imos;
  for (int i = 0; i < LOG - 1; i++){
    for (int j = 0; j < N; j++){
      if (pp[i][j] != -1){
        pp[i + 1][j] = pp[i][pp[i][j]];
        if (pp[i + 1][j] != -1){
          sum[i + 1][j] = sum[i][j] + sum[i][pp[i][j]];
        }
      }
    }
  }
  for (int i = 0; i < Q; i++){
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    if (d[x] > d[y]){
      swap(x, y);
    }
    int s = 0;
    for (int j = 0; j < LOG; j++){
      if (((d[y] - d[x]) >> j & 1) == 1){
        s += sum[j][y];
        y = pp[j][y]; 
      }
    }
    if (x != y){
      for (int j = LOG - 1; j >= 0; j--){
        if (pp[j][x] != pp[j][y]){
          s += sum[j][x];
          s += sum[j][y];
          x = pp[j][x];
          y = pp[j][y];
        }
      }
      s += sum[0][x];
      s += sum[0][y];
    }
    if (s == 0){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0