QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#506578 | #9102. Zayin and Elements | WorldFinalEscaped | AC ✓ | 1ms | 3820kb | C++20 | 3.2kb | 2024-08-05 19:43:55 | 2024-08-05 19:43:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define link lnk
const int N = 505;
vector<int> adj[N];
int n, m, ntot;
void addedge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
int link[N], mate[N], bel[N], vis[N], cur;
int q[N], type[N], t;
int getLCA(int u, int v) {
for (++cur; ; swap(u, v)) {
if (u) {
if (vis[u] == cur) return u;
vis[u] = cur, u = bel[link[mate[u]]];
}
}
}
void upward(int u, int v, int target) {
while (bel[u] != target) {
link[u] = v;
if (type[mate[u]] == 1)
q[++t] = mate[u], type[mate[u]] = 0;
if (bel[u] == u)
bel[u] = target;
if (bel[mate[u]] == mate[u])
bel[mate[u]] = target;
v = mate[u], u = link[v];
}
}
bool match(int ver) {
q[t = 1] = ver;
for (int u = 1; u <= ntot; u++)
bel[u] = u, type[u] = -1;
type[ver] = 0;
for (int i = 1; i <= t; i++) {
int u = q[i];
for (auto v: adj[u]) {
if (!~type[v]) {
link[v] = u, type[v] = 1;
int nu = mate[v];
if (!nu) {
while (v) {
int nv = mate[link[v]];
mate[v] = link[v], mate[link[v]] = v;
v = nv;
}
return true;
}
q[++t] = nu, type[nu] = 0;
} else if (!type[v] && bel[u] != bel[v]) {
int lca = getLCA(u, v);
upward(u, v, lca), upward(v, u, lca);
for (int u = 1; u <= ntot; u++)
bel[u] = bel[bel[u]];
}
}
}
return false;
}
void sc() {
memset(link, 0, sizeof(link));
memset(mate, 0, sizeof(mate));
memset(bel, 0, sizeof(bel));
memset(q, 0, sizeof(q));
memset(type, 0, sizeof(type));
while (ntot) {
adj[ntot].clear();
ntot--;
}
scanf("%d", &n);
scanf("%d", &m);
ntot = n;
for (int i = 1; i <= m; i++) {
int a, b, c; scanf("%d%d%d", &a, &b, &c);
int len; scanf("%d", &len);
vector<int> to;
for (int j = 0; j < len; j++) {
int x; scanf("%d", &x);
to.push_back(x);
}
while (a--) {
++ntot;
for (auto it: to) addedge(ntot, it);
}
b *= 2;
while (b--) {
++ntot;
if (b % 2 == 0) addedge(ntot - 1, ntot);
for (auto it: to) addedge(ntot, it);
}
while (c--) {
++ntot, ++ntot;
addedge(ntot - 1, ntot);
for (auto it: to) addedge(ntot, it);
}
}
int ans = 0;
for (int i = 1; i <= ntot; i++) {
if (mate[i]) continue;
if (match(i)) {
ans++;
} else if (i <= n) {
puts("-1");
return ;
}
}
printf("%d\n", ans - n);
}
int main() {
int T; scanf("%d", &T);
while (T--) sc();
return 0;
}
/*
2
5
2
2 3 1 2 3 1
1 1 1 3 4 5 2
5
2
2 3 1 1 3
1 0 1 2 1 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
2 5 2 2 3 1 2 3 1 1 1 1 3 4 5 2 5 2 2 3 1 1 3 1 0 1 2 1 2
output:
5 -1
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
5 9 10 0 0 10 2 3 8 0 0 10 2 4 6 0 8 2 2 2 4 0 0 10 3 1 3 8 0 4 6 2 2 3 0 8 2 3 2 6 7 0 9 1 2 1 7 0 2 8 3 1 4 9 0 7 3 1 5 0 0 10 3 5 6 9 3 10 0 9 1 1 2 0 5 5 1 1 0 1 9 1 1 0 7 3 1 1 0 7 3 0 0 0 10 0 0 6 4 1 3 0 9 1 0 0 7 3 0 0 9 1 1 2 90 20 0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89 0 1 9 12 3 8 21 3...
output:
94 97 155 151 152
result:
ok 5 lines