Submission #345386

#TimeUsernameProblemLanguageResultExecution timeMemory
345386vonatlusMarriage questions (IZhO14_marriage)C++17
44 / 100
1584 ms17072 KiB
/// wa 
 
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
 
#include<bits/stdc++.h>
 
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
 
using namespace std;
 
using ll = long long;
using pii = pair<int, int>;                                   
 
const int MOD = 1e9 + 7; 
const int INF = 1e9 + 1e2;
  
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
void fin() {
#ifdef AM
    freopen(".in", "r", stdin);
#endif        
}                   
 
const bool flag = 0;
 
const int N = 3e5+10;
 
int was[N];
 
int mt[N], p[N], tmp;
 
vector<int> adj[N], g[N];
                     
bool dfs(int x) {
    if (was[x] == tmp) return 0;
    was[x] = tmp;
    for (int to : adj[x]) {
        if (mt[to] == -1) {
            mt[to] = x;
            p[x] = to;
            return 1;
        }
    }
    for (int to : adj[x]) {
        if (dfs(mt[to])) {
            mt[to] = x;
            p[x] = to;
            return 1;
        }
    }
    return 0;
}
 
void ma1n() {
    int n, m, k;
    cin >> n >> m >> k;
    for (int u, v; k--;) {
        cin >> u >> v;
        u--, v--;
        g[u].pb(v);
    }
    int r = 0;
    ll ans = 0;
    memset(mt, -1, sizeof(mt));
    int prev = -1;
    for (int l = 0; l < n; ++l) {
        bool ok = 0;
        while (r < n && !ok) {
            for (int to : g[r]) {
                adj[to].pb(r);
            }
            fill(p, p+m, -1);
            for (int i = 0; i < m; ++i) {
                tmp++;
                dfs(i);
            }
            ok = 1;
            for (int i = 0; i < m; ++i) {
                if (~p[i]) {
                    mt[p[i]] = -1;
                } else {          
                    ok = 0;
                }
            }
            r++;
        }        
        for (int to : g[l]) {
            adj[to].erase(find(all(adj[to]), l));
        }
        if (ok) {
            ans += n-r+1;
        }
    }
    cout << ans;
} 
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr), fin();
    int ts = 1;
    if (flag) {
        cin >> ts;
    }
    while (ts--) {
        ma1n(); 
    }
    return 0;
}

Compilation message (stderr)

marriage.cpp: In function 'void ma1n()':
marriage.cpp:71:9: warning: unused variable 'prev' [-Wunused-variable]
   71 |     int prev = -1;
      |         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...