#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; struct BipartiteMatching { vector< vector< int > > graph; vector< int > match, alive, used; int timestamp; BipartiteMatching(int n) : graph(n), alive(n, 1), used(n, 0), match(n, -1), timestamp(0) {} void add_edge(int u, int v) { graph[u].push_back(v); graph[v].push_back(u); } bool dfs(int idx) { used[idx] = timestamp; for(auto &to : graph[idx]) { int to_match = match[to]; if(alive[to] == 0) continue; if(to_match == -1 || (used[to_match] != timestamp && dfs(to_match))) { match[idx] = to; match[to] = idx; return true; } } return false; } int bipartite_matching() { int ret = 0; for(int i = 0; i < graph.size(); i++) { if(alive[i] == 0) continue; if(match[i] == -1) { ++timestamp; ret += dfs(i); } } return ret; } void output() { for(int i = 0; i < graph.size(); i++) { if(i < match[i]) { cout << i << "-" << match[i] << endl; } } } }; vector

v[500050]; int main() { int h, w; cin>>h>>w; for(int i=0; i>a; v[a].push_back({i, j}); } } int ans=0; for(int i=1; i<=500000; i++){ if(v[i].empty()) continue; map mp; for(auto p:v[i]){ mp[p.first]=0; mp[p.second+h]=0; } int c=0; for(auto &p:mp){ p.second=c++; } BipartiteMatching g(mp.size()); for(auto p:v[i]){ g.add_edge(mp[p.first], mp[p.second+h]); } ans+=g.bipartite_matching(); } cout<