답안 #443001

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
443001 2021-07-09T13:13:50 Z leaked 결혼 문제 (IZhO14_marriage) C++14
82 / 100
1500 ms 2964 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define vec vector
#define sz(x) ( int)x.size()
#define m_p make_pair
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int,ll> pil;
typedef pair<int,int> pii;
typedef unsigned long long ull;
auto rng=bind(uniform_int_distribution<int>(1,1000),mt19937(time(0)));
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=3e4+1;
const int K=170;
const int M=2001;
vec<int>g[N];
int tt=1;
bool kuhn(int v,vec<int> &u,vec<short> &mt){
    if(u[v]==tt) return 0;
    u[v]=tt;
    for(auto &z : g[v]){
        if(mt[z]==-1){
            mt[z]=v;
            return 1;
        }
    }
    for(auto &z : g[v]){
        if(kuhn(mt[z],u,mt)){
            mt[z]=v;
            return 1;
        }
    }
    return 0;
}
vec<short> mt[N/K+1];
short cnt[N/K+1];
vec<int> u(N,0);
vec<short> cr(M);
int c=0;
int n,m;
bool check(int l,int r){
    if(r/K>0){
        for(int i=0;i<m;i++) cr[i]=mt[r/K-1][i];
        c=cnt[r/K-1];
    }
    else{
        for(int i=0;i<m;i++) cr[i]=-1;
        c=0;
    }
    for(int i=max(l,(r/K)*K);i<=r;i++){
        tt++;
        if(kuhn(i,u,cr)) c++;
    }
    return c==m;
}
void add(int id){
    for(int i=id/K;i<=(n-1)/K;i++){
        tt++;
        cnt[i]+=kuhn(id,u,mt[i]);
    }
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int k;
    cin>>n>>m>>k;
    for(int i=0;i<k;i++){
        int v,u;
        cin>>v>>u;--v;--u;
        g[v].pb(u);
    }
    for(int i=0;i<N/K+1;i++){
        mt[i].assign(m,-1);
    }
    for(int j=0;j<m;j++) cr[j]=-1;
    int l=n;
    int h=0;
    while(h!=m && l>0){
        l--;
        tt++;
        h+=kuhn(l,u,cr);
    }
    if(h!=m){
        cout<<0;
        return 0;
    }
    for(int i=n-1;i>l;i--){
        add(i);
    }
    int r=n-1;
    ll ans=0;
    for(int i=l;i>=0;i--){
//        cout<<i<<' '<<r<<endl;
        add(i);
        while(check(i,r-1)) r--;
        ans+=(n-r);
    }
    cout<<ans;
    return 0;
}

/*
5
1 2
2 3
3 4
4 5
5 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
3 Correct 1 ms 1100 KB Output is correct
4 Correct 1 ms 1100 KB Output is correct
5 Correct 1 ms 1100 KB Output is correct
6 Correct 1 ms 1100 KB Output is correct
7 Correct 1 ms 1100 KB Output is correct
8 Correct 1 ms 1100 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
11 Correct 1 ms 1100 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 1 ms 1100 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 1100 KB Output is correct
16 Correct 1 ms 1100 KB Output is correct
17 Correct 1 ms 1100 KB Output is correct
18 Correct 1 ms 1100 KB Output is correct
19 Correct 2 ms 1100 KB Output is correct
20 Correct 1 ms 1100 KB Output is correct
21 Correct 1 ms 1100 KB Output is correct
22 Correct 1 ms 1100 KB Output is correct
23 Correct 1 ms 1100 KB Output is correct
24 Correct 1 ms 1100 KB Output is correct
25 Correct 17 ms 1296 KB Output is correct
26 Correct 38 ms 1100 KB Output is correct
27 Correct 1 ms 1100 KB Output is correct
28 Correct 1 ms 1100 KB Output is correct
29 Correct 112 ms 1232 KB Output is correct
30 Correct 80 ms 1352 KB Output is correct
31 Correct 181 ms 1628 KB Output is correct
32 Correct 1059 ms 1372 KB Output is correct
33 Correct 1 ms 1228 KB Output is correct
34 Correct 3 ms 1356 KB Output is correct
35 Correct 315 ms 1868 KB Output is correct
36 Correct 244 ms 1852 KB Output is correct
37 Correct 1274 ms 1932 KB Output is correct
38 Execution timed out 1593 ms 2284 KB Time limit exceeded
39 Correct 22 ms 1996 KB Output is correct
40 Correct 5 ms 1996 KB Output is correct
41 Execution timed out 1519 ms 2104 KB Time limit exceeded
42 Execution timed out 1578 ms 1988 KB Time limit exceeded
43 Execution timed out 1586 ms 1868 KB Time limit exceeded
44 Execution timed out 1550 ms 2524 KB Time limit exceeded
45 Correct 453 ms 2424 KB Output is correct
46 Execution timed out 1593 ms 2760 KB Time limit exceeded
47 Execution timed out 1584 ms 2796 KB Time limit exceeded
48 Execution timed out 1585 ms 2792 KB Time limit exceeded
49 Execution timed out 1588 ms 2964 KB Time limit exceeded
50 Correct 67 ms 1868 KB Output is correct