题目链接:

contest=0&problem=242">点击打开链接

题意:

给定n个人,m个终点

以下n行表示每一个人能够去m个点。

每一个人仅仅能去一个点。

输出随意一个方案使得每一个点至少有2个人到达。

若存在输出m行,第一个数字表示i这个点来了几个人,后面是人的点标。

思路:

建一个二部图n-m,然后m到汇点限流2。推断是否满流,若不满流就无解。

若满流则其它的人随便走。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
typedef int ll;
#define N 420
#define M 100000
#define inf 107374182
struct Edge{
ll from, to, cap, nex;
}edge[M*2];
ll head[N], edgenum;
void add(ll u, ll v, ll cap, ll rw = 0){
Edge E = {u, v, cap, head[u]};
edge[edgenum] = E;
head[u] = edgenum ++;
Edge E2 = {v, u, rw, head[v]};
edge[edgenum] = E2;
head[v] = edgenum ++;
}
ll sign[N];
bool BFS(ll from, ll to){
memset(sign, -1, sizeof sign);
sign[from] = 0;
queue<ll>q;
q.push(from);
while( !q.empty() ) {
ll u = q.front(); q.pop();
for(ll i = head[u]; ~i; i = edge[i].nex)
{
ll v = edge[i].to;
if(sign[v] == -1 && edge[i].cap){
sign[v] = sign[u] +1, q.push(v);
if(sign[to] != -1) return true;
}
}
}
return false;
}
ll Stack[N], top, cur[N];
ll Dinic(ll from, ll to){
ll ans = 0;
while( BFS(from, to) )
{
memcpy(cur, head, sizeof head);
ll u = from; top = 0;
while(1)
{
if(u==to)
{
ll flow = inf, loc;
for(ll i = 0; i < top; i++)
if(flow > edge[Stack[i]].cap)
{
flow = edge[Stack[i]].cap;
loc = i;
}
for(ll i = 0; i < top; i++)
{
edge[ Stack[i] ].cap -= flow;
edge[Stack[i]^1].cap += flow;
}
ans += flow;
top = loc;
u = edge[Stack[top]].from;
}
for(ll i = cur[u]; ~i; cur[u] = i = edge[i].nex)
if(edge[i].cap && (sign[u]+1 == sign[edge[i].to]))break;
if(cur[u] != -1){
Stack[top++] = cur[u];
u = edge[cur[u]].to;
}
else
{
if(top==0)break;
sign[u] = -1;
u = edge[Stack[--top]].from;
}
}
}
return ans;
}
void init(){memset(head, -1, sizeof head); edgenum = 0;}
int n, m, from, to, to2, ans[N], G[N];
vector<int>D[N];
bool solve(){
from = 0; to = n+m+1; to2 = to+1;
init();
for(int i = 1; i <= n; i++)
{
G[i] = -1;
add(from, i, 1);
int x, y; scanf("%d",&x);
while(x--){
scanf("%d",&y);
G[i] = y;
add(i, n+y, 1);
}
}
for(int i = 1; i <= m; i++)
add(n+i, to, 2);
add(to, to2, inf);
if(m*2 > n || Dinic(from, to2) < m*2)return false;
puts("YES");
for(int i = 1; i <= n; i++)
{
ans[i] = -1;
for(int j = head[i]; ~j; j = edge[j].nex)
{
if(edge[j].cap==0 && edge[j].to != from)
{
ans[i] = edge[j].to-n;
D[edge[j].to-n].push_back(i);
break;
}
}
if(ans[i]==-1 && G[i]!=-1)
D[G[i]].push_back(i);
}
for(int i = 1; i <= m; i++)
{
printf("%d", D[i].size());
for(int j = 0; j < D[i].size(); j++)
printf(" %d", D[i][j]);
puts("");
}
return true;
}
int main(){
while(~scanf("%d %d",&n,&m)){
if(solve()==false)puts("NO");
for(int i = 1; i <= m; i++)D[i].clear();
}
return 0;
}
/*
5 2
1 1
1 2
1 1
1 1
1 1 5 2
1 1
1 2
1 1
1 1
2 1 2 */

SGU 242 Student&#39;s Morning 网络流(水的更多相关文章

  1. SGU 242. Student's Morning( 网络流 )

    看英文题真是麻烦...理解题意花的时间比想的时间还长...裸的网络流, 我们只要限制每个人出发流量为1, 每个大学进入的流量至多为2即可, 相当于构造可行解. -------------------- ...

  2. SGU 185 Two shortest ★(最短路+网络流)

    [题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...

  3. Could not drop object &#39;student&#39; because it is referenced by a FOREIGN KEY constraint

    1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...

  4. hdu 4274 Spy&#39;s Work(水题)

    Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. POJ 2081 Recaman&#39;s Sequence(水的问题)

    [简要题意]:这个主题是很短的叙述性说明.挺easy. 不重复. [分析]:只需要加一个判断这个数是否可以是一个数组,这个数组的范围. // 3388K 0Ms #include<iostrea ...

  6. HDU 4791 Alice&#39;s Print Service 水二分

    点击打开链接 Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  8. 2014-11-9------- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  9. 2014.06.20 (转)IEEE与论坛灌水

    转自"饮水思源"      电子类学生大都知道IEEE, 这个IEEE就像一个大的BBS论坛,而这个协会下面有很多杂志,比如图像处理,信号处理,微波技术等.这些杂志就是论坛下的分版 ...

随机推荐

  1. jquery鼠标滑过展示图片时显示详情

    jquery: <script src="js/jquery.js" type="text/javascript"></script> ...

  2. python3--(变量)

    变量: Python 是动态类型语言, 也就是说不需要预先声明变量的类型.变量是对象的引用,变量只是将指针指向了对象所在的内存地址.变量的类型和值在赋值那一刻被初始化. 变量起名: 1.显式--> ...

  3. C语言复杂的函数指针声明

    复习C语言ING,发现复杂的函数指针声明看不懂,百度半天终于略知一二. 讲的比较详细的一篇blog: http://blog.csdn.net/megaboy/article/details/4827 ...

  4. ODI性能问题

        在这里分析下最近分析和解决ODI性能问题的点滴,用作参考.在介入该问题前,已经具备的基础知识包括了ODI基础,SOA理论和实施,特别是04年封闭四天的informatic ETL培训和实操.几 ...

  5. Javascript面向对象之创建对象

    面向对象的语言具有一个共同的标志,那就是具有“类”的概念,但是在javascript中没有类的概念,在js中将对象定义为“无序属性的集合,其属性可以包含基本值,对象或者函数”,即其将对象看作是一组名值 ...

  6. Oracle EBS-SQL (SYS-8):职责定义明细.sql

    SELECT DISTINCT fa.application_short_name 模块,                 b.responsibility_name 职责名称, fa.applica ...

  7. poj2000---和1969一样的模板

    #include <stdio.h> #include <stdlib.h> int main() { int d; while(scanf("%d",&a ...

  8. 织梦DEDECMS 首页列表页内容也时间日期调用标签

    DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strftime()函数格式化时间)0 dedecms首页时间标 ...

  9. Ubuntu小私房(3)--Uubutnu启动美化大变身

    Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...

  10. java中关于线程间协作所用关键字synchronized,wait,notify的用法

    wait/notify()关键字适用于一个线程通知另一个线程所需的条件状态已就绪,最常用于线程在循环中休眠直到获取特定条件的场景. 例如,一个线程一直等待直到队列中有一个组件能够处理:当组件添加到队列 ...