結果

問題 No.1640 簡単な色塗り
ユーザー SSRSSSRS
提出日時 2021-08-06 21:48:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 2,705 bytes
コンパイル時間 1,988 ms
コンパイル使用メモリ 184,688 KB
実行使用メモリ 15,996 KB
最終ジャッジ日時 2023-09-12 01:46:26
合計ジャッジ時間 17,038 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 222 ms
9,540 KB
testcase_05 AC 234 ms
15,996 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 162 ms
8,724 KB
testcase_11 AC 126 ms
7,808 KB
testcase_12 AC 115 ms
8,172 KB
testcase_13 AC 260 ms
12,928 KB
testcase_14 AC 268 ms
13,544 KB
testcase_15 AC 80 ms
6,188 KB
testcase_16 AC 104 ms
7,632 KB
testcase_17 AC 214 ms
11,280 KB
testcase_18 AC 17 ms
4,376 KB
testcase_19 AC 111 ms
7,164 KB
testcase_20 AC 151 ms
8,432 KB
testcase_21 AC 124 ms
7,736 KB
testcase_22 AC 13 ms
4,376 KB
testcase_23 AC 170 ms
9,580 KB
testcase_24 AC 43 ms
4,764 KB
testcase_25 AC 112 ms
7,516 KB
testcase_26 AC 183 ms
8,972 KB
testcase_27 AC 76 ms
6,036 KB
testcase_28 AC 256 ms
13,152 KB
testcase_29 AC 192 ms
10,608 KB
testcase_30 AC 14 ms
4,380 KB
testcase_31 AC 135 ms
12,872 KB
testcase_32 AC 119 ms
12,232 KB
testcase_33 AC 82 ms
8,708 KB
testcase_34 AC 109 ms
11,240 KB
testcase_35 AC 57 ms
7,588 KB
testcase_36 AC 16 ms
4,536 KB
testcase_37 AC 22 ms
5,200 KB
testcase_38 AC 104 ms
10,844 KB
testcase_39 AC 47 ms
6,744 KB
testcase_40 AC 48 ms
6,920 KB
testcase_41 AC 105 ms
10,064 KB
testcase_42 AC 42 ms
6,832 KB
testcase_43 AC 59 ms
8,340 KB
testcase_44 AC 55 ms
7,512 KB
testcase_45 AC 41 ms
6,896 KB
testcase_46 AC 19 ms
5,056 KB
testcase_47 AC 12 ms
4,376 KB
testcase_48 AC 140 ms
12,928 KB
testcase_49 AC 6 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 306 ms
14,268 KB
testcase_53 AC 303 ms
14,288 KB
07_evil_01.txt AC 697 ms
26,840 KB
07_evil_02.txt AC 993 ms
29,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    E[A].push_back(make_pair(i, B));
    E[B].push_back(make_pair(i, A));
  }
  bool ok = true;
  vector<int> ans(N);
  vector<bool> used(N, false);
  vector<int> d(N);
  for (int i = 0; i < N; i++){
    d[i] = E[i].size();
  }
  for (int i = 0; i < N; i++){
    if (!used[i]){
      used[i] = true;
      vector<int> id_v;
      set<int> id_e;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        id_v.push_back(v);
        for (auto P : E[v]){
          id_e.insert(P.first);
          int w = P.second;
          if (!used[w]){
            used[w] = true;
            Q.push(w);
          }
        }
      }
      if (id_v.size() != id_e.size()){
        ok = false;
        break;
      }
      queue<int> Q2;
      for (int v : id_v){
        if (d[v] == 1){
          Q2.push(v);
        }
      }
      while (!Q2.empty()){
        int v = Q2.front();
        Q2.pop();
        for (auto P : E[v]){
          int id = P.first;
          int w = P.second;
          d[w]--;
          if (d[w] > 0){
            ans[id] = v;
          }
          if (d[w] == 1){
            Q2.push(w);
          }
        }
      }
      vector<int> cycle;
      for (int v : id_v){
        if (d[v] == 2){
          cycle.push_back(v);
          used[v] = false;
        }
      }
      vector<int> cycle_v = {cycle[0]};
      vector<int> cycle_e;
      while (true){
        int v = cycle_v.back();
        used[v] = true;
        bool ok2 = false;
        for (auto P : E[v]){
          int id = P.first;
          int w = P.second;
          if (!used[w]){
            cycle_e.push_back(id);
            cycle_v.push_back(w);
            ok2 = true;
            break;
          }
        }
        if (!ok2){
          for (auto P : E[v]){
            int id = P.first;
            int w = P.second;
            if (w == cycle[0]){
              bool ok3 = true;
              if (cycle_e.size() == 1){
                if (cycle_e[0] == id){
                  ok3 = false;
                }
              }
              if (ok3){
                cycle_e.push_back(id);
                break;
              }
            }
          }
          break;
        }
      }
      int cnt = cycle.size();
      for (int j = 0; j < cnt; j++){
        ans[cycle_e[j]] = cycle_v[j];
      }
    }
  }
  if (!ok){
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 0; i < N; i++){
      cout << ans[i] + 1 << endl;
    }
  }
}
0