Fleury algorithm look here http://hihocoder.com/problemset/problem/1181
Each point is treated as an edge, and each horizontal ordinate is viewed as a point, resulting in an no-show graph.
If the degree of each point in the new diagram is an even number, then it is a Eulerian graph, to run over the graph of the Euler circuit, the passing edge of the dye, you can ensure that each point is connected to the edge of the red and blue color equal.
If there is an odd number of points, create a new two points A and B. The degree of the horizontal axis is an odd point and a edge, the ordinate is an odd point and a b edge, so that a maximum of only A and b are odd degrees, you can run Euler path.
Pay attention to the Fleury algorithm, the time to delete the visited side (really delete instead of marking), otherwise repeated access will lead to the complexity of soaring.
#include <cstdio> #include <cstring> #include <vector>using namespace std;struct edge{int v,id;}; Vector<edge>g[400010];int n,s,t;bool anss[200010],pen,vis[600010];inline void dfs (int U) {while (! G[u].empty ()) {Edge e=g[u].back (); G[u].pop_back (), if (!vis[e.id]) {Vis[e.id]=1;dfs (E.V); if (e.id<=n) {anss[e.id]=pen;pen^=1;}}} int main () {//freopen ("c.in", "R", stdin); int x,y;scanf ("%d", &n); s=400001; t=400002;for (int i=1;i<=n;++i) {scanf ("%d%d", &x,&y); G[x].push_back (Edge) {y+200000,i}); G[y+200000].push_back (Edge) {x,i});} int cnt=n;for (int i=1;i<=200000;++i) {if (G[i].size () &1) {G[s].push_back ((Edge) {i,++cnt}); G[i].push_back ((Edge) {s,cnt});}} for (int i=200001;i<=400000;++i) {if (G[i].size () &1) {G[i].push_back (Edge) {t,++cnt}); G[t].push_back ((Edge) {i,cnt});}} if (G[s].size () &1) {DFS (S);} if (! G[t].empty ()) {DFS (T);} for (int i=1;i<=200000;++i) {if (! G[i].empty ()) {DFS (i);}} for (int i=1;i<=n;++i) {Putchar (anss[i]? ' R ': ' B ');} Puts (""); return 0;}
"Euler loop" "Euler path" "Fleury algorithm" CDOJ1634 remember the Little Apple, two center of gravity character Luo clothes