結果

問題 No.1640 簡単な色塗り
ユーザー 沙耶花沙耶花
提出日時 2021-08-06 22:34:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,203 bytes
コンパイル時間 4,816 ms
コンパイル使用メモリ 270,528 KB
実行使用メモリ 21,856 KB
最終ジャッジ日時 2023-09-12 02:30:38
合計ジャッジ時間 15,856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 210 ms
16,156 KB
testcase_05 AC 194 ms
21,492 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 29 ms
8,316 KB
testcase_32 AC 25 ms
7,696 KB
testcase_33 AC 17 ms
6,304 KB
testcase_34 AC 22 ms
6,900 KB
testcase_35 AC 17 ms
6,320 KB
testcase_36 AC 6 ms
4,380 KB
testcase_37 AC 7 ms
4,472 KB
testcase_38 AC 26 ms
7,644 KB
testcase_39 AC 12 ms
5,120 KB
testcase_40 AC 13 ms
5,304 KB
testcase_41 AC 22 ms
6,792 KB
testcase_42 AC 14 ms
5,516 KB
testcase_43 AC 14 ms
5,648 KB
testcase_44 AC 14 ms
5,624 KB
testcase_45 AC 11 ms
4,940 KB
testcase_46 AC 7 ms
4,384 KB
testcase_47 AC 4 ms
4,380 KB
testcase_48 AC 28 ms
7,956 KB
testcase_49 AC 2 ms
4,388 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000

vector<int> get(vector<int> a,vector<int> b){
	int n = a.size();
	vector<vector<int>> E(n);
	rep(i,n){
		E[a[i]].push_back(i);
		E[b[i]].push_back(i);
	}
	vector<int> ret(n,-1);
	vector<int> cnt(n);
	queue<int> Q;
	rep(i,n){
		cnt[i] = E[i].size();
		if(cnt[i]==1)Q.push(i);
	}
	
	while(Q.size()>0){
		int u = Q.front();
		Q.pop();
		
		rep(i,E[u].size()){
			int v = a[E[u][i]] ^ b[E[u][i]] ^ u;
			if(ret[v]==-1)continue;
			ret[E[u][i]] = u;
			cnt[v]--;
			if(cnt[v]==1){
				Q.push(v);
			}
			break;
		}
	}
	{
		vector<vector<int>> e(n);
		rep(i,n){
			if(ret[a[i]]==-1&&ret[b[i]]==-1){
				e[a[i]].push_back(i);
				e[b[i]].push_back(i);
			}
		}
		swap(E,e);
	}
	
	rep(i,n){
		if(E[i].size()==0)continue;
		int cur = i;
		while(true){
			bool f = false;
			rep(j,E[cur].size()){
				int to = a[E[cur][j]] ^ b[E[cur][j]] ^ cur;
				if(ret[E[cur][j]]==-1){
					cnt[to] = -1;
					ret[E[cur][j]] = to;
					cur = to;
					f = true;
					break;
				}
			}
			if(!f)break;
		}
		
		break;
	}
	
	return ret;
	
}

int main(){
	
	int N;
	cin>>N;
	
	vector<int> A(N),B(N);
	
	dsu D(N);
	rep(i,N){
		scanf("%d %d",&A[i],&B[i]);
		A[i]--;B[i]--;
		
		D.merge(A[i],B[i]);
	}
	
	vector<int> p(N);
	auto g = D.groups();
	rep(i,g.size()){
		rep(j,g[i].size()){
			p[g[i][j]] = i;
		}
	}
	
	vector<vector<int>> inds(g.size());
	
	rep(i,N){
		inds[p[A[i]]].push_back(i);
	}
	
	rep(i,g.size()){
		if(inds[i].size() != g[i].size()){
			cout<<"No"<<endl;
			return 0;
		}
	}
	
	cout<<"Yes"<<endl;
	vector<int> ans(N);
	rep(i,g.size()){
		sort(g[i].begin(),g[i].end());
		
		vector<int> aa(g[i].size()),bb(g[i].size());
		
		rep(j,inds[i].size()){
			int k = inds[i][j];
			aa[j] = distance(g[i].begin(),lower_bound(g[i].begin(),g[i].end(),A[k]));
			bb[j] = distance(g[i].begin(),lower_bound(g[i].begin(),g[i].end(),B[k]));
		}
		vector<int> ret = get(aa,bb);
		rep(j,ret.size()){
			int k = inds[i][j];
			ans[k] = g[i][ret[j]];
		}
		
	}
	
	rep(i,N){
		cout<<ans[i]+1<<endl;
	}
	
	return 0;
}
0