标签:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 305;
const int INF = 0x3f3f3f3f;
const int OF = 50;
int n, m, L, s, t, rec[N];
map<string, int> mp2;
string size[7] = {"", "XS", "S", "M", "L", "XL", "XXL"};
struct Edge{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[N];
void init() {
memset(rec, 0, sizeof(rec));
for (int i = 0; i < N; i++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, int cap, int flow) {
edges.push_back((Edge){from, to, cap, 0});
edges.push_back((Edge){to, from, 0, 0});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
void input() {
mp2[size[1]] = 1;
mp2[size[2]] = 2;
mp2[size[3]] = 3;
mp2[size[4]] = 4;
mp2[size[5]] = 5;
mp2[size[6]] = 6;
for (int i = 1; i <= m; i++) {
addEdge(0, i, 1, 0);
}
string a, b;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
for (int j = 0; j < L; j++) {
int pa = mp2[a] + OF + j * 6;
addEdge(i, pa, 01, 0);
if (!rec[pa]) {
addEdge(pa, t, 1, 0);
rec[pa] = 1;
}
int pb = mp2[b] + OF + j * 6;
addEdge(i, pb, 1, 0);
if (!rec[pb]) {
addEdge(pb, t, 1, 0);
rec[pb] = 1;
}
}
}
}
int vis[N], d[N];
int BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int cur[N];
int DFS(int u, int a) {
if (u == t || a == 0) return a;
int flow = 0, f;
for (int &i = cur[u]; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[u][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int MF() { //dinic算法求最大流
int ans = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
ans += DFS(s, INF);
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
s = 0, t = 200;
L = n / 6;
init();
input();
int ans;
ans = MF();
if (ans == m) printf("YES\n");
else printf("NO\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
uva 11045 My T-shirt suits me (二分图匹配 最大流)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47056131