2016年12月12日 星期一

(UVA) 11045 - My T-shirt suits me

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=1986

flowing~~~

#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cstring>
#include <map>
using namespace std;

const int MAX_N = 106;
const int INF = 1e9 + 7;

struct Edge {
    int to,cap,rev;
};

Edge MP(int to,int cap,int rev) {
    return (Edge){to,cap,rev};
}

vector<Edge> edg[MAX_N];
int iter[MAX_N];
int level[MAX_N];

void add_edge(int from,int to,int cap) {
    edg[from].push_back(MP(to,cap,edg[to].size()));
    edg[to].push_back(MP(from,0,edg[from].size()-1));
}

void bfs(int s) {
    //cout<<"s = "<<s<<endl;
    memset(level,-1,sizeof(level));
    level[s]=0;
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
        int t=que.front();
        //cout<<"t = "<<t<<endl;
        que.pop();
        int len=edg[t].size();
        for (int x=0;len>x;x++) {
            Edge e=edg[t][x];
            if(e.cap > 0 && level[e.to] < 0) {
                //cout<<"update = "<<e.to<<" , t = "<<t<<" val = "<<level[e.to]<<" , "<<level[t]<<endl;
                level[e.to] = level[t] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f) {
    //cout<<"v = "<<v<<" , t = "<<t<<" , f = "<<f<<endl;
    if (v==t) return f;
    int len=edg[v].size();
    for(int &i=iter[v];i<len;i++) {
        Edge &e=edg[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            int d=dfs(e.to,t,min(f,e.cap));
            if (d>0) {
                e.cap -= d;
                edg[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int Dinic(int s,int t) {
    int flow = 0;
    while (true) {
        //cout<<"inn\n";
        bfs(s);
        //cout<<"innnnnnnn\n";
        if (level[t] < 0) break;
        memset(iter,0,sizeof(iter));
        int f;
        while ((f=dfs(s,t,INF)) > 0) {
            flow += f;
        }
        //cout<<"flow = "<<flow<<endl;
    }
    return flow;
}

int main () {
    map<string,int> mp;
    mp["XS"]=41;
    mp["S"]=42;
    mp["M"]=43;
    mp["L"]=44;
    mp["XL"]=45;
    mp["XXL"]=46;
    int T;
    scanf("%d",&T);
    while (T--) {
        int n,m;
        scanf("%d %d",&n,&m);
        for (int x=0;MAX_N>x;x++) {
            edg[x].clear();
        }
        for (int i=41;46>=i;i++) {
            add_edge(0,i,n/6);
        }
        for (int x=1;m>=x;x++) {
            string i,j;
            cin >> i >> j;
            add_edge(mp[i],x,1);
            add_edge(mp[j],x,1);
            add_edge(x,m+1,1);
        }
        //cout<<"innn\n";
        int ret=Dinic(0,m+1);
        if (ret == m) puts("YES");
        else puts("NO");
    }
}

沒有留言:

張貼留言