BZOJ4238: Voltage

Source: Internet
Author: User

If an edge is feasible, then after deleting the edge, the remaining figure is a binary graph and the sides of the edge have the same color.

Then the feasible edge must belong to the intersection of all odd rings, and not belong to any even ring.

Pick a tree, for a non-tree edge, it forms a ring:

If even rings, the edges on the ring are marked as not selectable.

For odd rings, add one to the number of odd rings passing through the edges of the ring.

You can use the tree chain to maintain the prefix and do $o (M\log N) $.

For other rings, it can be obtained from the following three conditions:

1. A singular ring or a even ring to get a new singular ring, the public part must pass through a couple of rings, even if not marked will be abandoned.

2. A singular ring or a singular ring to get a new even ring, the new ring on the edge must not belong to all the odd ring of the intersection, even if not marked will be abandoned.

3. A pair of even rings or a pair of even rings is given a new even ring, and the public part must pass through a couple of rings and be marked, so there is no need to mark again.

So just consider all 22 different or can represent the base ring of all rings.

#include <cstdio> #define N 300010int n,m,i,x,y,f[n],g[n],nxt[n<<1],v[n<<1],ed,son[n],size[n],d[n ],dis[n],top[n],loc[n],dfn,s[2][n];int e[n][2],need[n],ban[n],cnt,ans;int F (int x) {return f[x]==x?x:f[x]=f (f[x]);}  inline void Add (int x,int y) {v[++ed]=y;nxt[ed]=g[x];g[x]=ed; v[++ed]=x;nxt[ed]=g[y];g[y]=ed;}  void Dfs (int x,int y) {size[x]=1,d[x]=d[f[x]=y]+1,dis[x]=dis[y]+ (x<=n);    for (int i=g[x];i;i=nxt[i]) if (v[i]!=y) {DFS (v[i],x); size[x]+=size[v[i];  if (Size[v[i]]>size[son[x]]) son[x]=v[i];  }}void dfs2 (int x,int y) {top[x]=y;loc[x]=++dfn;  if (Son[x]) DFS2 (son[x],y); for (int i=g[x];i;i=nxt[i]) if (v[i]!=son[x]&&v[i]!=f[x]) DFS2 (V[i],v[i]);}     inline void Modify (int x,int Y,int p) {for (; Top[x]!=top[y];x=f[top[x]]) {if (D[top[x]]<d[top[y]]) {int z=x;x=y;y=z;}  s[p][loc[top[x]]]++,s[p][loc[x]+1]--;  } if (D[x]<d[y]) {int z=x;x=y;y=z;} s[p][loc[y]]++,s[p][loc[x]+1]--;} inline void Read (int&a) {char c;while (!) ( ((C=getchar ()) >= ' 0 ') && (c<= ' 9')); a=c-' 0 '; while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*=10) +=c-' 0 ';}  int main () {read (n), read (m);  for (i=1;i<=n;i++) f[i]=i;    for (i=1;i<=m;i++) {read (x), read (y), e[i][0]=x,e[i][1]=y;  if (F (x)!=f (y)) F[f[x]]=f[y],add (X,i+n), add (y,i+n); else need[i]=1;  } for (i=1;i<=n;i++) if (!d[i]) DFS (i,0), DFS2 (i,i);    for (i=1;i<=m;i++) if (Need[i]) {x=e[i][0],y=e[i][1];  if ((dis[x]&1) ^ (dis[y]&1)) ban[i]=1,modify (x,y,0); else modify (x,y,1), cnt++;  } for (i=2;i<=dfn;i++) s[0][i]+=s[0][i-1],s[1][i]+=s[1][i-1];  for (i=1;i<=m;i++) if (Need[i]) {if (!ban[i]&&cnt==1) ans++;  }else{if (!s[0][loc[i+n]]&&s[1][loc[i+n]]==cnt) ans++; } return printf ("%d", ans), 0;}

  

BZOJ4238: Voltage

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.