update ucpc2026

This commit is contained in:
2026-07-11 09:38:32 +09:00
parent 7176febe54
commit 0b8c2864ae
20 changed files with 329 additions and 163 deletions
+17 -23
View File
@@ -1,18 +1,15 @@
// From https://github.com/koosaga/olympiad
// matching_short.cpp
const int MAXN = 2020 + 1;
// 1-based Vertex index
int vis[MAXN], par[MAXN], orig[MAXN], match[MAXN], aux[MAXN], t, N;
vector<int> conn[MAXN];
queue<int> Q;
// Originate from https://github.com/koosaga/olympiad
// matching_short.cpp / 1-based Vertex index / const int N = 2020 + 1;
int vis[N], par[N], orig[N], match[N], aux[N], t, n;
vector<int> conn[N]; queue<int> Q;
void addEdge(int u, int v) { conn[u].push_back(v); conn[v].push_back(u); }
void init(int n) {
N = n; t = 0;
for(int i=0; i<=n; ++i) {
conn[i].clear();
match[i] = aux[i] = par[i] = 0;
}
::n = n; t = 0;
fors(i, 0, n) conn[i].clear();
fors(i, 0, n) match[i] = aux[i] = par[i] = 0;
}
void augment(int u, int v) {
int pv = v, nv;
do {
@@ -35,12 +32,11 @@ void blossom(int v, int w, int a) {
while(orig[v] != a) {
par[v] = w; w = match[v];
if(vis[w] == 1) Q.push(w), vis[w] = 0;
orig[v] = orig[w] = a;
v = par[w];
orig[v] = orig[w] = a; v = par[w];
}
}
bool bfs(int u) {
fill(vis+1, vis+1+N, -1); iota(orig + 1, orig + N + 1, 1);
fill(vis+1, vis+1+n, -1); iota(orig + 1, orig + n + 1, 1);
Q = queue<int> (); Q.push(u); vis[u] = 0;
while(!Q.empty()) {
int v = Q.front(); Q.pop();
@@ -58,17 +54,15 @@ bool bfs(int u) {
}
return false;
}
int Match() {
int matching() {
int ans = 0;
// find random matching (not necessary, constant improvement)
vector<int> V(N-1); iota(V.begin(), V.end(), 1);
vector<int> V(n-1); iota(V.begin(), V.end(), 1);
shuffle(V.begin(), V.end(), mt19937(0x94949));
for(auto x: V) if(!match[x]){
for(auto y: conn[x]) if(!match[y]) {
match[x] = y, match[y] = x;
++ans; break;
}
for(auto x: V) if(!match[x]) for(auto y: conn[x]) if(!match[y]) {
match[x] = y, match[y] = x;
++ans; break;
}
for(int i=1; i<=N; ++i) if(!match[i] && bfs(i)) ++ans;
forr(i, n) if(!match[i] && bfs(i)) ++ans;
return ans;
}