zoukankan      html  css  js  c++  java
  • [ZJOI 2009] 假期的宿舍

    [题目链接]

             https://www.lydsy.com/JudgeOnline/problem.php?id=1433

    [算法]

             二分图匹配
    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 55
    
    struct edge
    {
            int to,nxt; 
    } e[MAXN * MAXN];
    
    int i,j,n,T,tot;
    int a[MAXN],b[MAXN],head[MAXN],match[MAXN << 1];
    bool visited[MAXN << 1];
    bool g[MAXN][MAXN];
    bool ans;
    
    template <typename T> inline void read(T &x)
    {
            int f = 1; x = 0;
            char c = getchar();
            for (; !isdigit(c); c = getchar()) 
            {
                    if (c == '-') f = -f;
            }
            for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
            x *= f;
    }
    inline void addedge(int u,int v)
    {
            tot++;
            e[tot] = (edge){v,head[u]};
            head[u] = tot;
    }
    inline bool hungary(int u)
    {
            int i,v;
            visited[u] = true;
            for (i = head[u]; i; i = e[i].nxt)
            {
                    v = e[i].to;
                    if (!visited[v])
                    {
                            visited[v] = true;
                            if (!match[v] || hungary(match[v]))
                            {
                                    match[v] = u;
                                    return true;
                            }
                    }
            }
            return false;
    }
    
    int main() 
    {
            
            read(T);
            while (T--)
            {
                    read(n);
                    tot = 0;
                    for (i = 1; i <= n; i++) 
                    {
                            head[i] = 0;
                            match[i] = match[n + i] = 0;
                    }
                    for (i = 1; i <= n; i++) read(a[i]);
                    for (i = 1; i <= n; i++) read(b[i]);
                    for (i = 1; i <= n; i++)
                    {
                            for (j = 1; j <= n; j++)
                            {
                                    read(g[i][j]);        
                            }        
                    }        
                    for (i = 1; i <= n; i++)
                    {
                            if (a[i] == 1)
                            {
                                    if (b[i] == 1) continue;
                                    for (j = 1; j <= n; j++)
                                    {
                                            if ((i == j) || (a[j] == 1 && g[i][j]))
                                                    addedge(i,j + n);    
                                    }    
                            }    else
                            {
                                    for (j = 1; j <= n; j++)
                                    {
                                            if (a[j] == 1 && g[i][j])
                                                    addedge(i,j + n);
                                    }
                            }
                    }        
                    ans = true;
                    for (i = 1; i <= n; i++)
                    {
                            if ((a[i] == 1 && b[i] == 0) || a[i] == 0) 
                            {
                                    memset(visited,false,sizeof(visited));
                                    if (!hungary(i))
                                    {
                                            ans = false;
                                            break;
                                    }
                            }
                    }
                    if (ans) printf("^_^
    ");
                    else printf("T_T
    ");
            } 
            
            return 0;
        
    }
  • 相关阅读:
    [转]:Android 5.0的调度作业JobScheduler
    Android 移动端数据结构
    算法-二分查找
    设计模式-设计原则
    设计模式-单例模式(Singleton Pattren)(饿汉模式和懒汉模式)
    Android 7.0 Dialog 无法显示的问题
    设计模式-装饰模式(Decorator Pattern)
    产品中心2
    java中static和final修饰符
    java访问修饰符
  • 原文地址:https://www.cnblogs.com/evenbao/p/9492800.html
Copyright © 2011-2022 走看看