日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/8/22 编程问答 38 如意码农
生活随笔 收集整理的這篇文章主要介紹了 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&#39;s Morning( 网络流 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。