/***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
marriage.cpp: In function 'int main()':
marriage.cpp:146:15: warning: unused variable 'CT' [-Wunused-variable]
146 | ll T = 1, CT = 0; //cin >> T;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1216 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1216 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1216 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1244 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1352 KB |
Output is correct |
25 |
Correct |
4 ms |
1748 KB |
Output is correct |
26 |
Incorrect |
2 ms |
1364 KB |
Output isn't correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Incorrect |
2 ms |
1620 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
1492 KB |
Output isn't correct |
31 |
Correct |
19 ms |
2848 KB |
Output is correct |
32 |
Incorrect |
2 ms |
1484 KB |
Output isn't correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
11 ms |
1416 KB |
Output is correct |
35 |
Correct |
27 ms |
4172 KB |
Output is correct |
36 |
Correct |
23 ms |
3924 KB |
Output is correct |
37 |
Correct |
64 ms |
3028 KB |
Output is correct |
38 |
Incorrect |
14 ms |
4564 KB |
Output isn't correct |
39 |
Correct |
254 ms |
1652 KB |
Output is correct |
40 |
Correct |
4 ms |
1748 KB |
Output is correct |
41 |
Incorrect |
11 ms |
2088 KB |
Output isn't correct |
42 |
Incorrect |
7 ms |
2252 KB |
Output isn't correct |
43 |
Incorrect |
10 ms |
2892 KB |
Output isn't correct |
44 |
Incorrect |
24 ms |
4160 KB |
Output isn't correct |
45 |
Incorrect |
11 ms |
2644 KB |
Output isn't correct |
46 |
Incorrect |
29 ms |
4656 KB |
Output isn't correct |
47 |
Incorrect |
21 ms |
4544 KB |
Output isn't correct |
48 |
Incorrect |
20 ms |
4564 KB |
Output isn't correct |
49 |
Incorrect |
32 ms |
5016 KB |
Output isn't correct |
50 |
Correct |
356 ms |
1784 KB |
Output is correct |