結果
| 問題 | No.3552 Triangular Coloring |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-22 23:37:16 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,079 ms / 2,000 ms |
| コード長 | 3,019 bytes |
| 記録 | |
| コンパイル時間 | 1,802 ms |
| コンパイル使用メモリ | 239,220 KB |
| 実行使用メモリ | 78,216 KB |
| 最終ジャッジ日時 | 2026-05-22 23:37:26 |
| 合計ジャッジ時間 | 9,177 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
| 純コード判定待ち |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 17 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N,M; cin >> N >> M;
vector<set<int>> Graph(N);
for(int i=0; i<M; i++){
int u,v; cin >> u >> v;
u--; v--;
Graph.at(u).insert(v);
Graph.at(v).insert(u);
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> Q;
vector<int> V(N);
for(int i=0; i<N; i++){
Q.push({Graph.at(i).size(),i});
V.at(i) = Graph.at(i).size();
}
vector<tuple<int,int,int,int>> T;
while(T.size() < N-2){
auto [v,pos] = Q.top(); Q.pop();
if(V.at(pos) != v) continue;
if(v == 3){
auto itr = Graph.at(pos).begin();
int v1 = pos;
int v2 = *itr; itr++;
int v3 = *itr; itr++;
int v4 = *itr; itr++;
Graph.at(v2).erase(v1);
Graph.at(v3).erase(v1);
Graph.at(v4).erase(v1);
V.at(v2)--,V.at(v3)--,V.at(v4)--,V.at(v1) -= 3;
Q.push({V.at(v2),v2});
Q.push({V.at(v3),v3});
Q.push({V.at(v4),v4});
T.push_back({v1,v2,v3,v4});
}
else if(v == 2){
auto itr = Graph.at(pos).begin();
int v1 = pos;
int v2 = *itr; itr++;
int v3 = *itr; itr++;
T.push_back({v1,v2,v3,-1});
}
}
vector<int> C(N,-1),left(N-2,4);
left.at(N-3) = 0;
vector<vector<int>> Group(N);
for(int i=0; i<N-3; i++){
auto [v1,v2,v3,v4] = T.at(i);
Group.at(v1).push_back(i);
Group.at(v2).push_back(i);
Group.at(v3).push_back(i);
Group.at(v4).push_back(i);
}
{
stack<int> st;
auto paint = [&](int pos,int c) -> void {
C.at(pos) = c;
for(auto g : Group.at(pos)){
left.at(g)--;
if(left.at(g) == 1) st.push(g);
}
};
{
auto [v1,v2,v3,v4] = T.back();
paint(v1,0);
paint(v2,1);
paint(v3,2);
}
while(st.size()){
auto g = st.top(); st.pop();
if(left.at(g) == 0) continue;
assert(left.at(g) == 1);
auto [v1,v2,v3,v4] = T.at(g);
int use = 15;
if(C.at(v1) != -1) use -= 1<<C.at(v1);
if(C.at(v2) != -1) use -= 1<<C.at(v2);
if(C.at(v3) != -1) use -= 1<<C.at(v3);
if(C.at(v4) != -1) use -= 1<<C.at(v4);
if(use == 1) use = 0;
else if(use == 2) use = 1;
else if(use == 4) use = 2;
else use = 3;
if(C.at(v1) == -1) paint(v1,use);
if(C.at(v2) == -1) paint(v2,use);
if(C.at(v3) == -1) paint(v3,use);
if(C.at(v4) == -1) paint(v4,use);
}
}
cout << "Yes\n";
for(auto c : C) cout << c+1 << " "; cout << "\n";
}