Codeforces Round #770 (Div. 2) E. Fair Share

首先题目中两个明显的限制:

  1. 相邻两个数必须不同分组

  1. 相同的数必须分组

那么依据这两个限制来建边并且染色就好了(用一个哈希表来离散化)

#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,水平太低

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值