看英文题真是麻烦...理解题意花的时间比想的时间还长...裸的网络流, 我们只要限制每个人出发流量为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
Author: | Alexey Preobrajensky |
Resource: | --- |
Date: | October, 2003 |
SGU 242. Student's Morning( 网络流 )