The Perfect Stall+spoj+强联通分量+缩点
来源:互联网 发布:java程序加载类的步骤 编辑:程序博客网 时间:2024/05/22 13:52
The Perfect Stall
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18203 Accepted: 8279
Description
Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Input
The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.
Output
For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
Sample Input
5 52 2 53 2 3 42 1 53 1 2 51 2
Sample Output
4
解决方案:求强联通分量,缩点,反向建图。
code:#include<iostream>#include<cstdio>#include<cstring>#include<stack>#include<vector>#define MMAX 5005using namespace std;int low[MMAX],dfn[MMAX],instack[MMAX],fa[MMAX],in[MMAX],save[MMAX];int index;vector<int>Map[MMAX];stack<int>S;int V,E;void init(){ index=0; memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack)); memset(in,0,sizeof(in)); for(int i=0; i<=E; i++) { fa[i]=i; Map[i].clear(); } while(!S.empty())S.pop();}void tarjan(int u){ low[u]=dfn[u]=index++; S.push(u); instack[u]=1; int len=Map[u].size(); for(int i=0; i<len; i++) { int v=Map[u][i]; if(!instack[v]) { tarjan(v); low[u]=min(low[v],low[u]); } else if(instack[v]==1) { low[u]=min(low[v],low[u]); } } if(dfn[u]==low[u]) { while(!S.empty()) { int temp=S.top(); S.pop(); fa[temp]=u; instack[temp]=2; if(temp==u) break; } }}int main(){ while(~scanf("%d",&E)&&E) { init(); scanf("%d",&V); for(int i=0; i<V; i++) { int a,b; scanf("%d%d",&a,&b); Map[a].push_back(b); } for(int i=1; i<=E; i++) { if(!instack[i]) { tarjan(i); } } for(int i=1; i<=E; i++) { int len=Map[i].size(); for(int j=0; j<len; j++) { int v=Map[i][j]; if(fa[v]!=fa[i]) { in[fa[i]]++; } } } int k=0; for(int i=1; i<=E; i++) { if(!in[fa[i]]) { save[k++]=i; } } printf("%d",save[0]); for(int i=1; i<k; i++) { printf(" %d",save[i]); } printf("\n"); } return 0;}
0 0
- The Perfect Stall+spoj+强联通分量+缩点
- 强联通分量+缩点
- 强联通分量 缩点 tarjan算法
- poj2186 强联通分量 缩点
- poj 2553 The Bottom of a Graph (强联通分量+缩点)
- POJ 1236 Network of Schools 强联通分量 + 缩点
- BZOJ 2438 杀人游戏 强联通分量tarjan缩点
- poj 2186 Popular Cows (强联通分量+缩点)
- poj 1236 Network of Schools (强联通分量+缩点)
- sdut 2604 Thrall’s Dream (强联通分量+缩点)
- ZOJ 3795 Grouping(强联通分量 + 缩点 + Dp)
- poj2186 Popular Cows (强联通分量+缩点)
- bzoj1501受欢迎的牛(强联通分量+缩点)
- 强联通分量 缩点 tarjan 入门题小集
- 割点,割边,强联通分量,点双联通分量,边双联通分量
- HDOJ 3861 - The King’s Problem tarjan求强联通分量&缩点&有向图最小路径覆盖(匈牙利)
- POJ 2553 The Bottom of a Graph 缩点之后求出度为0的强联通分量的元素
- POJ1274 The Perfect Stall
- crm组织服务中的crm消息
- RHEL 6.4中安装G++编译器及GCC与G++区别
- hdu-2674-N!Again
- Eclipse标准版安装J2EE模块
- js注册检测 用户名、密码、手机号、邮箱等
- The Perfect Stall+spoj+强联通分量+缩点
- 第一章 引论-C++常识
- 今天我做了别人不愿做的事,明天我就能做到别人所不能做的事
- 工作日志2014-08-01
- HDU1728 逃离迷宫 【方向BFS】
- javascript自定义异常
- The Documentatiion of Kinect Face Tracking
- uva 12119 - The Bells are Ringing(数论+枚举)
- Client:TSocketConnection 和Server: Scktsrvr关系----压缩数据传输方案