結果

問題 No.1640 簡単な色塗り
ユーザー tpc011854tpc011854
提出日時 2021-08-06 22:49:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,982 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 50,088 KB
実行使用メモリ 14,064 KB
最終ジャッジ日時 2023-09-12 02:50:19
合計ジャッジ時間 8,032 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,964 KB
testcase_02 WA -
testcase_03 AC 2 ms
7,136 KB
testcase_04 AC 31 ms
7,624 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
7,036 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 8 ms
7,768 KB
testcase_31 AC 48 ms
11,832 KB
testcase_32 AC 43 ms
11,532 KB
testcase_33 AC 28 ms
10,220 KB
testcase_34 AC 36 ms
11,572 KB
testcase_35 AC 28 ms
10,448 KB
testcase_36 AC 8 ms
7,864 KB
testcase_37 AC 11 ms
8,360 KB
testcase_38 AC 43 ms
11,416 KB
testcase_39 AC 18 ms
9,072 KB
testcase_40 AC 18 ms
9,004 KB
testcase_41 AC 37 ms
10,700 KB
testcase_42 AC 22 ms
9,100 KB
testcase_43 AC 22 ms
9,536 KB
testcase_44 AC 22 ms
9,672 KB
testcase_45 AC 18 ms
9,128 KB
testcase_46 AC 8 ms
8,044 KB
testcase_47 AC 7 ms
7,804 KB
testcase_48 AC 44 ms
12,288 KB
testcase_49 AC 4 ms
7,208 KB
testcase_50 AC 2 ms
7,064 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
using std::vector;
int a[100005], b[100005];
struct Edge {
	int u, v;
	int id;
};
vector<Edge> g[100005];
int color[100005];
int cnt[100005];
int vis[100005];
Edge c[100005];
int stack[100005]; int top = 0;
int set[100005];

void dfs(int u){
	stack[++top] = u;
	vis[u] = 1;
	for(Edge e: g[u]){
		int v = e.v;
		if(!vis[v]){
			//color[id] = v;
			dfs(v);
		}
	}
}

void dfs1(int u){
	//printf("u = %d\n",u);
	vis[u] = 1;
	set[u] = 1;
	for(Edge e: g[u]){
		int v = e.v, id = e.id;
		//printf("v = %d\n",v);
		if(!color[id] && !vis[v]){
			color[id] = v;
			dfs1(v);
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d",&a[i],&b[i]);
		cnt[a[i]]++;
		cnt[b[i]]++;
	} 
	for(int i = 1; i <= n; i++){
		if(cnt[a[i]]==1){
			color[i] = a[i];
			vis[a[i]] = 1;
			//dfs1(a[i]);
		}
		else if(cnt[b[i]]==1){
			color[i] = b[i];
			vis[b[i]] = 1;
			//dfs1(a[i]);
		}
		else if(a[i]==b[i]){
			color[i] = b[i];
			vis[b[i]] = 1;
			//dfs1(a[i]);
		}
		else{
			g[a[i]].push_back(Edge{a[i], b[i],i});
			g[b[i]].push_back(Edge{b[i], a[i],i});
		}
	}
	for(int i = 1; i <= n; i++) if(vis[i] && !set[i]) dfs1(i);
	//printf("break\n");
	for(int i = 1; i <= n; i++){
		if(!vis[i]){
			//printf("i = %d\n",i);
			int find = -1;
			int u = i;
			for(Edge e: g[u]){
				if(!color[e.id]){
					find = e.id;
					break;
				}
			}
			if(find==-1){
				break;
			}
			else{
				top = 0;
				//printf("find = %d: %d %d\n",find,a[find],b[find]);
				color[find] = a[find];
				dfs(a[find]);
				int bad = !vis[b[find]];
				if(bad){
					top = 0;
					//printf("haha\n");
					color[find] = b[find];
					dfs(b[find]);
					int bb = !vis[a[find]];
					if(bb) break;
					else dfs1(b[find]);
				}
				else dfs1(a[find]);
			}
		}
	}
	int find = 0;
	for(int i = 1; i <= n; i++){ 
		if(!vis[i]) find = 1;
	}
	if(find) printf("No\n");
	else{
		printf("Yes\n");
		for(int i = 1; i <= n; i++) printf("%d\n",color[i]);
	}
	return 0;
}
0