zoukankan      html  css  js  c++  java
  • UVA 753 A Plug for UNIX(二分图匹配)

     A Plug for UNIX

    You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible.


    Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can.


    Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug.


    In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.

    Input 

    The input will consist of several case. The first line of the input contains the number of cases, and it's followed bya blank line. The first line of each case contains a single positive integer n ( $1 leŸ n leŸ 100$) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m ( $1 leŸ m leŸ 100$) indicating the number of devices you would like to plug in. Each of the next mlines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k ( $1 leŸ k leŸ 100$) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

    There's a blank line between test cases.

    Output 

    For each case, print a line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in. Print a blank line between cases.

    Sample Input 

    1
    
    4
    A
    B
    C
    D
    5
    laptop B
    phone C
    pager B
    clock B
    comb X
    3
    B X
    X A
    X D
    

    Sample Output 

    1

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<string>
    #include<map>
    #include<algorithm>
    #define pb push_back
    using namespace std;
    int m,k,n,cnt,link[505],vis[505],mask[505];
    bool mat[505][505];
    char s[26],temp[26];
    map<string,int>f;
    vector<int>G[505];
    bool dfs(int u)
    {
        for(int len=G[u].size(),i=0;i<len;i++)
        {
            int v=G[u][i];
            if(!vis[v])
            {
                vis[v]=1;
                if(link[v]==-1||dfs(link[v]))
                {
                    link[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int match()
    {
        int ans=0;
        memset(link,-1,sizeof(link));
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))ans++;
        }
        return ans;
    }
    void floyd()
    {
        for(int i=1;i<=cnt;i++)
            mat[i][i]=1;
        for(int t=1;t<=cnt;t++)
            for(int i=1;i<=cnt;i++)
                for(int j=1;j<=cnt;j++)
                    mat[i][j]|=mat[i][t]&mat[t][j];
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        bool flag=0;
        while(T--)
        {
            scanf("%d",&n);
            cnt=1;
            f.clear();
            memset(mat,0,sizeof(mat));
            for(int i=1;i<=n;i++)G[i].clear();
            for(int i=1;i<=n;i++)
            {
                scanf("%s",s);
                if(!f.count(s))f[s]=cnt++;
                mask[i]=f[s];
            }
            scanf("%d",&m);
            for(int i=n+1;i<=n+m;i++)
            {
                scanf("%s%s",temp,s);
                if(!f.count(s))f[s]=cnt++;
                mask[i]=f[s];
            }
            scanf("%d",&k);
            string u,v;
            for(int i=1;i<=k;i++)
            {
                cin>>u>>v;
                if(!f.count(u))f[u]=cnt++;
                if(!f.count(v))f[v]=cnt++;
                mat[f[u]][f[v]]=1;
            }
            floyd();
            for(int i=1;i<=n;i++)
                for(int j=n+1;j<=n+m;j++)
                    if(mat[mask[i]][mask[j]]||mat[mask[j]][mask[i]])
                        G[i].pb(j);
            if(flag)putchar('
    ');
            flag=1;
            printf("%d
    ",m-match());
        }
        return 0;
    }
  • 相关阅读:
    iis管理器的程序应用池中没有Asp.NET v4.0
    Rowlock、UPDLOCK
    转SQLSERVER 会不会自动加锁
    安装IE11必备更新
    阻止表单提交刷新页面的问题
    C#分屏控件用法实例
    Flex内存泄露解决方法和内存释放优化原则
    DataSet.Clear() Method()
    短文件名漏洞修复
    vs2017创建dotnetcore web项目,并部署到centos7上
  • 原文地址:https://www.cnblogs.com/homura/p/6125643.html
Copyright © 2011-2022 走看看