# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1088278 | Kerim | Marriage questions (IZhO14_marriage) | C++17 | 1600 ms | 6736 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;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e5+5;
int ed[N], st[N], n, m, k, love[N];
bool vis[N], vis1[N], mark[N];
vector<int> adj[N], g[N];
bool check(vector<int> &v){
int cnt = 0;
for(auto &i : v)
cnt += !mark[i], mark[i] = 1;
for(auto &i : v)
mark[i] = 0;
return (cnt == m);
}
bool kuhn(int x){
if(vis[x]) return 0;
vis[x] = 1;
for(auto i : adj[x])
if(love[i] == -1 or kuhn(love[i])){
love[i] = x;
return 1;
}
return 0;
}
int main(){
// freopen("file.in", "r", stdin);
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= k; ++i){
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
}
int answer = 0;
for(int l = 1; l <= n; ++l){
vector<int> v;
for(int r = l; r <= n; ++r){
for(auto &i : g[r])
v.pb(i);
if(!check(v))
continue;
for(auto &i : g[r])
adj[r].pb(i);
for (int j = 1; j <= m; j++)
love[j] = -1;
// Improved Kuhn starts here
for (int j = l; j <= r; j++)
vis1[j] = 0;
for (int j = l; j <= r; j++)
for (int to : adj[j])
if (love[to] == -1) {
love[to] = j;
vis1[j] = true;
break;
}
for (int i = l; i <= r; i++){
if (vis1[i])
continue;
// Improved Kuhn ends here
for (int j = l; j <= r; j++)
vis[j] = 0;
kuhn(i);
}
bool ok = 1;
for (int i = 1; i <= m and ok; i++)
ok &= (love[i] != -1);
answer += ok;
}
for (int i = 1; i <= n; i++)
adj[i].clear();
}
printf("%d\n", answer);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |