QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613550 | #9449. New School Term | ucup-team4479# | WA | 1ms | 3712kb | C++23 | 2.6kb | 2024-10-05 14:12:14 | 2024-10-05 14:14:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 5005, M = 1e6 + 5;
vector <int> E[N];
int ans[N], cnt[2], tmp[N], n, tmp_cnt[2], U[N], V[N];
bool vis[N];
bool check(int u) {
tmp[u] ^= 1;
tmp_cnt[tmp[u]]++;
tmp_cnt[tmp[u] ^ 1]--;
vis[u] = 1;
bool res = true;
for (int v : E[u]) {
if (tmp[u] ^ tmp[v]) continue;
if (vis[v]) return false;
res &= check(v);
}
return res;
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cout.tie(0);
int m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> U[i] >> V[i];
}
memset(ans, -1, sizeof(ans));
for (int i = m; i >= 1; --i) {
int u = U[i], v = V[i];
if (ans[u] == -1 && ans[v] == -1) {
cnt[0]++, cnt[1]++;
ans[u] = 0, ans[v] = 1;
if (cnt[0] == n || cnt[1] == n) break;
E[u].push_back(v);
E[v].push_back(u);
} else if (ans[u] != -1 && ans[v] != -1) {
if (ans[u] ^ ans[v]) {
E[u].push_back(v);
E[v].push_back(u);
continue;
}
memcpy(tmp, ans, sizeof(tmp));
memset(vis, 0, sizeof(vis));
tmp_cnt[0] = cnt[0], tmp_cnt[1] = cnt[1];
vis[v] = 1;
if (check(u) && tmp_cnt[0] <= n && tmp_cnt[1] <= n) {
memcpy(ans, tmp, sizeof(ans));
E[u].push_back(v);
E[v].push_back(u);
} else {
memcpy(tmp, ans, sizeof(tmp));
memset(vis, 0, sizeof(vis));
tmp_cnt[0] = cnt[0], tmp_cnt[1] = cnt[1];
vis[u] = 1;
if (check(v) && tmp_cnt[0] <= n && tmp_cnt[1] <= n) {
memcpy(ans, tmp, sizeof(ans));
E[u].push_back(v);
E[v].push_back(u);
}
}
} else {
if (ans[u] == -1) swap(u, v);
ans[v] = ans[u] ^ 1;
cnt[ans[v]]++;
if (cnt[0] == n || cnt[1] == n) break;
E[u].push_back(v);
E[v].push_back(u);
}
// for (int j = 1; j <= 2 * n; ++j) cout << ans[j];
// cout << endl;
}
for (int i = 1; i <= 2 * n; ++i) {
if (ans[i] != -1) {
cout << ans[i];
continue;
}
if (cnt[0] < n) ans[i] = 0, cnt[0]--;
else ans[i] = 1, cnt[1]--;
cout << ans[i];
}
return 0;
}
/*
3 7
2 5
1 3
4 6
2 6
4 5
4 2
5 6
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3612kb
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: 3596kb
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: 0ms
memory: 3712kb
input:
1 0
output:
00
result:
wrong answer The number of 0s must be equal to that of 1s.