sgu242:http://acm.sgu.ru/problem.php?contest=0&problem=242

题意:把n个人分到m组,每一组至少2个人,每个人只能属于一个组。一开始把题目读错了,以为这n个必须都要有一个组,实际上,有些人可以不应分组,直接留在家里,这样就简单许多。

题解:把问题简化一下就是每个学校分2个人,如果这个条件都满足不了的话,就不可能满足。所以建图的时候s-->学校,容量是2,学校到人,容量是1,然后人到t,容量是1,这里是不需要的拆点的,因为上诉的建图凡是已经把正每个人只会属于一个组,只会被应一次。所以直接跑网络流就好,然后输出即可。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#define INF 100000000
using namespace std;
const int N=;
const int M=;
struct Node{
int v;
int f;
int next;
}edge[M];
int n,m,u,v,cnt,sx,ex;
int head[N],pre[N];
int val[N][];//根据题目要求申请
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w){
edge[cnt].v=v;
edge[cnt].f=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].f=;
edge[cnt].v=u;
edge[cnt].next=head[v];
head[v]=cnt++;
}
bool BFS(){
memset(pre,,sizeof(pre));
pre[sx]=;
queue<int>Q;
Q.push(sx);
while(!Q.empty()){
int d=Q.front();
Q.pop();
for(int i=head[d];i!=-;i=edge[i].next ){
if(edge[i].f&&!pre[edge[i].v]){
pre[edge[i].v]=pre[d]+;
Q.push(edge[i].v);
}
}
}
return pre[ex]>;
}
int dinic(int flow,int ps){
int f=flow;
if(ps==ex)return f;
for(int i=head[ps];i!=-;i=edge[i].next){
if(edge[i].f&&pre[edge[i].v]==pre[ps]+){
int a=edge[i].f;
int t=dinic(min(a,flow),edge[i].v);
edge[i].f-=t;
edge[i^].f+=t;
flow-=t;
if(flow<=)break;
} }
if(f-flow<=)pre[ps]=-;
return f-flow;
}
int solve(){
int sum=;
while(BFS())
sum+=dinic(INF,sx);
return sum;
}
int main() {
int T,k,temp,sum,tt=;
while(~scanf("%d%d",&n,&m)) {
sum=;
init();
for(int i=; i<=n; i++) {
scanf("%d",&T);
while(T--){
scanf("%d",&temp);
add(temp,i+m,);
}
add(i+m,n+m+,);
}
for(int j=;j<=m;j++)
add(,j,);
sx=,ex=n+m+;
if(solve()<*m)puts("NO\n");
else{
printf("YES\n");
for(int i=;i<=m;i++){
printf("");
for(int j=head[i];j!=-;j=edge[j].next){
if(edge[j].f==)//这里不是f>0,而是f==0才是满流的标志,注意和邻接矩阵的区别
printf(" %d",edge[j].v-m);
}
puts("");
}
}
}
return ;
}

Student's Morning的更多相关文章

  1. java.io.NotSerializableException: test.io.file.Student

    java.io.NotSerializableException: test.io.file.Student    at java.io.ObjectOutputStream.writeObject0 ...

  2. 使用java反射机制编写Student类并保存

    定义Student类 package org; public class Student { private String _name = null; ; ; public Student() { } ...

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

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

  4. The constructor User.Student(String, String, String) is not visible

    项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...

  5. Java-集合-第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList(); l

    第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; ...

  6. student表中创建触发器,实现student表和student _course表的级联删除

    create trigger Delete_sc on student for delete as delete student_course where student_course.s_no in ...

  7. 第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList();

    list.add(new Student("Tom", 18, 100, "class05")); list.add(new Student("Jer ...

  8. 编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试Stud

    package zuoye; public class student { int age; String name; int stuNO; void outPut() { System.out.pr ...

  9. Student管理系统

    使用三层架构实现Student管理系统,分为Studrnt.Model层,Student.DAL层,Student.BLL层和Student.UI层 步骤分析: 1.在Student.Model添加S ...

  10. Linq查询非泛型集合要指定Student类型(比如List)

    #region Linq to 集合查询非泛型集合要指定Student类型            //ArrayList list = new ArrayList();            //li ...

随机推荐

  1. 剖析@weakify 和 @strongify

    前言 使用RAC的时候我们常会看到这两个宏@weakify(self).@strongify(self),用来防止使用block时出现引用闭环. 今天看YYKit的时候,看到里面也写了类似的宏,还是来 ...

  2. ubuntu15.10英文系统中文输入法配置 fcitx

    15.10 默认安装的输入法engine就是fcitx,如果你安装的时候locale选中文,应该不用任何折腾就可以用了,但我习惯了用英文系统,所以..... 系统安装好之后,做如下修改: 安装语言包 ...

  3. apache的一些基本配置

    Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) 基本配置: ServerRoot "/mnt/softw ...

  4. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

  5. jQuery获取radio选中项的值【转藏】

    <title></title> <script src="js/jquery-1.7.2.min.js"></script> < ...

  6. 【转】Java中equals和==的区别

    [转]Java中equals和==的区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boole ...

  7. 源码心德`Context`类

    Context,中文直译为“上下文”,SDK中对其说明如下: Interface to global information about an application environment. Thi ...

  8. Apache虚拟目录

    Apache虚拟目录  1.打开Apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#! 2.在httpd.conf 末尾 ...

  9. ios专题 - 安全

    iOS通过以下几种机制来保全整个系统的安全性: 一:系统结构 所有iOS设备中,系统与硬件都高度集成,从系统启动.系统更新.应用的安装.应用的运行时等多个方面来保全系统的安全,具体包括: 1:所有iO ...

  10. MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性

    在学习MVVM的过程中,总是会遇到挫折,一碰到就是花费好长时间去解决..唉,不求量,只求质. 第一种(已经实践成功): (1)http://bindableapplicationb.codeplex. ...