結果

問題 No.1640 簡単な色塗り
ユーザー 👑 platinumplatinum
提出日時 2021-06-18 13:25:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,238 bytes
コンパイル時間 2,196 ms
コンパイル使用メモリ 209,592 KB
実行使用メモリ 814,664 KB
最終ジャッジ日時 2023-09-12 01:33:37
合計ジャッジ時間 6,043 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 RE -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 193 ms
11,756 KB
testcase_05 MLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
const int Max_N = (int)1e5;

class UnionFind {
public:
	vector <long long> par;
	vector <long long> siz;

	UnionFind(long long sz_): par(sz_), siz(sz_, (long long)1) {
		for (long long i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(long long sz_) {
		par.resize(sz_);
		siz.assign(sz_, (long long)1);
		for (long long i = 0; i < sz_; ++i) par[i] = i;
	}

	long long root(long long x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}

	bool unite(long long x, long long y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}

	bool same(long long x, long long y) {
		return root(x) == root(y);
	}

	long long size(long long x) {
		return siz[root(x)];
	}
};


struct edge{
	int to, num;
	edge(int to, int num) : to(to), num(num) {}
};

vector<vector<edge>> G;
int visited[100010], ans[100010];

void dfs(int n, int par = -1){
	for(auto e : G[n]){
		if(e.to == par) continue;
		ans[e.num] = e.to + 1;
		dfs(e.to, n);
	}
}
void cycle(int n, int edge_prev = -1){
	int st = -1, er = -1;
	visited[n] = 1;
	for(int j = 0; j < G[n].size(); j++){
		edge e = G[n][j];
		if(e.num == edge_prev) continue;
		if(visited[e.to]){
			st = e.to;
			ans[e.num] = e.to + 1;
			er = j;
			continue;
		}
		cycle(e.to, e.num);
	}
	G[n].erase(G[n].begin() + er);
	dfs(st);
}

int main(){
	int N;
	cin >> N;
	G.resize(N);
	assert(1 <= N and N <= Max_N);
	vector<int> A(N);
	UnionFind uf(N);
	rep(i,N){
		int a, b;
		cin >> a >> b;
		assert(1 <= a and a <= N);
		assert(1 <= b and b <= N);
		a--; b--;
		uf.unite(a, b);
		A[i] = a;
		G[a].emplace_back(b, i); G[b].emplace_back(a, i);
	}
	vector<int> edge_num(N);
	rep(i,N){
		int p = uf.root(A[i]);
		edge_num[p]++;
	}
	rep(i,N){
		if(uf.root(i) != i) continue;
		int siz = uf.size(i);
		if(siz != edge_num[i]){
			cout << "No" << endl;
			return 0;
		}
	}
	rep(i,N) if(!visited[i]) cycle(i);
	cout << "Yes" << endl;
	rep(i,N) cout << ans[i] << endl;

	return 0;
}
0