zoukankan      html  css  js  c++  java
  • 【hdu 1068】Girls and Boys

    Link:http://acm.hdu.edu.cn/showproblem.php?pid=1068

    Description

    有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该集合中人的最大个数。

    Solution

    最大独立子集问题;
    等于节点个数-最小点覆盖.
    写之前做一下染色,从0开始写最大匹配.

    NumberOf WA

    0

    Reviw


    Code

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define ri(x) scanf("%d",&x)
    #define rl(x) scanf("%lld",&x)
    #define rs(x) scanf("%s",x+1)
    #define oi(x) printf("%d",x)
    #define ol(x) printf("%lld",x)
    #define oc putchar(' ')
    #define os(x) printf(x)
    #define all(x) x.begin(),x.end()
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 1000;
    
    int n,color[N+10],Try[N+10],pre[N+10];
    vector <int> v[2],g[N+10];
    
    void dfs(int x,int c){
        color[x] = c;v[c].pb(x);
        int len = g[x].size();
        rep1(j,0,len-1){
            int y = g[x][j];
            if (color[y]==-1) dfs(y,1-c);
        }
    }
    
    bool hungary(int x){
        int len = g[x].size();
        rep1(i,0,len-1){
            int y = g[x][i];
            if (!Try[y]){
                Try[y] = 1;
                if ( pre[y]==-1 || hungary(pre[y])){
                    pre[y] = x;
                    return true;
                }
            }
        }
        return false;
    }
    
    int main(){
        //Open();
        //Close();
        while (~scanf("%d",&n)){
            rep1(i,1,n) g[i].clear();
            rep1(i,1,n){
                int x,cnt;
                scanf("%d: ",&x);
                x++;
                scanf("(%d)",&cnt);
                rep1(j,1,cnt){
                    int y;
                    ri(y);
                    y++;
                    g[x].pb(y),g[y].pb(x);
                }
            }
    
            rep1(i,0,1) v[i].clear();
            ms(color,255);
            rep1(i,1,n)
                if (color[i]==-1)
                    dfs(i,0);
    
            int len = v[0].size(),ans = 0;
            ms(pre,255);
            rep1(i,0,len-1){
                ms(Try,0);
                if (hungary(v[0][i])) ans++;
            }
            oi(n-ans);puts("");
        }
        return 0;
    }
    
  • 相关阅读:
    Happy Number
    N-Queens
    Palindrome Partitioning
    Linked List Cycle I & II
    leetcode 96: Unique Binary Search Trees java
    cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
    cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum
    355. Design Twitter [classic design]
    400. Nth Digit
    211. Add and Search Word
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626120.html
Copyright © 2011-2022 走看看