CodeForces 1484C: flujo de red de Diplomacia básica

Portal

Descripción del Título

Darte nnn niños, debes estar enmmElija un niño todos los días dentro de m días, y cada niño no puede ser seleccionado más de⌈ mx ⌉ \ lceil \ frac {m} {x} \ rceilXm , solo puede elegir a unos pocos niños todos los días y preguntarle si tiene un plan legal al final

análisis

La solución correcta a esta pregunta debería ser codiciosa, pero el flujo de la red puede ser directamente secundario.
Partimos de SSS apunta a1 ~ n 1 ~ n1 n Cada punto conectado a un caudal es⌈ mx ⌉ \ lceil \ frac {m} {x} \ rceilXm lado,1 ~ n 1 ~ n1 n cada punto a(n + 1) ~ (n + m) (n + 1) ~ (n + m)( n+1 ) ( n+m ) Cada punto con un caudal es1 11 lado, al final(n + 1) ~ (n + m) (n + 1) ~ (n + m)( n+1 ) ( n+m ) aTTT tráfico de enlace1 1En el borde de 1 , el flujo máximo se puede obtener al final
. La solución legal solo necesita juzgarse si el flujo en el lado está terminado.

Código

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10,M = N * 4;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
    
    return (b>0)?gcd(b,a%b):a;}
int n,m,S,T;
int h[N],ne[M],e[M],f[M],idx;
int q[N], d[N], cur[N];
int ans[N];

void add(int x,int y,int z){
    
    
    e[idx] = y,ne[idx] = h[x],f[idx] = z,h[x] = idx++;
    e[idx] = x,ne[idx] = h[y],f[idx] = 0,h[y] = idx++;
}

bool bfs(){
    
    
    int hh = 0, tt = 0;
    memset(d,-1,sizeof d);
    q[0] = S, d[S] = 0, cur[S] = h[S];
    while(hh <= tt){
    
    
        int t = q[hh ++ ];
        for(int i = h[t];~i;i = ne[i]){
    
    
            int j = e[i];
            if(d[j] == -1 && f[i]){
    
    
                d[j] = d[t] + 1;
                cur[j] = h[j];
                if(j == T) return true;
                q[ ++ tt] = j;
            }
        }
    }
    return false;
}

int find(int u,int limit){
    
    
    if(u == T) return limit;
    int flow = 0;
    for(int i = cur[u];~i && flow < limit;i = ne[i]){
    
    
        cur[u] = i;
        int j = e[i];
        if(d[j] == d[u] + 1 && f[i]){
    
    
            int t = find(j,min(f[i],limit - flow));
            // if(u >= 1 && u <= n && j >= n + 1 && j <= n + m && t){
    
    
            //     ans[j - n] = u;
            // }
            if(!t) d[j] = -1;
            f[i] -= t,f[i ^ 1] +=t,flow += t;
        }
    }
    return flow;
}

int dinic(){
    
    
    int res = 0,flow;
    while(bfs()) while(flow = find(S,INF)) res += flow;
    return res;
}


int main(){
    
    
    int ppp;
    read(ppp);
    while(ppp--){
    
    
        idx = 0;
        for(int i = 0;i <= n + m + 1;i++) h[i] = -1;
        read(n),read(m);
        S = 0,T = n + m + 1;
        for(int i = 1; i <= n; i++){
    
    
            add(0,i,(m + 1) / 2);
        }
        
        for(int i = 1; i <= m; i++){
    
    
            int x;
            scanf("%d",&x);
            int y;
            for(int j = 1; j <= x; j++){
    
    
                scanf("%d",&y);
                add(y,i + n,1);
            }
        }
        
        for(int i = 1; i <= m; i++){
    
    
            add(i + n,T,1);
        }
        if(dinic() != m) {
    
    
            puts("NO");
            continue;
        }
        puts("YES");
        for(int i = 1;i <= n;i++){
    
    
            for(int j = h[i];~j;j = ne[j]){
    
    
                int t = e[j];
                // cout << t << endl;
                if(t >= n + 1 && t <= n + m && !f[j]){
    
    
                    ans[t - n] = i;
                }
            }
        }
        for(int i = 1;i <= m;i++) printf("%d ",ans[i]);
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/
 

Supongo que te gusta

Origin blog.csdn.net/tlyzxc/article/details/115128306
Recomendado
Clasificación