URAL 1099. Work Scheduling 一般图匹配带花树


一般图匹配带花树模版题:

将奇环缩成圈(Blossom),然后找增广路.....


1099. Work Scheduling

Time limit: 0.5 second
Memory limit: 64 MB
There is certain amount of night guards that are available to protect the local junkyard from possible junk robberies. These guards need to scheduled in pairs, so that each pair guards at different night. The junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired). Please note that each guard can be scheduled with only one of his colleagues and no guard can work alone.

Input

The first line of the input contains one number  N ≤ 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs ( ij) follow, each such pair means that guard # i and guard # j can work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards i.e. helmets, pants, jackets. It is impossible to put small helmet on a guard with a big head or big shoes on guard with small feet). The input ends with Eof.

Output

You should output one possible optimal assignment. On the first line of the output write the even number  C, the amount of scheduled guards. Then output  C/2 lines, each containing 2 integers ( ij) that denote that  i and  j will work together.

Sample

input output
3
1 2
2 3
1 3
2
1 2
Problem Author: Jivko Ganev
Tags:  graph theory   ( hide tags for unsolved problems )



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn=250;

/*******************************************/

struct Edge
{
  int to,next;
}edge[maxn*maxn];

int Adj[maxn],Size;

void init()
{
  memset(Adj,-1,sizeof(Adj)); Size=0;
}

void add_edge(int u,int v)
{
  edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
}

/*******************************************/

int n;
int Match[maxn];
bool G[maxn][maxn];
int Start,Finish,NewBase;
int Father[maxn],Base[maxn];
bool InQueue[maxn],InPath[maxn],InBlossom[maxn];
int Count;
queue<int> q;


int FindCommonAncestor(int u,int v)
{
  memset(InPath,false,sizeof(InPath));
  while(true)
    {
      u=Base[u];
      InPath[u]=true;
      if(u==Start) break;
      u=Father[Match[u]];
    }
  while(true)
    {
      v=Base[v];
      if(InPath[v]) break;
      v=Father[Match[v]];
    }
  return v;
}

void ResetTrace(int u)
{
  int v;
  while(Base[u]!=NewBase)
    {
      v=Match[u];
      InBlossom[Base[u]]=InBlossom[Base[v]]=true;
      u=Father[v];
      if(Base[u]!=NewBase) Father[u]=v;
    }
}

void BlosomContract(int u,int v)
{
  NewBase=FindCommonAncestor(u,v);
  memset(InBlossom,false,sizeof(InBlossom));
  ResetTrace(u);
  ResetTrace(v);
  if(Base[u]!=NewBase) Father[u]=v;
  if(Base[v]!=NewBase) Father[v]=u;
  for(int tu=1;tu<=n;tu++)
    {
      if(InBlossom[Base[tu]])
        {
          Base[tu]=NewBase;
          if(!InQueue[tu])
            {
              q.push(tu);
              InQueue[tu]=true;
            }
        }
    }
}

void FindAugmentingPath()
{
  memset(InQueue,false,sizeof(InQueue));
  memset(Father,0,sizeof(Father));
  for(int i=1;i<=n;i++)
    Base[i]=i;
  while(!q.empty()) q.pop();
  q.push(Start); InQueue[Start]=true;
  Finish=0;

  while(!q.empty())
    {
      int u=q.front(); //InQueue[u]=false;
      q.pop();
      for(int i=Adj[u];~i;i=edge[i].next)
        {
          int v=edge[i].to;
          if((Base[u]!=Base[v])&&Match[u]!=v)
            {
              if(v==Start||(Match[v]>0&&Father[Match[v]]>0))
                BlosomContract(u,v);
              else if(Father[v]==0)
                {
                  Father[v]=u;
                  if(Match[v]>0)
                    {
                      q.push(Match[v]);
                      InQueue[Match[v]]=true;
                    }
                  else
                    {
                      Finish=v;
                      return ;
                    }
                }
            }
        }
    }
}

void AugmentPath()
{
  int u,v,w;
  u=Finish;
  while(u>0)
    {
      v=Father[u];
      w=Match[v];
      Match[v]=u;
      Match[u]=v;
      u=w;
    }
}

void Edmonds()
{
  memset(Match,0,sizeof(Match));
  for(int u=1;u<=n;u++)
    {
      if(Match[u]==0)
        {
          Start=u;
          FindAugmentingPath();
          if(Finish>0) AugmentPath();
        }
    }
}

void PrintMatch()
{
  Count=0;
  for(int i=1;i<=n;i++)
    {
      if(Match[i]) Count++;
    }
  printf("%d\n",Count);
  for(int u=1;u<=n;u++)
    {
      if(u<Match[u])
        printf("%d %d\n",u,Match[u]);
    }
}

int main()
{
  //freopen("data.in","r",stdin);
  while(scanf("%d",&n)!=EOF)
    {
      init();
      memset(G,false,sizeof(G));
      int u,v;
      while(scanf("%d%d",&u,&v)!=EOF)
        {
          if(G[u][v]==true) continue;
          G[u][v]=G[v][u]=true;
          add_edge(u,v);
          add_edge(v,u);
        }
      Edmonds();
      PrintMatch();
    }
  return 0;
}


你可能感兴趣的:(URAL 1099. Work Scheduling 一般图匹配带花树)