poj 1469 COURSES

Posted war1111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 1469 COURSES相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=1469

网络流跑二分图模板题

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()

using namespace std;
long long T,n,m,x,y,v,maxflow,tot,len;
long long head[N],deep[N],cur[N];
struct node{
    long long n;
    long long v;
    long long next;
}e[N];
queue<long long>q;

void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<0||c>9){if(c==-)y=-1;c=getchar();}
    while(c<=9&&c>=0){ x=(x<<1)+(x<<3)+c-0;c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p(-);x=-x;}
    if(x>9)o(x/10);
    p(x%10+0);
}

void push(long long x,long long y,long long v){
    e[tot].n=y;
    e[tot].v=v;
    e[tot].next=head[x];
    head[x]=tot++;
}

bool bfs(long long s,long long t){
    memset(deep,-1,sizeof(deep));
    For(i,0,n+m+1)
        cur[i]=head[i];
    deep[s]=0;
    q.push(s);
    while(!q.empty()){
        long long now=q.front();
        q.pop();
        for(long long i=head[now];i!=-1;i=e[i].next)
            if(deep[e[i].n]==-1 && e[i].v>0){
                deep[e[i].n]=deep[now]+1;
                q.push(e[i].n);
            }
    }
    return deep[t]!=-1;
}

long long dfs(long long now,long long t,long long lim){
    if(now==t||lim==0)
        return lim;
    long long flow=0,f;
    for(long long i=cur[now];i!=-1;i=e[i].next){
        cur[now]=i;
        if(deep[e[i].n]==deep[now]+1 && (f=dfs(e[i].n,t, min(lim,e[i].v) ))){
            flow+=f;
            lim-=f;
            e[i].v-=f;
            e[i^1].v+=f;
            if(!lim) break;
        }
    }
    return flow;
}

void dinic(long long s,long long t){
    while(bfs(s,t)){
        maxflow+=dfs(s,t,inf);
    }
}

void init(){
    memset(head,-1,sizeof(head));
    tot=maxflow=0;
}

signed main(){
    in(T);
    while(T--){
        init();
        in(n);in(m);
        For(i,1,n){
            in(len);
            For(j,1,len){
                in(x);
                push(i,n+x,1);
                push(n+x,i,0);
            }
        }
        For(i,1,n){
            push(0,i,1);
            push(i,0,0);
        }
        For(i,n+1,n+m){
            push(i,n+m+1,1);
            push(n+m+1,i,0);
        }
        dinic(0,n+m+1);
        if(maxflow>=n) puts("YES");
        else puts("NO");
    }     
    return 0;
}

 

以上是关于poj 1469 COURSES的主要内容,如果未能解决你的问题,请参考以下文章

poj 1469 COURSES

POJ 1469 COURSES

poj 1469 COURSES 题解

POJ 1469 COURSES

POJ 1469 -- COURSES (二分匹配)

poj——1469 COURSES