Poj1637Sightseeing tour (hybrid plot Euler loop)

Source: Internet
Author: User

Calculate the Euler loop of the hybrid graph. Question Analysis: Maximum stream. I used the network flow to find the Euler's loop of the hybrid graph. The rising position was ah .. In fact, this is also the case for careful consideration. The Euler loop is a loop that traverses all edges and returns to the starting point. As long as each vertex degree is an even number, the directed graph must ensure that the inbound degrees of all vertices are equal to the outbound degrees. If you want to find the path, you can use dfs. Hybrid graphs are complicated. First, the directed edge is used to obtain the inbound and outbound degrees of all points. If the difference between the inbound and outbound degrees of a certain point is odd, there must be no Euler's loop, for a hybrid graph, the undirected edge can specify any direction, But no matter which direction is specified, if the reverse direction is obtained, only one outbound degree and One inbound degree of the endpoint are affected, therefore, no matter how the undirected edge is targeted, it does not affect the parity between the inbound and outbound node. After the undirected edge is converted into a directed graph, all the vertices are divided into three categories: 1: Point of entry = degree of exit, which is already a balance point, no matter; 2: inbound> outbound vertex. An edge is built to the vertex. The Edge Weight is (inbound-outbound)/2. 3: Inbound <outbound vertex, create an edge between the Source Vertex and it. The Edge Weight is (outbound-Inbound)/2. Run the maximum stream once to check whether the stream is full. If the stream is full, there is an Euler loop. Because if a full stream is run, there are x edges in each inbound> outbound vertex, then the x edges are reversed, the entry level of the node is equal to the exit level and balanced. The same is true for each entry level> the inbound level. For the vertices whose degree of output is equal to the degree of entry, because they are not concerned with the plot, that is, they are originally the equilibrium point, so there is no direct edge between the source and sink points, but it does not mean that these vertices are not in the graph, because the non-equilibrium points are connected with edges. If a specific Euler loop is required, you only need to look at the specific network stream. For the side with the traffic of 1, the inverse is an edge in the Euler loop. The so-called inverse is only for the undirected edge. It indicates that the inverse is initially set for the undirected edge. For details, see the code:

#include <iostream>  #include<cstdio>  #include<cstring>  #include<algorithm>  #include<cmath>  using namespace std;  const int N = 205;  const int M = 40000;  const int inf = 0x3f3f3f3f;    int n,m,num,sum;  int head[N],sta[N],que[N],cnt[N],dis[N],rpath[N];  int in[N],out[N];  struct node  {      int to,c,next,pre;  }arc[M];  void build(int s,int e,int cap)  {      arc[num].to = e;      arc[num].c = cap;      arc[num].next = head[s];      head[s] = num ++;      arc[num - 1].pre = num;      arc[num].pre = num - 1;      arc[num].to = s;      arc[num].c = 0;      arc[num].next = head[e];      head[e] = num ++;  }  void init()  {      int i,a,b,d;      scanf("%d%d",&n,&m);      for(i = 1;i <= n;i ++)          in[i] = out[i] = 0;      memset(head,-1,sizeof(head));      num = 0;      while(m --)      {          scanf("%d%d%d",&a,&b,&d);          if(d == 0)              build(a,b,1);          out[a] ++;          in[b] ++;      }  }  void re_Bfs()  {      int i,front,rear;      for(i = 0;i <= n + 1;i ++)      {          dis[i] = n + 2;          cnt[i] = 0;      }      dis[n + 1] = 0;      cnt[0] = 1;      front = rear = 0;      que[rear ++] = n + 1;      while(front != rear)      {          int u = que[front ++];          for(i = head[u];i != -1;i = arc[i].next)          {              if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < n + 2)                  continue;              dis[arc[i].to] = dis[u] + 1;              cnt[dis[arc[i].to]] ++;              que[rear ++] = arc[i].to;          }      }  }  int ISAP()  {      re_Bfs();      int i,u,maxflow = 0;      for(i = 0;i <= n + 1;i ++)          sta[i] = head[i];      u = 0;      while(dis[0] < n + 2)      {          if(u == n + 1)          {              int curflow = inf;              for(i = 0;i != n + 1;i = arc[sta[i]].to)                  curflow = min(curflow,arc[sta[i]].c);              for(i = 0;i != n + 1;i = arc[sta[i]].to)              {                  arc[sta[i]].c -= curflow;                  arc[arc[sta[i]].pre].c += curflow;              }              maxflow += curflow;              u = 0;          }          for(i = sta[u];i != -1;i = arc[i].next)              if(arc[i].c > 0 && dis[arc[i].to] + 1 == dis[u])                  break;          if(i != -1)          {              sta[u] = i;              rpath[arc[i].to] = arc[i].pre;              u = arc[i].to;          }          else          {              if((-- cnt[dis[u]]) == 0)                  break;              int Min = n + 2;              sta[u] = head[u];              for(i = head[u];i != -1;i = arc[i].next)                  if(arc[i].c > 0)                      Min = min(Min,dis[arc[i].to]);              dis[u] = Min + 1;              cnt[dis[u]] ++;              if(u != 0)                  u = arc[rpath[u]].to;          }      }      return maxflow;  }  bool solve()  {      int i;      sum = 0;      for(i = 1;i <= n;i ++)      {          if(in[i] > out[i])          {              if((in[i] - out[i])&1)                  return false;              build(i,n + 1,(in[i] - out[i])>>1);          }          if(in[i] < out[i])          {              if((out[i] - in[i])&1)                  return false;              build(0,i,(out[i] - in[i])>>1);              sum += (out[i] - in[i])>>1;          }      }      return ISAP() == sum;  }  int main()  {      int t;      scanf("%d",&t);      while(t --)      {          init();          if(solve())              puts("possible");          else              puts("impossible");      }      return 0;  }  //200K  0MS  

 


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.