# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
154490 | srvlt | Marriage questions (IZhO14_marriage) | C++14 | 1570 ms | 7476 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.
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 32007;
int n, m, k, mt[N], used[N], f[N], timer;
vector <int> q[N], w[N];
bool rem[N];
bool dfs(int v) {
if (used[v] == timer) {
return false;
}
used[v] = timer;
for (auto i : q[v]) {
if (rem[i]) {
continue;
}
if (mt[i] == -1) {
mt[i] = v;
f[v] = i;
return true;
}
}
for (auto i : q[v]) {
if (rem[i]) {
continue;
}
if (dfs(mt[i])) {
mt[i] = v;
f[v] = i;
return true;
}
}
return false;
}
bool kuhn() {
for (int i = 0; i <= n + m; i++) {
timer = 0;
used[i] = 0;
mt[i] = f[i] = -1;
}
for (int i = 1; i <= m; i++) {
timer++;
if (f[i + n] == -1) {
dfs(i + n);
}
}
for (int i = 1; i <= m; i++) {
if (f[i + n] == -1) {
return false;
}
}
return true;
}
bool ok() {
for (int i = 1; i <= m; i++) {
if (f[i + n] == -1) {
return false;
}
}
return true;
}
void del2(int x) {
rem[x] = true;
q[x] = {};
}
void add2(int x) {
for (auto i : w[x]) {
q[x].pb(i);
q[i].pb(x);
}
}
void del(int x) {
int tmp = mt[x];
if (tmp != -1) {
f[tmp] = -1;
mt[x] = -1;
}
rem[x] = true;
timer++;
dfs(tmp);
q[x] = {};
}
void add(int x) {
for (auto i : w[x]) {
q[x].pb(i);
q[i].pb(x);
}
for (auto i : q[x]) {
if (f[i] == -1) {
timer++;
dfs(i);
}
}
}
void solve2() {
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
w[x].pb(y + n);
w[y + n].pb(x);
}
int j = 1, res = 0;
for (int i = 1; i <= n; i++) {
while (j <= n) {
add(j);
if (kuhn()) {
break;
}
j++;
}
res += (n - j + 1);
del(i);
}
cout << res;
}
void solve() {
for (int i = 0; i < N; i++) {
f[i] = mt[i] = -1;
}
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
w[x].pb(y + n);
w[y + n].pb(x);
}
int j = 1, res = 0;
for (int i = 1; i <= n; i++) {
while (j <= n) {
add(j);
if (ok()) {
break;
}
j++;
}
res += (n - j + 1);
del(i);
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m >> k;
if (n <= 1000 && m <= 500) {
solve2();
} else {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |