結果

問題 No.1640 簡単な色塗り
ユーザー tpc011854tpc011854
提出日時 2021-08-06 22:58:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,146 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 49,996 KB
実行使用メモリ 33,792 KB
最終ジャッジ日時 2023-09-12 03:05:10
合計ジャッジ時間 8,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
21,456 KB
testcase_01 AC 7 ms
21,400 KB
testcase_02 AC 8 ms
25,548 KB
testcase_03 AC 8 ms
25,648 KB
testcase_04 AC 34 ms
25,536 KB
testcase_05 AC 52 ms
31,036 KB
testcase_06 AC 7 ms
25,568 KB
testcase_07 AC 8 ms
25,516 KB
testcase_08 AC 8 ms
25,504 KB
testcase_09 AC 7 ms
25,436 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 14 ms
26,220 KB
testcase_31 AC 73 ms
32,316 KB
testcase_32 AC 56 ms
32,684 KB
testcase_33 AC 40 ms
30,792 KB
testcase_34 AC 48 ms
32,088 KB
testcase_35 AC 39 ms
30,940 KB
testcase_36 AC 14 ms
26,328 KB
testcase_37 AC 17 ms
26,744 KB
testcase_38 AC 64 ms
31,952 KB
testcase_39 AC 29 ms
29,508 KB
testcase_40 AC 29 ms
27,396 KB
testcase_41 AC 51 ms
31,348 KB
testcase_42 AC 31 ms
27,548 KB
testcase_43 AC 35 ms
29,976 KB
testcase_44 AC 31 ms
30,200 KB
testcase_45 AC 26 ms
29,556 KB
testcase_46 AC 15 ms
26,380 KB
testcase_47 AC 12 ms
26,164 KB
testcase_48 AC 74 ms
32,752 KB
testcase_49 AC 9 ms
25,608 KB
testcase_50 AC 8 ms
25,496 KB
testcase_51 AC 7 ms
21,480 KB
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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(){
	//printf("haha\n");
	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});
		}
	}
	//printf("ok\n");
	//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]];
				for(int j = 1; j <= top; j++) vis[stack[j]] = 0;
				if(bad){
					top = 0;
					//printf("haha\n");
					color[find] = b[find];
					dfs(b[find]);
					int bb = !vis[a[find]];
					for(int j = 1; j <= top; j++) 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