UPC9655 Plug It In!

问题 E: Plug It In!

时间限制: 12 Sec  内存限制: 128 MB
提交: 78  解决: 16
[提交] [状态] [命题人:admin]

题目描述

Adam just moved into his new apartment and simply placed everything into it at random. This means in particular that he did not put any effort into placing his electronics in a way that each one can have its own electric socket.
Since the cables of his devices have limited reach, not every device can be plugged into every socket without moving it first. As he wants to use as many electronic devices as possible right away without moving stuff around, he now tries to figure out which device to plug into which socket. Luckily the previous owner left behind a plugbar which turns one electric socket into 3. 
Can you help Adam figure out how many devices he can power in total?

输入

The input consists of:
• one line containing three integers m, n and k, where
– m (1 ≤ m ≤ 1 500) is the number of sockets;
– n (1 ≤ n ≤ 1 500) is the number of electronic devices;
– k (0 ≤ k ≤ 75 000) is the number of possible connections from devices to sockets.
• k lines each containing two integers xi and yi indicating that socket xi can be used to power device yi .
Sockets as well as electronic devices are numbered starting from 1.
The plugbar has no cable, i.e. if it is plugged into a socket it simply triples it.

输出

Output one line containing the total number of electrical devices Adam can power.

样例输入

3 6 8
1 1
1 2
1 3
2 3
2 4
3 4
3 5
3 6

样例输出

5

一道二分图匹配题目,比较裸,直接暴力枚举每一个插头变为三个的情况,然后都继承一下初始的匹配状态再次进行匹配就可以了。
#include <bits/stdc++.h>
#define rint register int
typedef long long ll;
using namespace std;
const int N=1.5e3+5;
int linker[N*2];
int link[N*2];
bool used[N*2];
vector<int>g[N];
vector<int>gg[N];
void add(int u,int v)
{
    g[u].push_back(v);
}
bool dfs(int u)
{
    int len=g[u].size();
    for(rint i=0; i<len; i++)
    {
        int v=g[u][i];
        if(!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k,u,v;
    int ans=0,res=0;
    cin>>m>>n>>k;
    for(rint i=1; i<=k; i++)
    {
        cin>>u>>v;
        add(u,v+m);
    }
    memset(linker,-1,sizeof linker);
    for(rint i=1; i<=m; i++)
    {
        memset(used,0,sizeof used);
        if(dfs(i))res++;
    }
    for(int i=1;i<=m*2+100;i++)link[i]=linker[i];
   // cout<<res<<endl;
    for(rint j=1; j<=m; j++)
    {
        for(int i=1;i<=2*m+100;i++)linker[i]=link[i];
        g[m+1]=g[j];
        g[m+2]=g[j];
        int sum=0;
        for(rint i=m+1;i<=m+2;i++)
        {
            memset(used,0,sizeof used);
            if(dfs(i))sum++;
        }
        ans=max(ans,sum+res);
    }cout<<ans<<endl;
    return 0;
}
 
 

猜你喜欢

转载自www.cnblogs.com/liuquanxu/p/11376273.html