/***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 <= n){
for(ll j = 1; j <= m; j++) vis[j] = 0;
if(kuhn(node, m + i + 1, m + k)){
f = 1;
break;
}
k++;
}
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;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is 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 |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 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 |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
2 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 |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
25 ms |
1620 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1492 KB |
Output is correct |
30 |
Correct |
2 ms |
1492 KB |
Output is correct |
31 |
Correct |
146 ms |
2456 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
1 ms |
1236 KB |
Output is correct |
34 |
Correct |
11 ms |
1400 KB |
Output is correct |
35 |
Correct |
57 ms |
3456 KB |
Output is correct |
36 |
Correct |
40 ms |
3392 KB |
Output is correct |
37 |
Correct |
502 ms |
2648 KB |
Output is correct |
38 |
Correct |
14 ms |
3796 KB |
Output is correct |
39 |
Correct |
259 ms |
1536 KB |
Output is correct |
40 |
Correct |
3 ms |
1620 KB |
Output is correct |
41 |
Correct |
8 ms |
1856 KB |
Output is correct |
42 |
Correct |
7 ms |
2076 KB |
Output is correct |
43 |
Correct |
9 ms |
2460 KB |
Output is correct |
44 |
Correct |
19 ms |
3304 KB |
Output is correct |
45 |
Correct |
12 ms |
2360 KB |
Output is correct |
46 |
Correct |
33 ms |
3844 KB |
Output is correct |
47 |
Correct |
22 ms |
3620 KB |
Output is correct |
48 |
Correct |
20 ms |
3592 KB |
Output is correct |
49 |
Correct |
35 ms |
4052 KB |
Output is correct |
50 |
Correct |
357 ms |
1620 KB |
Output is correct |