SGU 242. Student's Morning( 网络流 )
2015-12-14 20:39
603 查看
看英文题真是麻烦...理解题意花的时间比想的时间还长...裸的网络流, 我们只要限制每个人出发流量为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;}----------------------------------------------------------------------------------
memory limit per test: 6144 KB[/align][align=center]input: standard
output: standard[/align]
[align=left]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.[/align][align=left]
Input[/align][align=left]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.[/align][align=left]
Output[/align][align=left]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.[/align][align=left]
Sample test(s)[/align][align=left]
Input[/align][align=left][/align][align=left][/align][align=left]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[/align][align=left][/align][align=left][/align][align=left]
Output[/align][align=left][/align][align=left][/align][align=left]Test #1
YES
2 1 2
2 3 4
Test #2
NO[/align][align=left][/align][align=left][/align][align=left][submit][/align][align=left][forum][/align][align=left][/align]
----------------------------------------------------------------------------------
#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
[align=center]time limit per test: 0.25 sec.memory limit per test: 6144 KB[/align][align=center]input: standard
output: standard[/align]
[align=left]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.[/align][align=left]
Input[/align][align=left]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.[/align][align=left]
Output[/align][align=left]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.[/align][align=left]
Sample test(s)[/align][align=left]
Input[/align][align=left][/align][align=left][/align][align=left]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[/align][align=left][/align][align=left][/align][align=left]
Output[/align][align=left][/align][align=left][/align][align=left]Test #1
YES
2 1 2
2 3 4
Test #2
NO[/align][align=left][/align][align=left][/align][align=left][submit][/align][align=left][forum][/align][align=left][/align]
Author: | Alexey Preobrajensky |
Resource: | --- |
Date: | October, 2003 |
相关文章推荐
- bzoj1497[NOI2006]最大获利
- 为什么深层神经网络难以训练
- 网络爬虫:爬取动态网页(例如javascript)
- 一些简单的网络流模型
- Http中Set-Cookie
- 简述tcp与udp的相同和不同
- 技术向:一文读懂卷积神经网络
- [Servlet&JSP] HttpSession会话管理
- core.min.js:36 XMLHttpRequest cannot load http://【地址】. No 'Access-Control-Allow-Origin' header is pr
- "http://schemas.xmlsoap.org/wsdl/", the namespace on the "definitions" element, is not a valid SOAP
- 异步的方式查看网络图片
- 计算机网络笔记(二)谢希仁版
- 几种TCP连接中出现RST的情况
- UNIX网络编程卷1 server编程范式0 迭代server
- Apache HTTP Client Removal
- LTE物理层几个基本概念的定义和相互关系 http://blog.sina.com.cn/s/blog_696122b50100n6df.html
- 【负载均衡】四层和七层负载均衡的区别
- 神经网络编程入门
- java发送http的get、post请求
- 实现微信文章页面 http://mp.weixin.qq.com/s?__biz=MjM5MDI3OTAwMg==&mid=200337417&idx=1&sn=5959ed1d722c7da66b