結果

問題 No.1640 簡単な色塗り
ユーザー tnakao0123tnakao0123
提出日時 2021-08-08 02:48:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,664 bytes
コンパイル時間 1,032 ms
コンパイル使用メモリ 95,928 KB
実行使用メモリ 12,528 KB
最終ジャッジ日時 2023-09-12 04:01:29
合計ジャッジ時間 8,716 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,908 KB
testcase_01 AC 3 ms
6,588 KB
testcase_02 AC 3 ms
7,016 KB
testcase_03 AC 3 ms
7,016 KB
testcase_04 AC 32 ms
8,952 KB
testcase_05 AC 44 ms
12,528 KB
testcase_06 AC 3 ms
6,908 KB
testcase_07 AC 3 ms
7,000 KB
testcase_08 AC 3 ms
7,100 KB
testcase_09 AC 3 ms
6,528 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 28 ms
9,592 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 21 ms
8,800 KB
testcase_16 AC 24 ms
9,236 KB
testcase_17 WA -
testcase_18 AC 7 ms
7,472 KB
testcase_19 AC 28 ms
9,432 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 39 ms
10,664 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 9 ms
7,100 KB
testcase_31 AC 46 ms
10,628 KB
testcase_32 AC 39 ms
10,304 KB
testcase_33 AC 27 ms
9,076 KB
testcase_34 AC 33 ms
9,576 KB
testcase_35 AC 25 ms
8,992 KB
testcase_36 AC 8 ms
7,124 KB
testcase_37 AC 11 ms
7,324 KB
testcase_38 AC 42 ms
10,428 KB
testcase_39 AC 17 ms
8,164 KB
testcase_40 AC 18 ms
8,212 KB
testcase_41 AC 34 ms
9,724 KB
testcase_42 AC 20 ms
8,532 KB
testcase_43 AC 21 ms
8,628 KB
testcase_44 AC 21 ms
8,584 KB
testcase_45 AC 16 ms
7,912 KB
testcase_46 AC 9 ms
7,236 KB
testcase_47 AC 7 ms
7,004 KB
testcase_48 AC 46 ms
10,524 KB
testcase_49 AC 4 ms
6,692 KB
testcase_50 AC 3 ms
6,476 KB
testcase_51 AC 3 ms
7,044 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1640.cc:  No.1640 簡単な色塗り - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];
  UFT() {}

  void init(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

UFT uft;
vpii nbrs[MAX_N];
int rts[MAX_N], res[MAX_N], eps[MAX_N];
int ps[MAX_N], pes[MAX_N], cis[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  uft.init(n);
  memset(rts, -1, sizeof(rts));
  memset(res, -1, sizeof(res));

  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;

    int ra = uft.root(a), rb = uft.root(b);
    if (ra == rb)
      rts[ra] = a, res[ra] = i;
    else {
      uft.merge(ra, rb);
      nbrs[a].push_back(pii(b, i));
      nbrs[b].push_back(pii(a, i));
    }
  }

  for (int u = 0; u < n; u++)
    if (rts[uft.root(u)] < 0) { puts("No"); return 0; }

  memset(eps, -1, sizeof(eps));
  for (int ru = 0; ru < n; ru++) {
    if (ru != uft.root(ru)) continue;

    int rt = rts[ru], re = res[ru];
    eps[re] = rt;

    ps[rt] = pes[rt] = -1;
    for (int u = rt; u >= 0;) {
      vpii &nbru = nbrs[u];
      int up = ps[u];

      if (cis[u] < nbru.size()) {
	pii ve = nbru[cis[u]++];
	int v = ve.first, e = ve.second;
	if (v != up) {
	  ps[v] = u, pes[v] = e;
	  u = v;
	}
      }
      else {
	if (up >= 0) eps[pes[u]] = u;
	u = up;
      }
    }
  }

  puts("Yes");
  for (int i = 0; i < n; i++) printf("%d\n", eps[i] + 1);
  return 0;
}
0