题意

给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头。求无法匹配插座的插头设备个数。

题解

用\(map\)给每个字符串标号为\(a_i\)和\(b_i\)。

读入每种改变插头的方法,连边,权值为\(inf\)。

然后连边\(S \longrightarrow a_i\),权值为\(1\);\(b_i \longrightarrow T\),权值为\(1\)。

跑最大流即可。

代码

#include <bits/stdc++.h>

#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)
#define FOR(i,x,y) for (int i = x; i <= y; i++)
#define ROF(i,x,y) for (int i = x; i >= y; i--) using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 100;
const int maxm = 2e5 + 100; struct Edge
{
int to, next, cap, flow;
}edge[maxm]; int tot;
int head[maxn]; void init()
{
tot = 2;
memset(head, -1, sizeof(head));
} void build(int u, int v, int w, int rw = 0)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
edge[tot].next = head[u]; head[u] = tot++; edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
edge[tot].next = head[v]; head[v] = tot++;
} int Q[maxn];
int dep[maxn], cur[maxn], sta[maxn]; bool bfs(int s, int t, int n)
{
int front = 0, tail = 0;
memset(dep, -1, sizeof(dep[0]) * (n+1));
dep[s] = 0;
Q[tail++] = s;
while(front < tail)
{
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dep[v] == -1)
{
dep[v] = dep[u] + 1;
if (v == t) return true;
Q[tail++] = v;
}
}
}
return false;
} LL dinic(int s, int t, int n)
{
LL maxflow = 0;
while(bfs(s, t, n))
{
for (int i = 0; i < n; i++) cur[i] = head[i];
int u = s, tail = 0;
while(cur[s] != -1)
{
if (u == t)
{
int tp = inf;
for (int i = tail-1; i >= 0; i--)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); //if (tp >= inf) return -1;
maxflow += tp; for (int i = tail-1; i >= 0; i--)
{
edge[sta[i]].flow += tp;
edge[sta[i]^1].flow -= tp;
if (edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i;
}
u = edge[sta[tail]^1].to;
}
else if (cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow
&& dep[u]+1 == dep[edge[cur[u]].to])
{
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else
{
while(u != s && cur[u] == -1) u = edge[sta[--tail]^1].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
} int t;
int a[205], b[205];
string s, r;
map<string, int> M;
int n, m, S, T, cnt, q; int getid(string &s)
{
if (M.count(s)) return M[s];
return M[s] = ++cnt;
} int main()
{
// FOPI;
// FOPO; ios::sync_with_stdio(false);
cin.tie(NULL); cin >> t;
FOR(ca, 1, t)
{
init();
M.clear(); cnt = 0; cin >> n;
FOR(i, 1, n) { cin >> s; a[i] = getid(s); } cin >> m;
FOR(i, 1, m) { cin >> r >> s; b[i] = getid(s); } cin >> q;
FOR(i, 1, q)
{
cin >> s >> r;
int x = getid(s), y = getid(r);
build(y, x, inf);
} S = 0, T = cnt+1;
FOR(i, 1, n) build(S, a[i], 1);
FOR(j, 1, m) build(b[j], T, 1); LL ans = dinic(S, T, T);
printf("%lld\n", m-ans); if (ca != t) puts("");
}
}

UVA - 753 A Plug for UNIX(网络流)的更多相关文章

  1. 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 U ...

  2. POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)

    POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...

  3. 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 Unit ...

  4. UVA 753 A Plug for UNIX 电器插座(最大基数匹配,网络流)

    题意: 给n个插座,m个设备(肯定要插电了),k种转换头可无限次使用(注意是单向的),问有多少设备最终是不能够插上插座的? 分析: 看起来就是设备匹配插座,所以答案不超过m.这个题适合用网络流来解. ...

  5. UVa 753 - A Plug for UNIX(最大流)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVa 753 A Plug for UNIX (最大流)

    题意:给定 n 种插座,m种设备,和k个转换器,问你最少有几台设备不能匹配. 析:一个很裸的网络流,直接上模板就行,建立一个源点s和汇点t,源点和每个设备连一条边,每个插座和汇点连一条边,然后再连转换 ...

  7. UVA 753 A Plug for UNIX (最大流)

    关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...

  8. UVA 753 A Plug for UNIX

    最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...

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

    链接 : http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? id=26746 题目意思有点儿难描写叙述 用一个别人描写叙述好的. 我的 ...

随机推荐

  1. matlab 打不开excel文件

    方法论 excel的后缀为.xls, matlab是无法识别的, 需要将其另存为.xlsx文件格式 打开excel, 点击save as, 选中保存的文件格式是.xlsx即可

  2. c# string.format 的简写 $

    var name = "huchao"; var info = $"你是谁,我叫:{name}"; Console.Write(info); Console.R ...

  3. Rspec基本语法

    引用链接:http://reverocean.iteye.com/blog/1489957 1. describe和context describe和context方法用来组织相关的行为example ...

  4. flask--数据库连接--URL

    使用URL制定数据库 数据库引擎 URL MySQL mysql://username:password@hostname/database Postgres postgresql://usernam ...

  5. 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)

    本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...

  6. SQLSERVER 创建ODBC 报错的解决办法 SQLState:'01000'的解决方案

    错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOp ...

  7. 【BZOJ3925】[ZJOI2015] 地震后的幻想乡(状压期望DP)

    点此看题面 大致题意: 有\(n\)个点和\(m\)条边,每条边的权值是一个\(0\sim1\)的随机实数,要你用\(n-1\)条边将图联通,问这\(n-1\)条边中边权最大值的期望最小值. 提示 这 ...

  8. JSON对象转成formData对象,formData对象转成JSON对象

    在向后端请求时,如果上传的数据里存在file文件对象,需要用到表单提交,这时候我们需要将JSON对象,转成formData对象,具体见代码 const formData = new FormData( ...

  9. AngularJs学习笔记-组件间通讯

    组件间通讯 (1)输入属性@Input Tips:子组件属性的改变不会影响到父组件 如下,子组件中stockCode属性发生变化不会引起父组件stock属性的变化 (2)输入属性@Output 子组件 ...

  10. ERR_FAILED 浏览器访问

    我是针对上一篇的问题 继续的探究  ,我百度了 看了这 https://zhidao.baidu.com/question/1175643597811783659.html 之后我就用 电脑管家进行系 ...