题目链接:

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. Cross-site scripting

    Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web appli ...

  2. vs指定QT的工作目录(其它项目也是如此)

    当一个工程依赖第三方动态库时,这时vs编译出来后,运行会提示缺少动态库.解决方法: 项目->属性->调试: 工作目录:指定程序运行时的目录 环境:指定程序运行时的环境变量 我们可以在环境变 ...

  3. #include <cstdarg>

    实现可变参数 #include <iostream> #include <cstdarg> using namespace std; template <typename ...

  4. 从Excel转Access的一个方法说开去(DataRow的state状态)

    因为客户对access不太熟悉,更喜欢玩EXCEL.但是系统要求导入ACCESS.所以我们得做个把EXCEL转换成Access的小工具.(别问我为啥不让系统直接导入excel....我不知道!),然后 ...

  5. 单例模式 GetInstance()

    如何设计一个含GetInstance()函数的类 直接上代码: 头文件(MyClass.h): class CMyClass { public: CMyClass(void); ~CMyClass(v ...

  6. React.js 是什么?

    在相当长的一段时间内,我很努力地去尝试理解 React 是什么以及它在应用架构上的健壮程度.这篇文章解答了我希望别人为我解答的疑惑. React 是什么? 和 Angular,Ember,Backbo ...

  7. Web定时执行某个方法-网页获取

    实现该功能用到的是System.Timers.Timer 将定时的方法添加到Global.ascx.cs中 public class Global : System.Web.HttpApplicati ...

  8. samba服务器的安装及配置

    安装前首先查看服务器是否已经安装samba服务器 [root@bogon home]# rpm -qa|grep samba system-config-samba-docs-1.0.9-1.el6. ...

  9. framework层和native层实现联网控制(iptable方式)

    最近工作中,需要开发一个功能----联网控制,这个功能其实用过root的安卓机应该都知道,禁止某个应用连接移动网络或者wifi. root后,通过su去执行iptable的命令就可以根据uid去控制应 ...

  10. 弹出窗口a标签写下载,再弹出窗口

    如果这个窗口是弹出出口,直接<a href="">点击下载<a>是不行的,得用js这样写,弹出并关闭,不然会回到首页,如果没有定义首页会报错,<a h ...