hdu1054Strategic Game

简单的二分匹配,由于是树故必不存在奇数回路,如果是图应该先染色判断能否构成二分图,这题数据貌似异常强大

#include <iostream>
using namespace std;
const int size = 1510;
struct node
{
       int u, v, next;      
}mapp1[size*size];
int id;
int head[size*size];
void init()
{
     id = 0, memset(head, -1, sizeof(head));    
}
void addedge(int u, int v)
{
     mapp1[id].u = u, mapp1[id].v = v, mapp1[id].next = head[u], head[u] = id ++;
}
int match[size];
bool visited[size];
bool dfs(int x, int n)
{
     for (int i = head[x]; i != -1; i = mapp1[i].next){
         if (visited[mapp1[i].v])continue;
         visited[mapp1[i].v] = true;
         if (dfs(match[mapp1[i].v], n) || match[mapp1[i].v] == -1){
            match[mapp1[i].v] = x;
            return true;                  
         }   
     }
     return false;
}
int main()
{
    int n;
    while (scanf("%d", &n) != EOF){
          init();
          for (int i = 0; i < n; i ++){
              int a, k;
              scanf("%d:(%d)", &a, &k);
              while (k --){
                    int b;
                    scanf("%d", &b);
                    addedge(a, b);
                    addedge(b, a); 
              }
          }
          int ans = 0;
          memset(match, -1, sizeof(match));
          for (int i = 0; i < n; i ++){
              memset(visited, false, sizeof(visited));
              if (dfs(i, n))ans ++;   
          } 
          printf("%d\n", ans/2);
    }
    return 0;   
}

你可能感兴趣的:(struct)