# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393011 | patrikpavic2 | Marriage questions (IZhO14_marriage) | C++17 | 1047 ms | 3892 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 <cstdio>
#include <cstring>
#include <vector>
#define PB push_back
using namespace std;
typedef long long ll;
const int N = (1 << 15);
int par[N], n, m, bio[N], k, un[N], cookie = 1;
vector < int > v[N];
int jos[N], sz;
bool dodaj(int x){
if(bio[x] == cookie) return 0;
bio[x] = cookie;
for(int y : v[x]){
if(un[y] && (par[y] == -1 || dodaj(par[y]))){
par[x] = y; par[y] = x;
return 1;
}
}
return 0;
}
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--;
v[a].PB(n + b), v[b + n].PB(a);
}
memset(par, -1, sizeof(par));
for(int i = 0;i < m;i++) un[i + n] = 1, jos[i] = i + n;
sz = m;
int r = 0;
ll ans = 0;
for(int l = 0;l < n;l++){
if(l){
un[l - 1] = 0;
if(par[l - 1] != -1){
par[par[l - 1]] = -1;
dodaj(par[l - 1]); cookie++;
if(par[par[l - 1]] == -1)
jos[sz++] = par[l - 1];
}
}
while(r < n && sz > 0){
un[r] = 1; cookie++;
while(sz > 0 && dodaj(jos[sz - 1]))
sz--, cookie++;
r++;
}
ans += (n + 1 - r) * (sz == 0);
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |