链接

一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的。 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, 那么他就不会在去和别人换这一种。比如说第一个人有6个第一种, 2个第三种, 第二个人有2个第一种, 1个第二种, 那么第二个人就不会去和第一个人交换第二种, 而是去交换第三种。

问第一个人最后可以得到多少种不同的收藏品。

首先, 对于第一个人所拥有的收藏品, 建边(s, i+n, num[i]), 对于第2-n个人, 如果他所拥有的某种收藏品j的个数>1, 那么建边(i, j+n, num[j]-1), 如果没有某种收藏品, 那么建边(j+n, i, 1)。 对于所有收藏品, 建边(i+n, t, 1)。 最后跑一遍最大流即为答案。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int cnt[], num, q[maxn*], head[maxn*], dis[], s, t;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
int bfs() {
mem(dis);
int st = , ed = ;
dis[s] = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
int cost = ;
if(u == t)
return limit;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(e[i].c, limit-cost));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
void init() {
mem1(head);
num = ;
}
int main()
{
int T, m, n, x, y;
cin>>T;
for(int casee = ; casee <= T; casee++) {
cin>>n>>m;
init();
s = , t = n+m+;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
mem(cnt);
while(x--) {
scanf("%d", &y);
cnt[y]++;
}
if(i == ) {
for(int j = ; j<=m; j++) {
if(cnt[j])
add(s, j+n, cnt[j]);
}
} else {
for(int j = ; j<=m; j++) {
if(cnt[j]>) {
add(i, j+n, cnt[j]-);
}
if(cnt[j] == ) {
add(j+n, i, );
}
}
}
}
for(int j = ; j<=m; j++) {
add(j+n, t, );
}
printf("Case #%d: ", casee);
int ans = dinic();
cout<<ans<<endl;
}
}

uva 10779 Collectors Problem 网络流的更多相关文章

  1. UVA 10779 Collectors Problem(最大流)

    这个题是很难往网络流上面构思的... 从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数).这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数.那么交换后的 ...

  2. AC日记——Collectors Problem uva 10779

    UVA - 10779 思路: 最大流: s向所有的贴纸的种类连边,流量为Bob拥有的数量: 然后,Bob的朋友如果没有这种贴纸,则这种贴纸向bob的朋友连边,容量1: 如果bob的朋友的贴纸很多大于 ...

  3. UVA 10779 (最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33631 题目大意:Bob有一些贴纸,他可以和别人交换,他可以把自己 ...

  4. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  5. uva 11991 - Easy Problem from Rujia Liu?(STL)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  6. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  7. UVA-10779 Collectors Problem (网络流建模)

    题目大意:有n个人,已知每人有ki个糖纸,并且知道每张糖纸的颜色.其中,Bob希望能和同伴交换使得手上的糖纸数尽量多.他的同伴只会用手上的重复的交换手上没有的,并且他的同伴们之间不会产生交换.求出Bo ...

  8. UVa 11082 Matrix Decompressing - 网络流

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  9. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

随机推荐

  1. How to Use the UTL_MAIL Package

    APPLIES TO: PL/SQL - Version 10.1.0.2 and laterInformation in this document applies to any platform. ...

  2. java打包/命令行方式运行jar(命令行进行程序测试)

    public class Testtmp { public static void main(String[] args) { // TODO Auto-generated method stub f ...

  3. EC读书笔记系列之3:条款5、条款6、条款7

    条款5:了解C++默默编写并调用哪些函数 记住: ★编译器可以(仅仅是可以,并非必须,仅当程序中有这样的用法时才会这么做!!!)暗自为class创建default构造函数,copy构造函数,copy ...

  4. Java学习之finally关键字总结

    Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使 ...

  5. MySQL批量修改数据库的字符集

    #走过,试过的路 UPDATE information_schema.`SCHEMATA` SET DEFAULT_COLLATION_NAME='utf8_general_ci' WHERE DEF ...

  6. 使用ConcurrentDictionary实现轻量缓存

    项目中需要用到一个轻量缓存,存储重复使用的数据.在设计中需要考虑:1.做成通用组件,为未来其他模块方法操作结果做准备.2.缓存模块需要接口化,为未来替换使用外部缓存做准备.3.使用默认缓存过期时间,单 ...

  7. U盘装centos7系统过程

    1. 使用最新版UltraISO将ISO镜像刻录到U盘 一定要是最新版,试用版都可以,按下图操作: 2. U盘启动电脑进入安装界面 正常情况下你应该会看到下面的这个界面: 选择第一项,然后按TAB键, ...

  8. 走进C标准库(8)——"string.h"中函数的实现相关字符串操作函数

    我的strcat: char *strcat(char *dest,char *src) { char * reval = dest; while(*dest) dest++; while(*src) ...

  9. CSS自学笔记(7):CSS定位

    很多时候,我们需要对一些元素进行自定义排序.布局等,这是就需要用到CSS的定位属性了,用这些属性对一些元素进行自定义排序.布局等操作,可以改变浏览器默认的死板的排序. CSS定位的功能很容易理解,它允 ...

  10. XP下Virtualbox虚拟Ubuntu共享文件夹设置

    环境:主机操作系统是Windows XP,虚拟机是Ubuntu 8.10,虚拟机是VirtualBox 2.1.0. 1. 安装增强功能包(Guest Additions) 安装好Ubuntu 8.1 ...