Composition of supplementary solution to a problem Luogu P1231

Cackle

The problem was quite bare a maximum flow, just need to build a map on the line.

But why I could not even side could even anti-ah. . . .

Solution

Because the relationship between the two groups which know the book, so the book in the middle, both sides were put workbooks and answers.

A super virtual point source, are connected to the \ (N1 \) Workbook, the flow of each side are \ (1 \) , because each pair can only be used once (the same below).

A super virtual meeting point, respectively, by \ (N3 \) answers connected to it, a flow rate of \ (1 \) .

According to correspondence between the title given by the workbooks and even to book, even a book to answer are, flow \ (1 \) . But each book can only be used once, so we use the policy split point, the book is split into two, and then connect to a flow rate of \ (1 \) side, thus ensuring each book only once.

And then directly on the template, \ (Dinic \) directly to wave away.

Time complexity: \ (O (n-2 ^ \ CDOT m) \) Actually it not, in fact, is O (metaphysics) of

Code:

#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
    int f=1;char k=getchar();x=0;
    for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
    for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
    x*=f;
}
template<class T>il print(T x){
    if(x/10) print(x/10);
    putchar(x%10+'0');
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
    int res=1,bas=x%mod;
    while(m){
        if(m&1) res=(res*bas)%mod;
        bas=(bas*bas)%mod,m>>=1;
    }
    return res%mod;
}
const int MAXN = 4e4+5;
int n1,n2,n3,m,u,v,s,t,cur[MAXN],head[MAXN],num_edge=-1,dis[MAXN];
struct Edge{
    int next,to,w;
    Edge(){}
    Edge(int next,int to,int w):next(next),to(to),w(w){}
}edge[1000005];
il add_edge(int u,int v,int w){
    edge[++num_edge]=Edge(head[u],v,w),head[u]=num_edge;
    edge[++num_edge]=Edge(head[v],u,0),head[v]=num_edge;
}
inl bool BFS(int s,int t){
    queue<int> q;q.push(s);
    del(dis,0),dis[s]=1;
    while(!q.empty()){
        ri pos=q.front();q.pop();
        for(ri i=head[pos];i!=-1;i=edge[i].next)
            if(!dis[edge[i].to]&&edge[i].w>0){
                dis[edge[i].to]=dis[pos]+1;
                if(edge[i].to==t) return true;
                q.push(edge[i].to);
            }
    }
    return false;
}
it DFS(int now,int t,int flow){
    if(now==t) return flow;
    ri s=0,k;
    for(ri &i=cur[now];i!=-1;i=edge[i].next)
        if(dis[edge[i].to]==dis[now]+1&&edge[i].w>0){
            k=DFS(edge[i].to,t,min(flow-s,edge[i].w));
            s+=k,edge[i].w-=k,edge[i^1].w+=k;
            if(s==flow) break;
        }
    if(s==0) dis[now]=0;
    return s;
}
it Dinic(int s,int t){
    ri ans=0;
    while(BFS(s,t)){
        memcpy(cur,head,sizeof(head));
        ans+=DFS(s,t,INF);
    }
    return ans;
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    read(n1),read(n2),read(n3),t=n1*2+n2+n3+1,del(head,-1);
    for(ri i=1;i<=n1;++i) add_edge(i+n2,i+n1+n2,1);
    for(ri i=1;i<=n2;++i) add_edge(s,i,1);
    for(ri i=1;i<=n3;++i) add_edge(i+n1*2+n2,t,1);
    read(m);
    for(ri i=1;i<=m;++i) read(u),read(v),add_edge(v,u+n2,1);
    read(m);
    for(ri i=1;i<=m;++i) read(u),read(v),add_edge(u+n2+n1,v+n2+n1*2,1);
    printf("%d",Dinic(s,t));
    return 0;
}

to sum up

Routine ah, are routine. .

I've been speechless. .

Guess you like

Origin www.cnblogs.com/TheShadow/p/11372091.html