Problem Solving report SGU242 Student ' s morning

Source: Internet
Author: User

Problem Solving report SGU242 Student ' s morning

Description


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

More formally, your task was, given numbers of students, selected universities and preference list of every student, to Dec IDE whether it is possible to visit all universities by at least the students or no, and if it is possible your must out Put for each university numbers of students, which has to go to it on one company. One student can ' t is in more than one company.
InputFirst line of input file contains 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.
OutputFirst line of output file must contain word "YES" (without quotes), if it possible-visit all universities, satisfying r Ules 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 is going to corresponding university. First number in List of students must is a number of students in the list, followed by numbers of these students.
Sample Test (s)
Input
4 2 1 1 2 1 2 1 2 2 1 2 3 2 2 1 2 2 1 2 2 1 2Output

YES
2 1 2
2 3 4
NO

The main topic: a good water problem what do you read comprehension! N Students m-universities, each student has the university that he wants to go to, a student to go to a university (also can not go), each university must have at least two people to go, ask whether is a kind of plan satisfies? If any, output any one of the satisfied scenarios.
Analysis: A bit like binary graph matching, and then the full flow to determine whether the satisfaction, coupled with the residual network method output scheme. The specific ideas are as follows, the super source is connected with each student, the load is 1, each university is connected with the Super meeting point, the load is 2. Each student is connected to each university and has a load of 1. Then run the maximum flow, if satisfied with the max_flow==m*2, represents a solution. At this point, each side of the network that is connected to the student is scanned, if it is not connected to SRC and the load is reduced (indicating that the student has gone to the school), then add to the list of the corresponding university and finally output the list. In addition this problem memory is slightly small, so must control good edge number, probably is n*m a bit more.
on the code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <queue>using namespace std;const int maxn = 520;const int MAXM = 100000;const int INF = 0x3f3f3f3f; struct edge{int from, to, cap, Next, oricap;};  Edge edge[maxm];int head[maxn];int stu[405][405];int level[maxn];int src, des, cnt;void addedge (int from, int. to, int cap ) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].oricap = Cap;edge[cnt].next = Head[from];head[fr  OM] = Cnt++;swap (from, to); Edge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].oricap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level);queue<int> Q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (src); w Hile (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (edg E[i].cap > 0 && level[v] = =-1) {Level[v] = Level[u] + 1;q.push (v);}}} REturn Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to; if (Edge[i].cap > 0 && level[v] = = Level[u] + 1) {tem = DFS (V, min (f, edge[i].cap)), if (Tem > 0) {edge[i]. Cap-= tem;edge[i ^ 1].cap + = Tem;return tem;}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src)) > 0) {ans + = tem;}} return ans;}  int main () {int n, m;src = 0; des = 510;string str;while (cin >> n >> m) {memset (head,-1, sizeof head); cnt = 0;for (int i = 1; I <= n; i++) {Addedge (src, I, 1); int unum;cin >> unum;for (int j = 1; J <= Unum; j + +) {int U;cin >> U;addedge (i, U + 200, 1);}} for (int i = 1; I <= m; i++) {Addedge (i + 0, des, 2) stu[i + 200][0] =;} if (Dinic () < M * 2) {cout << "NO" << endl;continue;} for (int i = 1, i <= N; i++) {for (int j = head[i]; J! =-1; j = edge[j].next) {if (edge[j].to! = SRC&AMP;&AMP;EDGE[J].ORICAP&GT;EDGE[J].CAP) {Stu[edge[j].to][++stu[edge[j].to][0]] = i;}}} cout << "YES" << endl;for (int i = 1; I <= m; i++) {cout << stu[i + 200][0] << ""; for (int j = 1; J < Stu[i + 200][0]; J + +) {cout << stu[i + 200][j] << "";} cout << stu[i + 200][stu[i + 200][0]] << Endl;}} return 0;}

All right ... I wish the information security Contest success, although I am hugging the thigh haha haha ha ha haha BJ4 ...

Problem Solving report SGU242 Student ' s morning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.