QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#618701#9449. New School TermtovarischWA 0ms3672kbC++232.1kb2024-10-07 07:32:242024-10-07 07:32:25

Judging History

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

  • [2024-10-07 07:32:25]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3672kb
  • [2024-10-07 07:32:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;


const int maxn = 10010;

int f[maxn];
vector<int> x[maxn];

int col[maxn], cnt[2];
int from[maxn], to[maxn];
vector<pair<int, int>> graph[maxn];


int find(int n) { return f[n] = f[n] == n ? n : find(f[n]); }
void merge(int a, int b, int c) {
    a=find(a), b=find(b);

    if (a == b) return;

    if (x[a].size()<x[b].size()) swap(a, b);

    f[b] = a;

    while (x[b].size()) {
        if (c) {
            cnt[col[x[b].back()]]--;
            col[x[b].back()] ^= 1;
            cnt[col[x[b].back()]]++;
        }

        x[a].push_back(x[b].back());
        x[b].pop_back();
    }
}


int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // LOCAL

    int n, m; cin >> n >> m;
    for (int i=0; i<2*n; ++i) {
        f[i] = i;
        x[i].push_back(i);
    }

    for (int i=0; i<m; ++i) {
        cin >> from[i] >> to[i];
        from[i]--;
        to[i]--;

        graph[from[i]].push_back({to[i], i});
        graph[to[i]].push_back({from[i], i});
    }

    for (int i=0; i<2*n; ++i) col[i] = -1;

    for (int i = m-1; i>=0; --i) {
        int u = from[i], v = to[i];

        if (find(u) == find(v)) continue;

        int c = 0;

        if (col[u]!=-1 && col[v]!=-1) c=col[u]==col[v];
        else if (col[u]!=-1) {
            if (cnt[1 - col[u]]<n) col[v] = 1 - col[u];
            else col[v] = col[u];
            cnt[col[v]]++;
        } else if (col[v]!=-1) {
            if (cnt[1 - col[v]]<n) col[u] = 1 - col[v];
            else col[u] = col[v];
            cnt[col[u]]++;     
        } else {
            col[u]=0, col[v]=1;
            cnt[col[u]]++;
            cnt[col[v]]++;
        }

        merge(u, v, c);
    }

    for (int i=0; i<2*n; ++i) {
        if (col[i] == -1)  {
            col[i] = cnt[0]<n ? 0 : 1;
            cnt[col[i]]++;
        }

        cout << col[i];
    }
    cout << endl;

    assert(cnt[0] == n && cnt[1] == n);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3596kb

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: 0
Accepted
time: 0ms
memory: 3648kb

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

score: 0
Accepted
time: 0ms
memory: 3672kb

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 3664kb

input:

3 8
4 6
3 5
1 4
2 4
1 6
1 2
3 4
4 5

output:

011010

result:

wrong answer The division is not minimized.