結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,984 KB
testcase_01 AC 2 ms
5,988 KB
testcase_02 AC 3 ms
5,988 KB
testcase_03 AC 3 ms
6,064 KB
testcase_04 AC 31 ms
7,304 KB
testcase_05 AC 46 ms
13,900 KB
testcase_06 AC 3 ms
6,088 KB
testcase_07 WA -
testcase_08 AC 3 ms
6,004 KB
testcase_09 AC 3 ms
5,972 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 9 ms
6,796 KB
testcase_31 AC 81 ms
11,760 KB
testcase_32 AC 57 ms
11,972 KB
testcase_33 AC 34 ms
9,812 KB
testcase_34 AC 45 ms
11,336 KB
testcase_35 AC 34 ms
9,976 KB
testcase_36 AC 10 ms
7,012 KB
testcase_37 AC 12 ms
7,568 KB
testcase_38 AC 65 ms
11,284 KB
testcase_39 AC 23 ms
8,340 KB
testcase_40 AC 22 ms
8,336 KB
testcase_41 AC 47 ms
10,464 KB
testcase_42 AC 26 ms
8,452 KB
testcase_43 AC 29 ms
8,924 KB
testcase_44 AC 26 ms
9,032 KB
testcase_45 AC 20 ms
8,440 KB
testcase_46 AC 10 ms
7,124 KB
testcase_47 AC 7 ms
6,748 KB
testcase_48 AC 72 ms
12,312 KB
testcase_49 AC 5 ms
6,248 KB
testcase_50 AC 2 ms
6,020 KB
testcase_51 AC 3 ms
6,052 KB
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;

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){
	vis[u] = 1;
	for(Edge e: g[u]){
		int v = e.v, id = e.id;
		if(!color[id] && !vis[v]){
			color[id] = v;
			vis[v] = 1;
			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]){
			
			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 = 0;
				for(int j = 1; j <= top; j++){
					
					if(!vis[stack[j]]) bad = 1;
					vis[stack[j]] = 0;
				}
				if(bad){
					top = 0;
					//printf("haha\n");
					color[find] = b[find];
					dfs(b[find]);
					int bb = 0;
					for(int j = 1; j <= top; j++){
						if(!vis[stack[j]]){ 
							//printf("now = %d\n",stack[j]);
							bb = 1;
						}
						vis[stack[j]] = 0;
					}
					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