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

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

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

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cctype>

using namespace std;

const int MAXN = 209;

const int MAXV = 700;

inline int read() {

char c = getchar();

int ret = 0;

for(; !isdigit(c); c = getchar());

for(; isdigit(c); c = getchar()) ret = ret * 10 + c - ‘0‘;

return ret;

}

int N, M, V, S, T;

int ans[MAXN][2], c[MAXN];

int h[MAXV], cnt[MAXV];

struct edge {

int to, cap;

edge *next, *rev;

} E[100000], *pt = E, *head[MAXV], *p[MAXV], *cur[MAXV];

inline void Add(int u, int v, int w) {

pt->to = v; pt->cap = w; pt->next = head[u]; head[u] = pt++;

}

inline void AddEdge(int u, int v, int w) {

Add(u, v, w); Add(v, u, 0);

head[u]->rev = head[v];

head[v]->rev = head[u];

}

int maxFlow() {

for(int i = 0; i < V; i++) cur[i] = head[i];

memset(cnt, 0, sizeof cnt);

memset(h, 0, sizeof h);

cnt[0] = V;

edge* e;

int Flow = 0;

for(int A = MAXV, x = S; h[S] < V; ) {

for(e = head[x]; e; e = e->next)

if(e->cap && h[e->to] + 1 == h[x]) break;

if(e) {

A = min(e->cap, A);

p[e->to] = cur[x] = e;

if((x = e->to) == T) {

Flow += A;

for(; x != S; x = p[x]->rev->to) {

p[x]->cap -= A;

p[x]->rev->cap += A;

}

A = MAXV;

}

} else {

if(!--cnt[h[x]]) break;

h[x] = V;

for(e = head[x]; e; e = e->next) if(h[e->to] + 1 < h[x] && e->cap) {

h[x] = h[e->to] + 1;

cur[x] = e;

}

cnt[h[x]]++;

if(x != S) x = p[x]->rev->to;

}

}

return Flow;

}

void Init() {

N = read(); M = read(); V = N + M;

for(int i = 0; i < N; i++)

for(int t = read(); t--; ) AddEdge(i, read() + N - 1, 1);

S = V++; T = V++;

for(int i = 0; i < N; i++) AddEdge(S, i, 1);

for(int i = 0; i < M; i++) AddEdge(i + N, T, 2);

}

int main() {

Init();

if(maxFlow() == M * 2) {

puts("YES");

for(int x = 0; x < N; x++)

for(edge* e = head[x]; e; e = e->next)

if(!e->cap) ans[e->to - N][c[e->to - N]++] = x;

for(int x = 0; x < M; x++)

printf("2 %d %d\n", ++ans[x][0], ++ans[x][1]);

} else

puts("NO");

return 0;

}

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

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

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

时间: 2024-10-12 15:56:16

SGU 242. Student's Morning( 网络流 )的相关文章

SGU 242 Student&#39;s Morning 网络流(水

题目链接:点击打开链接 题意: 给定n个人,m个终点 下面n行表示每个人可以去m个点. 每个人只能去一个点. 输出任意一个方案使得每个点至少有2个人到达. 若存在输出m行,第一个数字表示i这个点来了几个人,后面是人的点标. 思路: 建一个二部图n-m,然后m到汇点限流2,判断是否满流,若不满流就无解. 若满流则其他的人随便走. #include <stdio.h> #include <string.h> #include <iostream> #include <

SGU 242 Student&amp;#39;s Morning 网络流(水

题目链接:contest=0&problem=242">点击打开链接 题意: 给定n个人,m个终点 以下n行表示每一个人能够去m个点. 每一个人仅仅能去一个点. 输出随意一个方案使得每一个点至少有2个人到达. 若存在输出m行,第一个数字表示i这个点来了几个人,后面是人的点标. 思路: 建一个二部图n-m,然后m到汇点限流2.推断是否满流,若不满流就无解. 若满流则其它的人随便走. #include <stdio.h> #include <string.h>

网络流专栏

最大流 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour (

网络流柱

最大流量 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour

网络流题集

最大流POJ 1273 Drainage DitchesPOJ 1274 The Perfect Stall (二分图匹配)POJ 1698 Alice's Chance(构图)POJ 1459 Power Network(构图)POJ 2112 Optimal Milking (二分)POJ 2455 Secret Milking Machine (二分)POJ 3189 Steady Cow Assignment (枚举)POJ 1637 Sightseeing tour (混合图欧拉回路)

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

待刷题目分类

各大OJ题目归类 Posted on 2012 年 8 月 8 日 by admin ---------–最优化问题------------- --------动态规划 SOJ1162 I-Keyboard SOJ2096 Maximum Submatrix SOJ2111 littleken bg SOJ2505 The County Fair SOJ2818 QQ音速 SOJ2469 Exploring Pyramids SOJ1833 Base Numbers SOJ2009 Zeros

Hibernate 一对多双向关联Demo

以Classes[班级]和Student[学生]为例的Demo //Classes.java public class Classes implements Serializable { private long cid; private String cname; private String cdesc; private Set<Student> students; //get和set } //Student .java public class Student implements Se

SGU242:最大流

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