Codeforces 247D Mike and Fish

Mike and Fish

我们可以把这个模型转换一下就变成有两类点,一类是X轴, 一类是Y轴, 每个点相当于对应的点之间建一条边,

如果这条边变红两点同时+1, 变蓝两点同时-1。 

我们能发现这个图其实是个二分图, 我们可以随便取一个点开始走路, 红蓝间隔开来,那么中间的点就权值不变,

对于最末尾的点虽然权值有改变,但是只会改变一次, 就这样一直走路直到所有的边都遍历完。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 4e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

int n, ans[N], color[N];

set<PII> G[N];

void dfs(int u, int op) {
    color[u] += -op;
    if(!G[u].empty()) {
        PII e = *G[u].begin();
        G[u].erase(G[u].begin());
        G[e.fi].erase(mk(u, e.se));
        color[u] += op;
        ans[e.se] = op;
        dfs(e.fi, -op);
    }
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        int x, y; scanf("%d%d", &x, &y);
        G[x].insert(mk(y + 200000, i));
        G[y + 200000].insert(mk(x, i));
    }
    for(int i = 1; i <= 400000; i++) {
        while(!G[i].empty()) {
            PII e = *G[i].begin();
            G[i].erase(G[i].begin());
            G[e.fi].erase(mk(i, e.se));
            if(color[i] <= 0) {
                color[i]++;
                ans[e.se] = 1;
                dfs(e.fi, -1);
            } else {
                color[i]--;
                ans[e.se] = -1;
                dfs(e.fi, 1);
            }
        }
    }
    for(int i = 1; i <= n; i++) printf("%c", ans[i] == 1 ? 'b' : 'r');
    puts("");
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10468608.html