SGU242:最大流

最近总是刷阅读理解还是有点好处的...给出k个学校和n和学生的喜欢名单,每个学校至少有两个学生拜访,每个学生只能去一次,求是否能满足k个学校都能去拜访,能的话给出方案。(然而里面的company那里是说什么...)。试着建图,s到每个学生c=1,每个学生到喜欢的学校c=1,每个学校到t c=2,然后就好了,感觉不算难。

------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
const int inf=0x3f3f3f;
int read(){
 int x=0,f=1;char c=getchar();
 while(!isdigit(c)){
  if(c==‘-‘) f=-1;
  c=getchar();
 }
 while(isdigit(c)){
  x=x*10+c-‘0‘;
  c=getchar();
 }
 return x*f;
}

struct edge{
 int to,cap;
 edge *next,*rev;
};
edge e[100000],*cur[405],*p[405],*head[405],*pt=e;
int d[405],cnt[405],ans[205][3];
void add(int u,int v,int d){
 pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++;
}
void adde(int u,int v,int d){
 add(u,v,d);add(v,u,0);head[u]->rev=head[v];head[v]->rev=head[u];
}

int maxflow(int s,int t,int n){
 clr(d,0);clr(cnt,0);cnt[0]=n;
 int x=s,flow=0,a=inf;
 while(d[s]<n){
  edge *ee;
  for(ee=cur[x];ee;ee=ee->next){
   if(ee->cap>0&&d[ee->to]+1==d[x])
     break;
  }
  if(ee){
   p[ee->to]=cur[x]=ee;
   a=min(a,ee->cap);
   x=ee->to;
   if(x==t){
    while(x!=s){
     p[x]->cap-=a;
     p[x]->rev->cap+=a;
     x=p[x]->rev->to;
    }
    flow+=a;
    a=inf;
   }
  }
  else{
   if(!--cnt[d[x]]) break;
   d[x]=n;
   for(ee=head[x];ee;ee=ee->next){
    if(ee->cap>0&&d[ee->to]+1<d[x]){
     d[x]=d[ee->to]+1;
     cur[x]=ee;
    }
   }
   cnt[d[x]]++;
   if(x!=s) x=p[x]->rev->to;
  }
 }
 return flow;
}

int main(){
 int n=read(),m=read(),s=0,t=n+m+1,tot=t+1;
 rep(i,n){
  adde(s,i,1);
  int t=read();
     while(t--)
      adde(i,n+read(),1);
 }
 rep(i,m)
  adde(n+i,t,2);
 if(maxflow(s,t,tot)!=m*2){
  puts("NO");
  return 0;
 }
 else{
  puts("YES");
  rep(i,n){
   for(edge *ee=head[i];ee;ee=ee->next)
       if(!ee->cap)
       ans[ee->to-n][++ans[ee->to-n][0]]=i;
  }
  rep(i,m)
      printf("2 %d %d\n",ans[i][1],ans[i][2]);
 }
 return 0;
}

------------------------------------------------------------------------------------

Student‘s Morning

Time Limit: 250MS   Memory Limit: 6144KB   64bit IO Format: %I64d & %I64u

Submit

Status

Description

242. Student‘s Morning

time limit per test: 0.25 sec. 
memory limit per test: 6144 KB

input: standard 
output: standard

One Monday morning after some very fun party N students woke up at the flat of one of them. Notice that it was a Monday morning and every student of that party needs to be in his university this day. But nobody wants to go to his university alone (there were students from different universities). So, they decided to select from all universities only K of them to visit. Every selected university must be visited by at least two of the students. Every student has his own preference list of universities. It means, if some university is in list of some student‘s preferred universities, this student can go to this university with some non-empty company of students. Notice, that some of students can stay at the flat and continue drinking "juices" and playing "games". For example, student Shokman was to stay home (due to failed exam) with foreign student Chokman, who remained home because of runny nose. 
In that problem there are no preferences between students, because if they have very fun party that already means that everyone of them prefers anybody from this company.

More formally, your task is, given numbers of students, selected universities and preference list of every student, to decide whether it is possible to visit all universities by at least two of students or no, and if it is possible you must output for each university numbers of students, which have to go to it in one company. One student can‘t be in more than one company.

Input

First line of input file contains two numbers N and K (0<=K<=N<=200). Next N lines contain preference lists of each student. Every preference list is started by number of preferred universities followed by numbers of these universities.

Output

First line of output file must contain word "YES" (without quotes), if it possible to visit all universities, satisfying rules of that task or word "NO" (also without quotes) when it is impossible. In case of positive answer next K lines must contain lists of students, who are going to corresponding university. First number in list of students must be a number of students in the list, followed by numbers of these students.

Sample test(s)

Input

Test #1

4 2 
1 1 
2 1 2 
1 2 
2 1 2

Test #2 
3 2 
2 1 2 
2 1 2 
2 1 2

Output

Test #1

YES 
2 1 2 
2 3 4

Test #2 
NO

[submit]

[forum]


Author: Alexey Preobrajensky
Resource: ---
Date: October, 2003
Server time: 2016-01-06 17:22:44 Online Contester Team © 2002 - 2015. All rights reserved.

Source

Submit

Status

时间: 2024-10-09 21:34:04

SGU242:最大流的相关文章

sgu-242 Student&#39;s Morning

题目大意: 有N个人,K个大学,每个人有Gi个心仪的学校,为集合Si,然后然后这N个人可以去任意一个且仅一个他心仪的学校,问是否可以使得K个学校每个学校都有不小于2个人去.如果没有,输出"NO",否则输出"YES",然后接下来K行,每行一个数,表示去第i个学校的人有几个,接下来输出那几个人去哪个学校.PS:有的人可以不去学校,只要满足每个学校有2个及以上的人去就行了,不要求N个人每个人都去上学,比如可以在家浪...... 解题思路: 首先这道题目显然可以用有上下确界

解题报告 之 SGU242 Student&#39;s Morning

解题报告 之 SGU242 Student's Morning Description One Monday morning after some very fun party N students woke up at the flat of one of them. Notice that it was a Monday morning and every student of that party needs to be in his university this day. But no

对IO流的操作(文件大小,拷贝,移动,删除)

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.SequenceInputStream; class LjyFileClass { /*LjyFileClass工具类使用需知: * * 1.计算

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

Java学习之IO流三

1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中(高效流) 1 /** 2 * 1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中 3 * @author vanguard 4 * 5 */ 6 public class Demo01 { 7 public static void main(String[] args) { 8 //键盘输入两个文件夹路径 9 Scanner sc = new Scanner(System.in); 1

标准文档流

标准流指的是在不使用其他的与排列和定位相关的特殊CSS规则时,各种元素的排列规则.HTML文档中的元素可以分为两大类:行内元素和块级元素.       1.行内元素不占据单独的空间,依附于块级元素,行内元素没有自己的区域.它同样是DOM树中的一个节点,在这一点上行内元素和块级元素是没有区别的.       2.块级元素总是以块的形式表现出来,并且跟同级的兄弟块依次竖直排列,左右自动伸展,直到包含它的元素的边界,在水平方向不能并排.盒子在标准流中的定位原则margin控制的是盒子与盒子之间的距离,

Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类

一.Properties 类(java.util)     概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,properties集合的key和value都是固定的数据类型(String),该集合提供了一些特有的方法存取值,是唯一一个可以与IO流相结合的集合; 定义:public class Properties extends Hashtable

14. 流、文件和IO

前言 InputStream/OutStream流用来处理设备之间的数据传输 Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列.输入流表示从一个源读取数据,输出流表示向一个目标写数据. 流按操作数据分为两种:字节流与字符流 按流向分为:输入流(InputStream)和输出流(OutputStream) Java 为 I/O 提供了强大的而

videojs集成--播放rtmp流

之前说到已经把流推送过来了,这时候就可以使用videojs来进行显示播放. 首先要先有一个文件,那就是video-js.swf 因为,这种播放方式html已经不能很好的进行播放了,需要用到flash来播放,videojs在这个地方就用到了这个. 代码就是下面这样. 里面一些细节注释都有. 重点就是看<video>标签里面的内容 [html] view plain copy <!DOCTYPE html> <html lang="en"> <he