System Engineer
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 507   Accepted: 217

Description

Bob is a skilled system engineer. He is always facing challenging problems, and now he must solve a new one. He has to handle a set of servers with differing capabilities available to process job requests from persistent sources - jobs that need to be processed over a long or indefinite period of time. A sequence of persistent job requests arrives revealing a subset of servers capable of servicing their request. A job is processed on a single server and a server processes only one job. Bob has to schedule the maximum number of jobs on the servers. For example, if there are 2 jobs j1, j2 and 2 servers s1, s2, job j1 requiring the server s1, and job j2 requiring also the server s1. In this case Bob can schedule only one job. Can you help him?
In the general case there are n jobs numbered from 0 to n-1, n servers numbered from n to 2*n-1, and a sequence of job requests. The problem asks to find the maximum number of jobs that can be processed.

Input

The program input is at most 1 MB. Each data set in the file stands for a particular set of jobs. A data set starts with the number n (n <= 10000) of jobs, followed by the list of required servers for each job, in the format:
jobnumber: (nr_servers) s1 ... snr_servers The program prints the maximum number of jobs that can be processed.

White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

2
0: (1) 2
1: (1) 2
1
0: (1) 1

Sample Output

1
1

Hint

There are two data sets. In the first case, the number of jobs n is 2, numbered 0 and 1. The sequence of requests for job 0 is: 0: (1) 2, meaning that job 0 requires 1 sever, the server numbered 2. The sequence of requests for job 1 is: 1: (1) 2, meaning that job 1 requires 1 sever, the server numbered 2. The result for the data set is the length of the maximum number of scheduled jobs, 1.

Source

Southeastern European Regional Programming Contest 2009

题意:分配工作。一个工作和一个人一一对应。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; struct edge
{
int to,next;
}e[100000];
int index; bool visit[10010]; //记录v2中的某个点是否被搜索过
int match[10010]; //记录与V2中的点匹配的点的编号
int head[10010];
int m,cnt; //向图中加边,注意加入的是有向边
//u为v的后继节点即v------>u
void addedge(int u,int v)
{
e[index].to=v;
e[index].next=head[u];
head[u]=index;
index++;
} //匈牙利算法(邻接表存图)
bool dfs(int u)
{
int i,v;
for(i=head[u];i;i=e[i].next)
{
v=e[i].to;
if(!visit[v]) //如果节点u与v相邻且未被查找过
{
visit[v]=true; //标记v已查找过
if(match[v]==-1||dfs(match[v])) //如果v未在前一个匹配M中,或者v在匹配M中,但是
{ //从与v相邻的节点出发可以有增广路径
match[v]=u; //记录查找成功,更新匹配即"取反"
return true; //返回查找成功。
}
}
}
return false;
} void init()
{
int aa,k,bb,i;
memset(head,0,sizeof(head)); //切记要初始化
memset(e,0,sizeof(e));
index=1;
for(i=1;i<=m;i++)
{
scanf("%d: (%d)",&aa,&k);
//printf("ok: %d %d\n",aa,k);
while(k--)
{
scanf("%d",&bb);
bb=bb-cnt+1;
addedge(bb,aa+1);
}
}
} int main()
{
int i;
while(scanf("%d",&cnt)!=EOF)
{
m=cnt;
init();
int ans=0;
memset(match,-1,sizeof(match));
for(i=1;i<=cnt;i++)
{
memset(visit,0,sizeof(visit)); //清空上次搜索时的标记
if(dfs(i)) //从i节点尝试扩展
ans++;
}
printf("%d\n",ans);
}
return 0;
}

poj 3894 System Engineer (二分图最大匹配--匈牙利算法)的更多相关文章

  1. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  2. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  3. 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement

    题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...

  4. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  5. Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

    解题报告 二分图第一题. 题目描写叙述: 为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; ...

  6. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  7. HDU1068 (二分图最大匹配匈牙利算法)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  9. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

随机推荐

  1. 文件atime未变问题的研究

    1. atime, ctime 以及mtime 这三个名词属于文件/文件夹的属性,存在于inode数据结构之中. 通过系统调用stat可以获取stat结构,其中包括:atime(accesstime) ...

  2. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  3. 在多线程环境中使用Jedis

    Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...

  4. C++模板 静态成员 定义(实例化)

    问一个问题: 考虑一个模板: template <typename T> class Test{ public: static std::string info; }; 对于下面若干种定义 ...

  5. UISegmentedControl判断点击第几项

    UISegmentedControl 关于UISegmentedControl判断当前点击的是第几项,找了很久,终于再老外的博客上找到了,在委托中 UISegmentedControl *Seg=se ...

  6. (转)Ubuntu中让终端对于历史输出的内容保持足够长

    原地址:http://www.crifan.com/ubuntu_terminal_make_retain_long_enough_history_output_content/ Ubuntu下用终端 ...

  7. zoj Grouping(强连通+缩点+关键路径)

    题意: 给你N个人,M条年龄大小的关系,现在打算把这些人分成不同的集合,使得每个集合的任意两个人之间的年龄是不可比的.问你最小的集合数是多少? 分析: 首先,假设有一个环,那么这个环中的任意两个点之间 ...

  8. 32位的CPU最多只能支持最大到4GBytes的内存

    和总线宽度相似的,CPU每次能够处理的数据量称为字组大小(word size), 字组大小依据CPU癿设计而有32位与64位.我们现在所称的计算机是32或64位主要是依据这个 CPU解析的字组大小而来 ...

  9. ElasticSearch入门知识扫盲

    ElasticSearch 入门介绍 tags: 第三方 lucene [toc] 1. what Elastic Search(ES)是什么 全文检索和lucene 全文检索 优点:高效,准确,分词 ...

  10. PHP方法的作用域

    PHP支持6种方法作用域:public.private.protected.abstract.final和static,本文只讨论前5种作用域. public:公共作用域.(就像空气和阳光,天下公用) ...