最近总是刷阅读理解还是有点好处的...给出k个学校和n和学生的喜欢名单,每个学校至少有两个学生拜访,每个学生只能去一次,求是否能满足k个学校都能去拜访,能的话给出方案。(然而里面的company那里是说什么...)。试着建图,s到每个学生c=1,每个学生到喜欢的学校c=1,每个学校到t c=2,然后就好了,感觉不算难。
------------------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
const int inf=0x3f3f3f;
int read(){
int x=0,f=1;char c=getchar();
while(!isdigit(c)){
if(c==‘-‘) f=-1;
c=getchar();
}
while(isdigit(c)){
x=x*10+c-‘0‘;
c=getchar();
}
return x*f;
}
struct edge{
int to,cap;
edge *next,*rev;
};
edge e[100000],*cur[405],*p[405],*head[405],*pt=e;
int d[405],cnt[405],ans[205][3];
void add(int u,int v,int d){
pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++;
}
void adde(int u,int v,int d){
add(u,v,d);add(v,u,0);head[u]->rev=head[v];head[v]->rev=head[u];
}
int maxflow(int s,int t,int n){
clr(d,0);clr(cnt,0);cnt[0]=n;
int x=s,flow=0,a=inf;
while(d[s]<n){
edge *ee;
for(ee=cur[x];ee;ee=ee->next){
if(ee->cap>0&&d[ee->to]+1==d[x])
break;
}
if(ee){
p[ee->to]=cur[x]=ee;
a=min(a,ee->cap);
x=ee->to;
if(x==t){
while(x!=s){
p[x]->cap-=a;
p[x]->rev->cap+=a;
x=p[x]->rev->to;
}
flow+=a;
a=inf;
}
}
else{
if(!--cnt[d[x]]) break;
d[x]=n;
for(ee=head[x];ee;ee=ee->next){
if(ee->cap>0&&d[ee->to]+1<d[x]){
d[x]=d[ee->to]+1;
cur[x]=ee;
}
}
cnt[d[x]]++;
if(x!=s) x=p[x]->rev->to;
}
}
return flow;
}
int main(){
int n=read(),m=read(),s=0,t=n+m+1,tot=t+1;
rep(i,n){
adde(s,i,1);
int t=read();
while(t--)
adde(i,n+read(),1);
}
rep(i,m)
adde(n+i,t,2);
if(maxflow(s,t,tot)!=m*2){
puts("NO");
return 0;
}
else{
puts("YES");
rep(i,n){
for(edge *ee=head[i];ee;ee=ee->next)
if(!ee->cap)
ans[ee->to-n][++ans[ee->to-n][0]]=i;
}
rep(i,m)
printf("2 %d %d\n",ans[i][1],ans[i][2]);
}
return 0;
}
------------------------------------------------------------------------------------
Time Limit: 250MS | Memory Limit: 6144KB | 64bit IO Format: %I64d & %I64u |
Description
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 |
|
Source