Codeforces 547D Mike and Fish Euler path

Source: Internet
Author: User

Codeforces 547D Mike and Fish Euler path

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 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include
      #include 
       
        #include #include 
        
         #include 
         
          #include 
          
           #include 
           
            using namespace std;template 
            
             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 
             
              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
              
               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*/
              
             
            
           
          
         
        
       
     
    
   
  
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.