QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#506575 | #9102. Zayin and Elements | WorldFinalEscaped | RE | 0ms | 4096kb | C++20 | 3.1kb | 2024-08-05 19:42:41 | 2024-08-05 19:42:42 |
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));
while (ntot) {
adj[ntot].clear();
ntot--;
}
scanf("%d", &n);
scanf("%d", &m);
ntot = n + m;
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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4096kb
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: -100
Runtime Error
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...