# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
95474 | choikiwon | Marriage questions (IZhO14_marriage) | C++17 | 788 ms | 4728 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 100010;
int N, M, K;
vector<int> adj[MN];
int vis[MN], who[MN], timer;
vector<int> rem;
bool dfs(int u, int l, int r) {
if(vis[u] == timer) return false;
vis[u] = timer;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if(v < l || r < v) continue;
if(who[v] == -1 || dfs(who[v], l, r)) {
who[v] = u;
return true;
}
}
return false;
}
bool check(int l, int r) {
while(rem.size()) {
int t = rem.back(); rem.pop_back();
timer++;
if(!dfs(t, l, r)) {
rem.push_back(t);
return false;
}
}
return true;
}
int main() {
scanf("%d %d %d", &N, &M, &K);
for(int i = 0; i < K; i++) {
int a, b; scanf("%d %d", &a, &b);
a--; b--;
adj[b].push_back(a);
}
memset(who, -1, sizeof(who));
for(int i = 0; i < M; i++) rem.push_back(i);
ll ans = 0;
int j = 0;
for(int i = 0; i < N; i++) {
while(j <= N && !check(i, j - 1)) j++;
if(rem.size()) break;
ans += N - j + 1;
if(who[i] != -1) {
rem.push_back(who[i]);
}
}
printf("%lld", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |