QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#614252 | #9449. New School Term | ucup-team3931# | WA | 1ms | 6144kb | C++14 | 2.3kb | 2024-10-05 16:01:04 | 2024-10-05 16:01:07 |
Judging History
answer
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;
const int maxn = 10050;
const int maxm = 1000100;
int n, m, fa[maxn << 1], c[maxn], b[maxn], d[maxn];
pii a[maxm];
vector<pii> G[maxn];
bitset<maxn> f;
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void merge(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
fa[x] = y;
}
}
int c0, c1;
void dfs(int u) {
c0 += (c[u] == 0);
c1 += (c[u] == 1);
for (pii p : G[u]) {
int v = p.fst, d = p.scd;
if (c[v] == -1) {
c[v] = c[u] ^ d;
dfs(v);
}
}
}
void solve() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n * 4; ++i) {
fa[i] = i;
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &a[i].fst, &a[i].scd);
}
for (int i = m; i; --i) {
int u = a[i].fst, v = a[i].scd;
if (find(u) == find(v) || find(u) == find(v + n * 2)) {
continue;
}
G[u].pb(v, 1);
G[v].pb(u, 1);
mems(c, -1);
int t = 0, tot = 0;
for (int j = 1; j <= n * 2; ++j) {
if (c[j] == -1) {
c[j] = c0 = c1 = 0;
dfs(j);
b[++tot] = max(c0, c1) - min(c0, c1);
}
}
mems(d, 0);
for (int j = 1; j <= tot; ++j) {
++d[b[j]];
t += b[j];
}
tot = 0;
for (int j = 1; j <= n * 2; ++j) {
if (!d[j]) {
continue;
}
int x = d[j];
for (int k = 1; k <= x; k <<= 1) {
x -= k;
b[++tot] = k * j;
}
if (x) {
b[++tot] = x * j;
}
}
f.reset();
f.set(0);
for (int j = 1; j <= tot; ++j) {
f |= (f << b[j]);
}
if (f.test(t / 2)) {
merge(u, v + n * 2);
merge(u + n * 2, v);
} else {
merge(u, v);
merge(u + n * 2, v + n * 2);
G[u].erase(find(G[u].begin(), G[u].end(), mkp(v, 1)));
G[v].erase(find(G[v].begin(), G[v].end(), mkp(u, 1)));
G[u].pb(v, 0);
G[v].pb(u, 0);
}
}
mems(c, -1);
for (int i = 1; i <= n * 2; ++i) {
if (c[i] == -1) {
c[i] = 0;
dfs(i);
}
}
for (int i = 1; i <= n * 2; ++i) {
putchar('0' + c[i]);
}
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4112kb
input:
2 4 1 3 2 4 1 4 1 2
output:
0101
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 0ms
memory: 3968kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
001101
result:
ok Output is valid. OK
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 6144kb
input:
1 0
output:
00
result:
wrong answer The number of 0s must be equal to that of 1s.