Submission #334734

# Submission time Handle Problem Language Result Execution time Memory
334734 2020-12-09T23:05:20 Z limabeans Marriage questions (IZhO14_marriage) C++17
100 / 100
829 ms 25580 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;


int n,m,k;
vector<int> g[maxn];



int match[maxn]; //husbands
int seen[maxn]; //daughters
queue<int> need;
int L, R;
int cloc = 0;




bool dfs(int at) {
    if (seen[at]==cloc) return false;
    seen[at]=cloc;
    for (int to: g[at]) {
	if (L<=to && to<=R) {
	    if (match[to]==-1 || dfs(match[to])) {
		match[to]=at;
		return true;
	    }
	}
    }
    return false;
}



int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>m>>k;
    for (int i=0; i<k; i++) {
	int u,v;
	cin>>u>>v;
	--u; --v;
	g[v].push_back(u);
    }


    for (int i=0; i<n; i++) {
	match[i]=-1;	
    }

    for (int i=0; i<m; i++) {
	need.push(i);
    }

    ll ans = 0;

    for (int l=0,r=0; l<n; l++) {
	while (r<n) {
	    L=l; R=r;
	    while (need.size()) {
		++cloc;
		int at = need.front();
		if (dfs(at)) {
		    need.pop();
		} else {
		    break;
		}
	    }
	    if (need.size()) {
		++r;
	    } else {
		break;
	    }
	}

	if (need.size()) {
	    break;
	}

	ans += n-r;

	// get rid of husband l
	if (match[l]>=0) {
	    need.push(match[l]);
	    match[l]=-1;
	}
	
    }

    cout<<ans<<endl;    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 16 ms 23788 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 16 ms 23788 KB Output is correct
6 Correct 16 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 16 ms 23788 KB Output is correct
9 Correct 18 ms 23936 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
11 Correct 16 ms 23788 KB Output is correct
12 Correct 16 ms 23788 KB Output is correct
13 Correct 19 ms 23788 KB Output is correct
14 Correct 16 ms 23788 KB Output is correct
15 Correct 19 ms 23788 KB Output is correct
16 Correct 16 ms 23788 KB Output is correct
17 Correct 17 ms 23788 KB Output is correct
18 Correct 16 ms 23788 KB Output is correct
19 Correct 18 ms 23916 KB Output is correct
20 Correct 17 ms 23788 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23788 KB Output is correct
23 Correct 17 ms 23916 KB Output is correct
24 Correct 17 ms 23788 KB Output is correct
25 Correct 54 ms 24172 KB Output is correct
26 Correct 20 ms 23916 KB Output is correct
27 Correct 16 ms 23824 KB Output is correct
28 Correct 17 ms 23788 KB Output is correct
29 Correct 18 ms 23916 KB Output is correct
30 Correct 18 ms 23916 KB Output is correct
31 Correct 297 ms 24556 KB Output is correct
32 Correct 18 ms 23916 KB Output is correct
33 Correct 17 ms 23788 KB Output is correct
34 Correct 20 ms 23916 KB Output is correct
35 Correct 125 ms 25196 KB Output is correct
36 Correct 95 ms 24940 KB Output is correct
37 Correct 829 ms 24812 KB Output is correct
38 Correct 34 ms 25196 KB Output is correct
39 Correct 108 ms 24044 KB Output is correct
40 Correct 19 ms 24044 KB Output is correct
41 Correct 21 ms 24300 KB Output is correct
42 Correct 23 ms 24300 KB Output is correct
43 Correct 33 ms 24664 KB Output is correct
44 Correct 34 ms 25324 KB Output is correct
45 Correct 25 ms 24556 KB Output is correct
46 Correct 39 ms 25452 KB Output is correct
47 Correct 39 ms 25452 KB Output is correct
48 Correct 36 ms 25452 KB Output is correct
49 Correct 43 ms 25580 KB Output is correct
50 Correct 213 ms 24172 KB Output is correct