結果

問題 No.1640 簡単な色塗り
ユーザー 👑 platinumplatinum
提出日時 2021-06-19 13:24:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 2,527 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 208,484 KB
実行使用メモリ 16,732 KB
最終ジャッジ日時 2023-09-12 01:37:36
合計ジャッジ時間 13,771 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 196 ms
11,912 KB
testcase_05 AC 200 ms
16,732 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 130 ms
8,660 KB
testcase_11 AC 110 ms
7,636 KB
testcase_12 AC 97 ms
7,152 KB
testcase_13 AC 207 ms
11,824 KB
testcase_14 AC 199 ms
11,636 KB
testcase_15 AC 68 ms
6,104 KB
testcase_16 AC 82 ms
6,716 KB
testcase_17 AC 164 ms
10,236 KB
testcase_18 AC 16 ms
4,380 KB
testcase_19 AC 94 ms
7,020 KB
testcase_20 AC 125 ms
8,348 KB
testcase_21 AC 102 ms
7,432 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 137 ms
8,964 KB
testcase_24 AC 38 ms
4,872 KB
testcase_25 AC 100 ms
7,196 KB
testcase_26 AC 149 ms
9,492 KB
testcase_27 AC 65 ms
5,788 KB
testcase_28 AC 199 ms
11,152 KB
testcase_29 AC 154 ms
9,332 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 94 ms
10,656 KB
testcase_32 AC 78 ms
9,760 KB
testcase_33 AC 50 ms
7,488 KB
testcase_34 AC 62 ms
8,644 KB
testcase_35 AC 48 ms
7,588 KB
testcase_36 AC 12 ms
4,380 KB
testcase_37 AC 16 ms
4,576 KB
testcase_38 AC 80 ms
9,980 KB
testcase_39 AC 29 ms
6,256 KB
testcase_40 AC 31 ms
6,148 KB
testcase_41 AC 60 ms
8,860 KB
testcase_42 AC 35 ms
6,604 KB
testcase_43 AC 37 ms
6,804 KB
testcase_44 AC 35 ms
6,448 KB
testcase_45 AC 25 ms
5,632 KB
testcase_46 AC 13 ms
4,384 KB
testcase_47 AC 9 ms
4,384 KB
testcase_48 AC 79 ms
10,120 KB
testcase_49 AC 5 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,384 KB
testcase_52 AC 239 ms
14,656 KB
testcase_53 AC 239 ms
14,400 KB
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

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 con[100010], visited[100010], ans[100010];

int st = -1, erase_num = -1;
bool fin = false;

void dfs(int n, int par = -1){
	for(auto e : G[n]){
		if(e.to == par) continue;
		if(e.num == erase_num) continue;
		ans[e.num] = e.to + 1;
		dfs(e.to, n);
	}
}

void connect(int n){
	con[n] = 1;
	for(auto e : G[n]){
		if(con[e.to]) continue;
		connect(e.to);
	}
}

void cycle(int n, int edge_prev = -1){
	visited[n] = 1;
	for(auto e : G[n]){
		if(fin) return;
		if(e.num == edge_prev) continue;
		if(visited[e.to]){
			st = e.to;
			ans[e.num] = e.to + 1;
			erase_num = e.num;
			fin = true;
			break;
		}
		cycle(e.to, e.num);
	}
}

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(!con[i]){
			connect(i);
			fin = false;
			cycle(i);
			//rep(j,G[st].size()) cout << G[st][j].to << endl;
			dfs(st);
		}
	}
	//cout << st << " " << erase_num << endl;
	cout << "Yes" << endl;
	rep(i,N) cout << ans[i] << endl;

	return 0;
}
0