QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#613576#9449. New School Termucup-team4479#WA 1ms7720kbC++232.7kb2024-10-05 14:15:522024-10-05 14:17:19

Judging History

你现在查看的是最新测评结果

  • [2024-10-05 14:17:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7720kb
  • [2024-10-05 14:15:52]
  • 提交

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[M], V[M];
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(int) * (2 * n + 1));
            memset(vis, 0, sizeof(int) * (2 * n + 1));
            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(int) * (2 * n + 1));
                E[u].push_back(v);
                E[v].push_back(u);
            } else {
                memcpy(tmp, ans, sizeof(int) * (2 * n + 1));
                memset(vis, 0, sizeof(int) * (2 * n + 1));
                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(int) * (2 * n + 1));
                    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: 5720kb

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: 7720kb

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: 3668kb

input:

1 0

output:

00

result:

wrong answer The number of 0s must be equal to that of 1s.