cf1264 E. Beautiful League

链接

点击跳转

题解

在这里插入图片描述

原题题解

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct Graph2
{
    ll head[maxn], next[maxe], to[maxe], w[maxe], c[maxe], tot;
    void clear(ll N)
    {
        ll i;
        for(i=1;i<=N;i++)head[i]=0;
        for(i=2;i<=tot;i++)next[i]=0;
        tot=1;
    }
    void add(ll a, ll b, ll ww, ll cc){to[++tot]=b;next[tot]=head[a];w[tot]=ww;c[tot]=cc;head[a]=tot;}
    void adde(ll a, ll b, ll ww,ll cc){add(a,b,ww,cc);add(b,a,-ww,0);}
    #define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
struct Mininum_Cost_Flow
{
    ll dist[maxn], S, T, in[maxn], pre[maxn], cost, maxflow;    //T必须是编号最大的点
    queue<ll> q;
    #define inf linf
    bool spfa(Graph2& G)
    {
        int i, p, x;
        for(i=1;i<=T;i++)dist[i]=inf;
        dist[S]=0;in[S]=1;q.push(S);
        while(!q.empty())
        {
            in[x=q.front()]=0;q.pop();
            forp(x,G)
                if(G.c[p] and dist[G.to[p]]>dist[x]+G.w[p])
                {
                    dist[G.to[p]]=dist[x]+G.w[p];
                    pre[G.to[p]]=p;
                    if(!in[G.to[p]])q.push(G.to[p]),in[G.to[p]]=1;
                }
        }
        return dist[T]!=inf;
    }
    ll augment(Graph2& G)
    {
        ll flow=inf, x;
        for(x=T;x^S;x=G.to[pre[x]^1])flow=min(flow,G.c[pre[x]]);
        for(x=T;x^S;x=G.to[pre[x]^1])G.c[pre[x]]-=flow, G.c[pre[x]^1]+=flow;
        cost+=dist[T]*flow;
        return flow;
    }
    void run(Graph2& G)
    {
        maxflow=cost=0;
        while(spfa(G))maxflow+=augment(G);
    }
    #undef inf
}cf;
ll cnt[maxn], ans[55][55];
set<pll> e;
map<ll,pll> table;
int main()
{
    ll n, i, j, m, u, v, tot=0;
    n=read(), m=read();
    rep(i,m)
    {
        u=read(), v=read();
        e.em(pll(u,v));
        e.em(pll(v,u));
        ans[u][v]=1;
        cnt[u]++;
    }
    cf.S = n+(n-1)*n/2+10, cf.T=cf.S+1;
    G.clear(cf.T);
    rep(i,n)
    {
        while(cnt[i]<n)G.adde(i,cf.T,cnt[i]++,1);
    }
    tot=n;
    rep(i,n)for(j=i+1;j<=n;j++)
    {
        if(e.find(pll(i,j))!=e.end())continue;
        tot++;
        G.adde(cf.S,tot,0,1);
        table[G.tot-1]=pll(i,j);
        G.adde(tot,i,0,1);
        G.adde(tot,j,0,1);
    }
    cf.run(G);
    for(auto pr:table)
    {
        ll i=pr.second.first, j=pr.second.second;
        if(G.c[pr.first+2]==0)ans[i][j]=1;
        else ans[j][i]=1;
    }
    rep(i,n)rep(j,n)
    {
        putchar(0x30+ans[i][j]);
        if(j==n)putchar(10);
    }
    ll k, cnt=0;
    rep(i,n)rep(j,n)rep(k,n)if(ans[i][j]&ans[j][k]&ans[k][i])cnt++;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值