1. 程式人生 > >Codeforces 247D Mike and Fish

Codeforces 247D Mike and Fish

遍歷 模型 set .com 但是 problem pan div define

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; } /* */

Codeforces 247D Mike and Fish