码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1469 COURSES

时间:2014-06-09 15:36:50      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Description

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: 

  • every student in the committee represents a different course (a student can represent a course if he/she visits that course) 
  • each course has a representative in the committee 

Input

Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format: 

P N 
Count1 Student1 1 Student1 2 ... Student1 Count1 
Count2 Student2 1 Student2 2 ... Student2 Count2 
... 
CountP StudentP 1 StudentP 2 ... StudentP CountP 

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses ?from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N. 
There are no blank lines between consecutive sets of data. Input data are correct. 

Output

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

Sample Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Sample Output

YES
NO

题目一看就知道是求二分图的最大匹配,看看最大匹配是不是等于P,那么就简单咯,可以用匈牙利算法,最大流算法来求,还有一个优化的地方,如果P大于N的话,直接输出NO
bubuko.com,布布扣
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

struct Edge
{
    int from,to,cap,flow;
};

struct DINIC{
    static const int maxn=2500+10;
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    int d[maxn],cur[maxn];
    bool vis[maxn];

    void AddEdge(int from,int to,int cap)
    {
        Edge temp;
        temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to;
        edges.push_back(temp);
        temp.cap=0; temp.flow=0; temp.from=to; temp.to=from;
        edges.push_back(temp);
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for (int i=0;i<G[x].size();i++)
            {
                Edge& e=edges[G[x][i]];
                if (!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if (x==t || a==0) return a;
        int flow=0,f;
        for (int& i=cur[x];i<G[x].size();i++)
        {
            Edge& e=edges[G[x][i]];
            if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0) break;
            }
        }
        return flow;
    }

    int Dinic()
    {
        int flow=0;
        while (BFS())
        {
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,inf);
        }
        return flow;
    }

    void init()
    {
        for (int i=0;i<=maxn;i++) G[i].clear();
        edges.clear();
    }
};

DINIC represent;

int main()
{
    //freopen("in.txt","r",stdin);
    int N,P,x,num,Case;
    scanf("%d",&Case);
    while(Case--)
    {
        scanf("%d%d",&P,&N);
        represent.n=P+N;
        represent.init();
        for (int i=1;i<=P;i++)
        {
            represent.AddEdge(0,i,1);
            scanf("%d",&num);
            while(num--)
            {
                scanf("%d",&x);
                represent.AddEdge(i,P+x,1);
            }
        }
        int T=P+N+1;
        for (int i=P+1;i<=P+N;i++)
        {
            represent.AddEdge(i,T,1);
        }
        represent.s=0;
        represent.t=T;
        if (P>N)
        {
            printf("NO\n");
            continue;
        }
        int ans=represent.Dinic();
        if (ans==P) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
bubuko.com,布布扣

 

poj 1469 COURSES,布布扣,bubuko.com

poj 1469 COURSES

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/chensunrise/p/3776501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!