結果

問題 No.3552 Triangular Coloring
コンテスト
ユーザー ぽえ
提出日時 2026-05-07 12:09:55
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 1,021 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,551 ms
コンパイル使用メモリ 342,576 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2026-05-22 21:49:01
合計ジャッジ時間 7,363 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
	int n, m; cin >> n >> m;
	vector<vector<int>> g(n);
	vector<int> deg(n);
	for (int i=0; i<m; i++) {
		int x, y; cin >> x >> y; x--; y--;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	queue<int> q;
	vector<char> visited(n);
	for (int i=0; i<n; i++) if (deg[i] == 3) q.push(i);
	stack<int> s;
	while (!q.empty()) {
		auto x = q.front(); q.pop();
		if (visited[x]) continue;
		visited[x] = true;
		s.push(x);
		for (auto& y : g[x]) if (!visited[y]) {
			deg[y]--;
			if (deg[y] == 3) q.push(y);
		}
	}
	vector<int> ans(n);
	{
		int x=1;
		for (int i=0 ;i<n; i++) if (!visited[i]) ans[i] = x++;
	}
	while (!s.empty()) {
		auto x = s.top(); s.pop();
		int mask = 0;
		for (auto& y : g[x]) if (ans[y] != 0) mask |= 1<<ans[y];
		for (int i=1; i<=4; i++) if (!((mask>>i)&1)) {
			ans[x] = i;
			break;
		}
	}
	cout << "Yes" << endl;
	for (int i=0; i<n; i++) cout << ans[i] << " \n"[i==n-1];
}
0