# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
813432 | farhan132 | Marriage questions (IZhO14_marriage) | C++17 | 356 ms | 5016 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.
/***Farhan132***/
// #pragma GCC optimize("Ofast,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clz(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
// const ll mod = (ll) 1e9 + 7;
const ll inf = numeric_limits<int>::max()-5;
const ll INF = (ll)2e18;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 32005;
ll mt[N], vis[N];
vector < ll > v[N];
bool kuhn(ll node, ll L, ll R){
if(vis[node]) return 0;
vis[node] = 1;
for(auto u : v[node]){
if(u < L || u > R) continue;
if(mt[u] == -1 || kuhn(mt[u], L, R)){
mt[u] = node;
return 1;
}
}
return 0;
}
ll T(ll x){
return (x * (x + 1)) / 2;
}
void solve(){
ll n, m, k; cin >> n >> m >> k;
for(ll i = 1; i <= k; i++){
ll x, y; cin >> x >> y;
v[x + m].pb(y);
v[y].pb(x + m);
}
ll l = 1, r = n;
k = n + 1;
while(l <= r){
ll mid = (l + r) / 2;
mem(mt, -1); bool f = 1;
for(ll i = 1; i <= m; i++){
for(ll j = 1; j <= m; j++) vis[j] = 0;
if(!kuhn(i, m + 1, m + mid)){
f = 0;
break;
}
}
if(f == 0){
l = mid + 1;
}else{
k = mid;
r = mid - 1;
}
}
if(k == n + 1){
cout << "0\n";
return;
}
mem(mt, -1);
for(ll i = 1; i <= m; i++){
for(ll j = 1; j <= m; j++) vis[j] = 0;
kuhn(i, m + 1, m + k);
}
ll ans = n + 1 - k;
for(ll i = 1; i < n; i++){
if(mt[i + m] == -1){
ans += n + 1 - k;
continue;
}
ll node = mt[i + m]; mt[i + m] = -1;
bool f = 0;
while(k + 1 <= n){
k++;
for(ll j = 1; j <= m; j++) vis[j] = 0;
if(kuhn(node, m + i + 1, m + k)){
f = 1;
break;
}
}
if(!f) break;
ans += n + 1 - k;
}
cout << ans << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
auto start_time = clock();
#else
// freopen("subsequence.in", "r", stdin);
// freopen("subsequence.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//precalc();
ll T = 1, CT = 0; //cin >> T;
while(T--){
// cout << "Case #" << ++CT << ": ";
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |