首先题目中两个明显的限制:
相邻两个数必须不同分组
相同的数必须分组
那么依据这两个限制来建边并且染色就好了(用一个哈希表来离散化)
#include <bits/stdc++.h>
using namespace std;
#define gogo ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const string YES = "YES";
const string NO = "NO";
const int N = 2e5 + 10;
void run() {
int m, n;
cin >> m;
vector<int> edge[N];
vector<int> t(m + 1);
map<int, int> hs;
int tot = 0;
int cnt = 0;
for (int i = 1;i <= m;i ++) {
cin >> t[i];
cnt += t[i];
for (int j = 1;j <= t[i];j ++) {
tot ++;
int s;
cin >> s;
if (hs[s]) {
edge[hs[s]].push_back(tot);
edge[tot].push_back(hs[s]);
hs[s] = 0;
} else
hs[s] = tot;
if (j & 1) {
edge[tot].push_back(tot + 1);
edge[tot + 1].push_back(tot);
}
}
}
vector<char> ans(cnt + 10);
for (auto item : hs)
if (item.second) {
cout << NO << endl;
return;
}
auto dfs = [&] (int u, int id, auto&& dfs) -> void {
if (id) ans[u] = 'R';
else ans[u] = 'L';
for (auto v : edge[u])
if (!ans[v])
dfs(v, id ^ 1, dfs);
};
for (int i = 1;i <= tot;i ++)
if (!ans[i])
dfs(i, 0, dfs);
cout << YES << endl;
tot = 0;
for (int i = 1;i <= m;i ++) {
for (int j = 1;j <= t[i];j ++)
cout << ans[++tot];
cout << endl;
}
}
int32_t main() {
gogo;
run();
return 0;
}
第一次做2400的题hhh,水平太低