Submission #1293250


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <numeric>
#include <vector>
#include <algorithm>
#include <queue>

// C++11-compatible FastIO
namespace FastIO{static constexpr size_t E=(1<<17);static constexpr size_t T=23
,A=334;static constexpr char O[]="%.16f";FILE*e=stdin,*t=stdout;char a[E|1],*o,
*i;char n[E|1],*s,*h;inline size_t r(){size_t g=fread(a,1,E,e);*(i=a+g)=0;o=a;
return g;}inline size_t d(){std::ptrdiff_t g=i-o;if(a+g>=o){return 0;}memcpy(a,
o,g);size_t v=fread(a+g,1,E-g,e);*(i=a+(g+=v))=0;o=a;return v;}inline void l(d\
ouble&g){if(*o==0&&r()){g=0;return;}char*b;g=strtod(o,&b);while(b==o){if(!r()){
g=0;return;}g=strtod(o,&b);}if(b==i&&d()){g=strtod(a,&b);}o=b;}inline void l(c\
har&g){if(*o==0){r();}if((g=*o)!=0){++o;}}inline void l(char*g){while(1){if(*o
==0&&!r()){*g=0;return;}if(*o<=' '){++o;}else{break;}}for(char*b=o;;++o){if(*o
==0){memcpy(g,b,o-b);g+=o-b;if(!r()){*g=0;return;}b=o;}else if(*o<=' '){memcpy(
g,b,o-b);g+=o-b;*g=0;return;}}}template<class N>inline void l(N&g,typename std
::enable_if<std::is_signed<N>::value>::type*y=0){g=0;int p=0;if(o+T>=i){d();}w\
hile(*o<'-'){++o;}if(*o=='-'){p=1;++o;}do{g=g*10+*o-'0';}while(*++o>='0');++o;
if(p){g=-g;}}template<class N>inline void l(N&g,typename std::enable_if<!std::
is_signed<N>::value>::type*y=0){g=0;if(o+T>=i){d();}while(*o<'0'){++o;}do{g=g*
10+*o-'0';}while(*++o>='0');++o;}inline void c(){while((*o!=0||r())&&*o++<=' ')
{}}inline void c(char g){while((*o!=0||r())&&*o++<=' '){}}inline void u(){fwri\
te(n,1,s-n,t);s=n;}inline void m(char g){if(s+1>=h)u();*s++=g;}inline void m(c\
onst char*g){size_t y=strlen(g);if(s+y>=h){u();}if(s+y>=h){fwrite(g,1,y,t);}el\
se{memcpy(s,g,y);s+=y;}}template<size_t N>inline void m(const char(&g)[N]){m(&g
[0]);}inline void m(double g){char y[A];size_t p=snprintf(y,A,O,g);char*b=s+p;
if(b>=h){u();b=n+p;}memcpy(s,y,p);s=b;}template<class N>inline void m(N g,type\
name std::enable_if<std::is_signed<N>::value>::type*y=0){if(s+T>=h){u();}if(g==
0){*s++='0';return;}char k[T],*j=k+T,*x=j;if(g<0){*s++='-';g=-g;}do{*--j=g%10+
'0';g/=10;}while(g);memcpy(s,j,x-j);s+=x-j;}template<class N>inline void m(N g,
typename std::enable_if<!std::is_signed<N>::value>::type*y=0){if(s+T>=h){u();}
if(g==0){*s++='0';return;}char v[T],*k=v+T,*j=k;do{*--k=g%10+'0';g/=10;}while(g
);memcpy(s,k,j-k);s+=j-k;}struct Scanner{Scanner(){o=a;i=a+E;r();}template<cla\
ss Z>inline void scan(Z&&z){l(z);}template<class Z,class...Q>inline void scan(Z
&&z,Q&&...q){l(z);scan(q...);}};struct Printer{Printer(){s=n;h=n+E;}~Printer(){
u();}template<class Z>inline void print(Z z){m(z);}template<class Z,class...Q>
inline void print(Z z,Q...q){m(z);print(q...);}template<class Z>inline void pr\
intln(Z z){m(z);m('\n');}};}

FastIO::Scanner cin;
FastIO::Printer cout;

using Weight=int;

static const Weight INF=1<<29;

struct Arc {
    // accessible to the reverse edge
    size_t src, dst;
    Weight capacity, flow;
    size_t rev;
    Arc() {}
    Arc(size_t src, size_t dst, Weight capacity):
        src(src), dst(dst), capacity(capacity), flow(0), rev(-1)
    {}
    Weight residue() const {
        return capacity - flow;
    }
};

using Arcs=std::vector<Arc>;
using Node=std::vector<Arc>;
using FlowNetwork=std::vector<Node>;

void connect(FlowNetwork &g, size_t s, size_t d, Weight c=1) {
    g[s].emplace_back(s, d, c);
    g[d].emplace_back(d, s, 0);

    g[s].back().rev = g[d].size()-1;
    g[d].back().rev = g[s].size()-1;
}

void join(FlowNetwork &g, size_t s, size_t d, Weight c=1) {
    g[s].emplace_back(s, d, c);
    g[d].emplace_back(d, s, c);

    g[s].back().rev = g[d].size()-1;
    g[d].back().rev = g[s].size()-1;
}

Weight maximum_flow(FlowNetwork &g, size_t s, size_t t) {
    /* Goldberg-Tarjan */
    size_t V=g.size();
    std::vector<size_t> d(V);
    std::vector<Weight> excess(V); excess[s]=INF;

    auto _global_relabel=[&, t]()->void {
        std::queue<size_t> q; q.push(t);
        d.assign(V, INF); d[t]=0;
        while (!q.empty()) {
            size_t u=q.front(); q.pop();
            for (Arc &e: g[u])
                if (g[e.dst][e.rev].residue()>0 && d[e.src]+1<d[e.dst]) {
                    q.push(e.dst);
                    d[e.dst] = d[e.src] + 1;
                }
        }
    };

    auto _push=[&](Arc &e)->void {
        Weight delta=std::min(excess[e.src], e.residue());
        e.flow += delta;
        excess[e.src] -= delta;
        excess[e.dst] += delta;
    };

    _global_relabel();
    if (d[s] >= INF) return 0;

    std::vector<std::queue<size_t>> B(V); B[d[s]].push(s);
    for (size_t b=d[s], count_=0; ~b; ++count_) {
        if (B[b].empty()) {
            --b;
            continue;
        }

        size_t v=B[b].front(); B[b].pop();
        if (!excess[v] || v==t) continue;

        for (Arc &e: g[v]) {
            size_t w=e.dst;
            if (e.residue()>0 && d[v]==d[w]+1) {
                _push(e);
                if (excess[w]>0 && w!=t)
                    B[d[w]].push(w);
            }
        }

        if (!excess[v]) continue;
        d[v] = V;
        for (Arc &e: g[v])
            if (e.residue() > 0)
                d[e.src] = std::min(d[e.src], d[e.dst]+1);

        if (d[v] < V) B[b=d[v]].push(v);
        if (!((++count_)%=V>>1|1)) _global_relabel();
    }
    return excess[t];
}

int main() {
    size_t N;
    int G, E;
    cin.scan(N, G, E);

    FlowNetwork g(N+1);
    for (int i=0; i<G; ++i) {
        size_t p;
        cin.scan(p);
        connect(g, p, N);
    }

    for (int i=0; i<E; ++i) {
        size_t a, b;
        cin.scan(a, b);
        join(g, a, b);
    }

    cout.println(maximum_flow(g, 0, N));
    return 0;
}

Submission Info

Submission Time
Task D - 浮気予防
User rsk0315
Language C++14 (GCC 5.4.1)
Score 100
Code Size 5843 Byte
Status AC
Exec Time 3 ms
Memory 768 KB

Judge Result

Set Name part All
Score / Max Score 99 / 99 1 / 1
Status
AC × 27
AC × 61
Set Name Test Cases
part test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_41_AB.txt, test_47_AB.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_26_A.txt, test_27_A.txt, test_28_A.txt, test_29_A.txt, test_30_A.txt, test_31_A.txt, test_32_A.txt, test_33_A.txt, test_34_A.txt, test_35_A.txt, test_36_A.txt, test_37_A.txt, test_38_A.txt, test_39_A.txt, test_40_A.txt, test_41_AB.txt, test_42_A.txt, test_43_A.txt, test_44_A.txt, test_45_A.txt, test_46_A.txt, test_47_AB.txt, test_48_A.txt, test_49_A.txt, test_50_A.txt, test_51_A.txt
Case Name Status Exec Time Memory
sample_01.txt AC 2 ms 384 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
sample_04.txt AC 1 ms 256 KB
sample_05.txt AC 1 ms 256 KB
test_01_AB.txt AC 1 ms 256 KB
test_02_AB.txt AC 1 ms 256 KB
test_03_AB.txt AC 1 ms 256 KB
test_04_AB.txt AC 1 ms 256 KB
test_05_AB.txt AC 1 ms 384 KB
test_06_AB.txt AC 1 ms 256 KB
test_07_AB.txt AC 1 ms 256 KB
test_08_AB.txt AC 1 ms 256 KB
test_09_AB.txt AC 1 ms 256 KB
test_10_AB.txt AC 1 ms 256 KB
test_11_AB.txt AC 1 ms 256 KB
test_12_AB.txt AC 1 ms 256 KB
test_13_AB.txt AC 1 ms 256 KB
test_14_AB.txt AC 1 ms 256 KB
test_15_AB.txt AC 1 ms 256 KB
test_16_AB.txt AC 1 ms 256 KB
test_17_AB.txt AC 1 ms 256 KB
test_18_AB.txt AC 1 ms 256 KB
test_19_AB.txt AC 1 ms 256 KB
test_20_AB.txt AC 1 ms 256 KB
test_21_AB.txt AC 1 ms 256 KB
test_22_AB.txt AC 1 ms 256 KB
test_23_AB.txt AC 1 ms 256 KB
test_24_AB.txt AC 1 ms 256 KB
test_25_AB.txt AC 1 ms 256 KB
test_26_A.txt AC 2 ms 768 KB
test_27_A.txt AC 2 ms 768 KB
test_28_A.txt AC 3 ms 768 KB
test_29_A.txt AC 3 ms 768 KB
test_30_A.txt AC 2 ms 768 KB
test_31_A.txt AC 3 ms 768 KB
test_32_A.txt AC 1 ms 384 KB
test_33_A.txt AC 2 ms 512 KB
test_34_A.txt AC 1 ms 256 KB
test_35_A.txt AC 1 ms 384 KB
test_36_A.txt AC 1 ms 384 KB
test_37_A.txt AC 1 ms 384 KB
test_38_A.txt AC 1 ms 256 KB
test_39_A.txt AC 1 ms 256 KB
test_40_A.txt AC 1 ms 256 KB
test_41_AB.txt AC 1 ms 256 KB
test_42_A.txt AC 2 ms 432 KB
test_43_A.txt AC 1 ms 256 KB
test_44_A.txt AC 1 ms 384 KB
test_45_A.txt AC 1 ms 256 KB
test_46_A.txt AC 1 ms 256 KB
test_47_AB.txt AC 1 ms 256 KB
test_48_A.txt AC 1 ms 256 KB
test_49_A.txt AC 1 ms 256 KB
test_50_A.txt AC 1 ms 256 KB
test_51_A.txt AC 1 ms 256 KB