Codeforces 547D Mike and Fish Euler path, codeforces547d
Question link: Click the open link
Question:
Coordinates of n points on a two-dimensional plane
Q:
Color each vertex in red or blue, so that the number of red and blue points in horizontal (or vertical) is no more than 1.
Ideas:
Let's create a two-part graph. set X to the X axis and set Y to the Y axis.
That point () is the edge connecting from 1 to 5 of the y set of x.
In this case, vertices are represented by edges, and our task is to dye edges.
Make:
For any vertex in a two-part graph, the number of red and blue edges connected by the vertex cannot exceed 1.
We can think that the inbound edge of this vertex is red and the outbound edge is blue. Obviously, this is an Euler's path.
Therefore, search for the Euler's path.
#include <iostream>#include <fstream>#include <string>#include <time.h>#include <vector>#include <map>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <set>#include <vector>using namespace std;template <class T>inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template <class T>inline void pt(T x) {if (x <0) {putchar('-');x = -x;}if (x>9) pt(x / 10);putchar(x % 10 + '0');}const int N = 2e5 + 10;typedef long long ll;struct Edge{int from, to, col, nex;}edge[N << 2];int head[N<<1], edgenum;void add(int u, int v){Edge E = { u, v, -1, head[u] };edge[edgenum] = E;head[u] = edgenum++;}int n;int vis[N << 2];int du[N << 2];void dfs(int u){//printf("%d ", u);for (int i = head[u]; ~i; i = head[u] = edge[i].nex){if (edge[i].col != -1)continue;int v = edge[i].to;edge[i].col = u < v;edge[i ^ 1].col = -2;vis[u]++; vis[v]++;dfs(v);break;}}vector<int>X, Y;int x[N], y[N];int main(){rd(n);memset(head, -1, sizeof head); edgenum = 0;for (int i = 1; i <= n; i++){rd(x[i]); rd(y[i]);X.push_back(x[i]); Y.push_back(y[i]);}sort(X.begin(), X.end());sort(Y.begin(), Y.end());X.erase(unique(X.begin(), X.end()), X.end());Y.erase(unique(Y.begin(), Y.end()), Y.end());for (int i = 1; i <= n; i++){x[i] = lower_bound(X.begin(), X.end(), x[i]) - X.begin() ;y[i] = lower_bound(Y.begin(), Y.end(), y[i]) - Y.begin() + n;du[x[i]]++; du[y[i]]++;add(x[i], y[i]); add(y[i], x[i]);}//for (int i = 0; i <= 2 * n; i++)printf("(%d,%d)\n", i, du[i]); for (int i = 0; i <= 2 * n; i++)if (du[i] & 1 && vis[i] < du[i]){//printf("odd dfs %d: ", i);dfs(i);//puts("");}for (int i = 0; i <= 2 * n; i++)if (du[i] && vis[i] < du[i]){//printf("normal dfs %d: ", i);dfs(i);//puts("");}for (int i = 0; i < edgenum; i ++)if (edge[i].col >= 0)putchar(edge[i].col ? 'r' : 'b');return 0;}/*41 11 22 12 271 02 02 10 21 22 22 3*/