結果

問題 No.1640 簡単な色塗り
ユーザー 👑 platinumplatinum
提出日時 2021-06-19 12:54:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,664 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 209,060 KB
実行使用メモリ 507,424 KB
最終ジャッジ日時 2023-09-12 01:35:16
合計ジャッジ時間 7,761 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 200 ms
11,716 KB
testcase_05 AC 198 ms
16,400 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 TLE -
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);
	}
}

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

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

void cycle(int n, int edge_prev = -1){
	for(int j = 0; j < G[n].size(); j++){
		if(fin) return;
		edge e = G[n][j];
		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;
			G[n].erase(G[n].begin() + j);
			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(!visited[i]){
			connect(i);
			fin = false;
			cycle(i);
			int k = 0;
			rep(j,G[st].size()){
				if(G[st][j].num == erase_num) k = j;
			}
			G[st].erase(G[st].begin() + k);
			//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