結果

問題 No.1640 簡単な色塗り
ユーザー 👑 NachiaNachia
提出日時 2021-08-06 22:34:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,606 bytes
コンパイル時間 2,484 ms
コンパイル使用メモリ 86,976 KB
実行使用メモリ 10,360 KB
最終ジャッジ日時 2023-09-12 02:34:15
合計ジャッジ時間 11,246 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 38 ms
9,584 KB
testcase_05 AC 39 ms
9,648 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 33 ms
7,640 KB
testcase_11 AC 26 ms
6,752 KB
testcase_12 AC 23 ms
6,480 KB
testcase_13 AC 48 ms
9,860 KB
testcase_14 AC 52 ms
9,836 KB
testcase_15 AC 17 ms
5,416 KB
testcase_16 AC 20 ms
6,136 KB
testcase_17 AC 42 ms
8,780 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 23 ms
6,460 KB
testcase_20 AC 32 ms
7,612 KB
testcase_21 AC 27 ms
6,648 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 36 ms
7,984 KB
testcase_24 AC 9 ms
4,480 KB
testcase_25 AC 23 ms
6,380 KB
testcase_26 AC 41 ms
8,432 KB
testcase_27 AC 15 ms
5,300 KB
testcase_28 AC 50 ms
9,572 KB
testcase_29 AC 40 ms
8,412 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 59 ms
9,796 KB
testcase_32 AC 46 ms
8,748 KB
testcase_33 AC 30 ms
6,876 KB
testcase_34 AC 44 ms
8,000 KB
testcase_35 AC 28 ms
6,972 KB
testcase_36 AC 7 ms
4,380 KB
testcase_37 AC 9 ms
4,376 KB
testcase_38 AC 54 ms
8,904 KB
testcase_39 AC 17 ms
5,756 KB
testcase_40 AC 18 ms
5,660 KB
testcase_41 AC 38 ms
7,744 KB
testcase_42 AC 20 ms
6,216 KB
testcase_43 AC 22 ms
6,244 KB
testcase_44 AC 21 ms
6,064 KB
testcase_45 AC 16 ms
5,320 KB
testcase_46 AC 8 ms
4,376 KB
testcase_47 AC 5 ms
4,384 KB
testcase_48 AC 48 ms
8,980 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 1 ms
4,384 KB
testcase_52 AC 61 ms
10,276 KB
testcase_53 AC 60 ms
10,360 KB
07_evil_01.txt AC 156 ms
18,352 KB
07_evil_02.txt AC 261 ms
25,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/dsu>
using namespace std;
using i32 = int32_t;
using i64 = int64_t;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<vector<pair<int,int>>> E;
vector<int> used;

int main(){
  cin >> N;
  E.resize(N);
  vector<int> I(N,0);
  rep(i,N){
    int a,b; cin >> a >> b; a--; b--;
    E[a].push_back({b,i});
    E[b].push_back({a,i});
    I[a]++;
    I[b]++;
  }
  used.assign(N,-1);

  vector<int> q;
  rep(i,N) if(I[i] == 1) q.push_back(i);
  rep(i,q.size()){
    int p = q[i];
    for(auto e : E[p]) if(used[e.second] == -1){
      used[e.second] = p;
      I[e.first]--;
      if(I[e.first] == 1){
        q.push_back(e.first);
        break;
      }
    }
    I[p]--;
  }

  rep(s,N) if(I[s] >= 2){
    int to = -1;
    I[s]--;
    for(auto e : E[s]) if(used[e.second] == -1){
      used[e.second] = s;
      to = e.first;
      I[e.first]--;
      break;
    }
    if(to == -1){ cout << "No\n"; return 0; }
    while(to != -1){
      int p = to;
      I[p]--;
      to = -1;
      for(auto e : E[p]) if(used[e.second] == -1){
        used[e.second] = p;
        to = e.first;
        I[e.first]--;
        break;
      }
    }
  }

  rep(i,N) if(used[i] == -1){ cout << "No\n"; return 0; }

  vector<int> filled(N,0);
  rep(i,N) filled[used[i]] = 1;
  rep(i,N) if(filled[i] == 0){ cout << "No\n"; return 0; }

  cout << "Yes\n";
  rep(i,N) cout << (used[i]+1) << "\n";
  return 0;
}




struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;




0