ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

时间:2022-05-19 22:02:14

链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26746

题目意思有点儿难描述 用一个别人描述好的。

ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)

ZOJ1157, POJ1087,UVA 753 A Plug for UNIX (最大流)


我的建图方法:一个源点一个汇点,和所有种类的插座。输入的n个插座直接与源点相连,容量为1,m个物品输入里 记录每个插座对应的物品个数,物品数然后大于0的插座直接连到汇点,意味着最终的物品只能由这些插座流出。中间的插座转换容量都是INF  a b表示  无论多少b都可以选择转化到a。


/*--------------------- #headfile--------------------*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
/*----------------------#define----------------------*/
#define DRII(X,Y) int (X),(Y);scanf("%d%d",&(X),&(Y))
#define EXP 2.7182818284590452353602874713527
#define CASET int _;cin>>_;while(_--)
#define RII(X, Y) scanf("%d%d",&(X),&(Y))
#define DRI(X) int (X);scanf("%d", &X)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(X) (X).begin(),(X).end()
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define RI(X) scanf("%d",&(X))
#define SZ(X) ((int)X.size())
#define PDI pair<double,int>
#define rson o<<1|1,m+1,r
#define PII pair<int,int>
#define MAX 0x3f3f3f3f
#define lson o<<1,l,m
#define MP make_pair
#define PB push_back
#define SE second
#define FI first
typedef long long ll;
template<class T>T MUL(T x,T y,T P){T F1=0;while(y){if(y&1){F1+=x;if(F1<0||F1>=P)F1-=P;}x<<=1;if(x<0||x>=P)x-=P;y>>=1;}return F1;}
template<class T>T POW(T x,T y,T P){T F1=1;x%=P;while(y){if(y&1)F1=MUL(F1,x,P);x=MUL(x,x,P);y>>=1;}return F1;}
template<class T>T gcd(T x,T y){if(y==0)return x;T z;while(z=x%y)x=y,y=z;return y;}
#define DRIII(X,Y,Z) int (X),(Y),(Z);scanf("%d%d%d",&(X),&(Y),&(Z))
#define RIII(X,Y,Z) scanf("%d%d%d",&(X),&(Y),&(Z))
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1000000007ll;
const int M = 1005;
const int N = 605;
using namespace std;

/*----------------------Main-------------------------*/
struct Edge {
    int to, c, rev;
    Edge() {}
    Edge(int _to, int _c, int _rev) {
        to = _to, c = _c, rev = _rev;
    }
};
vector<Edge> G[N];
int lv[N], iter[N];
int n, m;
void BFS(int s) {
    mem(lv, -1);
    queue<int> q;
    lv[s] = 0;
    q.push(s);
    while(!q.empty()) {
        int v = q.front(); q.pop();
        for(int i = 0; i < SZ(G[v]); i++) {
            Edge &e = G[v][i];
            if(e.c > 0 && lv[e.to] < 0) {
                lv[e.to] = lv[v] + 1;
                q.push(e.to);
            }
        }
    }
}
int dfs(int v, int t, int f) {
    if(v == t) return f;
    for(int &i = iter[v]; i < SZ(G[v]); i++) {
        Edge &e = G[v][i];
        if(e.c > 0 && lv[v] < lv[e.to]) {
            int d = dfs(e.to, t, min(f, e.c));
            if(d > 0) {
                e.c -= d;
                G[e.to][e.rev].c += d;
                return d;
            }
        }
    }
    return 0;
}
int MF(int s, int t) {
    int res = 0;
    for( ; ; ) {
        BFS(s);
        if(lv[t] < 0) return res;
        mem(iter, 0);
        int f;
        while((f = dfs(s, t, 1e9)) > 0) {
            res += f;
        }
    }
}
void add(int from, int to, int c) {
    G[from].PB( Edge(to, c, SZ(G[to])) );
    G[to].PB( Edge(from, 0, SZ(G[from]) - 1) );
}
int num[N];
int FF = 0;
void solve() {
    if(FF) puts(""); FF = 1;
    RI(n);
    for(int i = 0; i < 300; i++) G[i].clear();
    mem(num, 0);
    int s = 0, k = 0;
    map<string, int> vis;
    for(int i = 1; i <= n; i++) {
        string s1; cin >> s1;
        vis[s1] = ++k;
        add(s, i, 1);
    }
    RI(m);
    for(int i = 1; i <= m; i++) {
        string s1, s2;
        cin >> s1 >> s2;
        if(vis[s2] == 0) vis[s2] = ++k;
        num[ vis[s2] ]++;
    }
    int t = k + 1;
    for(int i = 1; i <= k; i++) {
        if(num[i]) add(i, t, num[i]);
    }
    DRI(x);
    k++;
    for(int i = 1; i <= x; i++) {
        string s1, s2;
        cin >> s1 >> s2;
        if(vis[s2] == 0) vis[s2] = ++k;
        if(vis[s1] == 0) vis[s1] = ++k;
        int u = vis[s2], v = vis[s1];
        add(u, v, 1e9);
    }
    int ans = MF(s, t);
    printf("%d\n", m - ans);
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    CASET
    solve();
    return 0;
}