Codeforces #305 D Mike and Fish

Source: Internet
Author: User

The positive solution looks like a big mob search?

First we consider that this is a binary graph, and it is easy to derive an algorithm after the network flow model is established.

S-> line capacity is NUM[X]/2;

Row-and-column capacity is 1 and requires (x, y) this point exists

Column->t capacity is NUM[Y]/2

After we ran the network stream, we got a set of solutions.

But we consider the output scenario

For each row, if NUM[X] is an even number, then it is clear that the output scheme is correct

But if NUM[X] is an odd number, the extra one is obviously both red and probably blue.

But the point is that we can't be sure that he's red or blue, because his state also affects the corresponding columns.

The same is true for column considerations.

So we have an odd number for the residual network, if NUM[X] or num[y], then even the corresponding capacity is 1

A network stream is carried out on the residual network, so that you can export the scheme spicy

(saying that the network stream 40w point is not a bit empty)

#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include < algorithm> #include <queue> #define H (i) i#define L (i) i+200000using namespace Std;const int Maxn=200010;const int oo=0x7fffffff;int s,t;int n,cntx,cnty;int x[maxn],y[maxn];int nx[maxn],ny[maxn];int idx[maxn],idy[maxn];bool VISX [Maxn],visy[maxn];int h[maxn<<1],cnt=1;int cur[maxn<<1];struct edge{int to,next,w;} G[4000010];int vis[maxn<<1];queue<int>q;void Add (int x,int y,int z) {++cnt; G[cnt].to=y; G[CNT].NEXT=H[X]; g[cnt].w=z;h[x]=cnt;++cnt; G[cnt].to=x; G[cnt].next=h[y]; g[cnt].w=0;h[y]=cnt;} void read (int &num) {Num=0;char Ch=getchar (); while (ch< '! ') Ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') num=num*10+ch-' 0 ', Ch=getchar ();} BOOL BFS () {memset (vis,-1,sizeof (VIS)); Q.push (S); Vis[s]=1;while (! Q.empty ()) {int U=q.front (); Q.pop (); for (int i=h[u];i;i=g[i].next) {int v=g[i].to;if (vis[v]==-1&&g[i].w>0) {vis[v]=vis[u]+1; Q.push (v);}} return vis[t]!=-1;} Int DFS (int x,int f) {if (x==t| | f==0) return f;int w,used=0;for (int i=cur[x];i;i=g[i].next) {if (vis[g[i].to]==vis[x]+1) {W=f-used;w=dfs (G[i].to,min ( W,G[I].W)); G[i].w-=w; G[i^1].w+=w;if (g[i].w>0) cur[x]=i;used+=w;if (used==f) return used;} if (!used) Vis[x]=-1;return used;} void Dinic () {while (BFS ()) {for (int i=s;i<=t;++i) cur[i]=h[i];D FS (S,oo);} return;} int main () {read (n); s=0; t=400001;for (int i=1;i<=n;++i) {read (x[i]); Read (Y[i]); nx[x[i]]++; ny[y[i]]++;} for (int i=1;i<=n;++i) Add (H (X[i]), L (Y[i]), 1), for (int i=1;i<=n;++i) {if (!visx[x[i]]) {Add (S,h (X[i]), Nx[x[i]] >>1); visx[x[i]]=true;} if (!visy[y[i]]) {Add (L (Y[i]), t,ny[y[i]]>>1); visy[y[i]]=true;}} Dinic (); memset (Visx,false,sizeof (VISX)); Memset (Visy,false,sizeof (Visy)); for (int i=1;i<=n;++i) {if (!visx[x[i]] {visx[x[i]]=true;if (nx[x[i]]&1) Add (S,h (X[i]), 1);} if (!visy[y[i]]) {visy[y[i]]=true;if (ny[y[i]]&1) Add (L (Y[i]), t,1);}} Dinic (); for (int i=1;i<=n;++i) {if (! g[(i<<1) +1].W) printf ("B"), Else printf ("R");} return 0;}

  

Codeforces #305 D Mike and Fish

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.