QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#626587#9449. New School TermtovarischWA 0ms3864kbC++233.6kb2024-10-10 10:24:442024-10-10 10:24:45

Judging History

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

  • [2024-10-10 10:24:45]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3864kb
  • [2024-10-10 10:24:44]
  • 提交

answer

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


const int maxn = 10010, rootn = 105;

struct DSU {
    int n;
    vector<int> f, ones;
    vector<vector<int>> x;

    DSU (int n_) : n(n_) {
        f.resize(n);
        ones.resize(n);
        x.assign(n, vector<int>());

        for (int i=0; i<n; ++i) {
            f[i] = i;
            ones[i] = i&1;
            x[i].push_back(i);
        }

    }

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

        if (a == b) return;

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

        f[b] = a;
        ones[a] += ones[b];

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

vector<int>  comp, other;

int col[maxn];
int from[maxn], to[maxn];

bitset<maxn> dp, dp2, affected;

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<m; ++i) {
        cin >> from[i] >> to[i];
        from[i]--;
        to[i]--;
    }

    DSU dsu(2*n), dsu2(2*n);
    for (int i=0; i<2*n; ++i) col[i] = i&1;

    auto solve = [&](bitset<maxn>& bit, DSU& ds, vector<int>& a) -> void {
        bit.reset();
        bit[0]=1;
        int s = 0;

        for (int k : a) {
            bit = (bit << (ds.ones[k])) | (bit << (ds.x[k].size() - ds.ones[k]));
            s += ds.x[k].size();
        }

        bit[0] = bit[s];
    };

    for (int i=m-1, j=m-1; i>=0; --i) {
        if (i == j) { // new block
            int added = 0;
            affected.reset();

            while (j>=0 && added<rootn) {
                int u = from[j], v = to[j];
                if (dsu2.find(u) != dsu2.find(v)) {
                    affected[u] = affected[v] = 1;
                    added++;
                    dsu2.merge(u, v);
                }
                j--;
            }

            other.clear();
            for (int k=0; k<2*n; ++k) if (dsu2.find(k) == k && !affected[k] ) other.push_back(k);
            solve(dp2, dsu2, other);
        }

        int u=from[i], v = to[i];
        if (dsu.find(u) == dsu.find(v)) continue;
        if (col[u] != col[v]) {
            dsu.merge(u, v);
            continue;
        }

        comp.clear();
        for (int k=0; k<2*n; ++k) if (dsu.find(k) == k && affected[k]) {
            if (dsu.find(k) != u && dsu.find(k) != v) comp.push_back(k);
        }

        solve(dp, dsu, comp);

        int flipu = (dsu.x[u].size()-dsu.ones[u]) + dsu.ones[v];
        int flipv = (dsu.x[v].size()-dsu.ones[v]) + dsu.ones[u];
        bool oku = 0, okv = 0;

        if (other.empty()) {
            oku |= n - flipu >= 0 && dp[n - flipu];
            okv |= n - flipv >= 0 && dp[n - flipv];
        } else {
            for (int k=0; k<=n; ++k) if (dp[k]) {
                oku |= n - flipu - k >= 0 && dp2[n - flipu - k];
                okv |= n - flipv - k >= 0 && dp2[n - flipv - k];
            }
        }

        int c = -1;
        if (oku) c = u;
        else if (okv) c = v;

        if (c != -1) {
            for(int& w : dsu.x[c]) {
                dsu.ones[c] -= col[w];
                col[w] ^= 1;
                dsu.ones[c] += col[w];
            }
        }

        dsu.merge(u, v);
    }

    for(int i=0; i<2*n; ++i) cout << col[i];
    cout << endl;
    return 0;
}

详细

Test #1:

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

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

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

100101

result:

ok Output is valid. OK

Test #3:

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

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

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

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

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

input:

2 3
2 4
3 4
1 2

output:

0101

result:

wrong answer The division is not minimized.