QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#628602#9449. New School TermtovarischWA 1ms3784kbC++233.2kb2024-10-10 21:09:512024-10-10 21:09:56

Judging History

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

  • [2024-10-10 21:09:56]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3784kb
  • [2024-10-10 21:09:51]
  • 提交

answer

#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;


const int maxn = 10005, rootn = 105;
struct DSU {
    int n;
    vector<int> f;
    vector<int> ones, zeros;
    vector<vector<int>> comp;

    DSU (int n_): n(n_) {
        f.resize(n);
        ones.resize(n);
        comp.resize(n);
        zeros.resize(n);

        for (int i=0; i<n; ++i) {
            f[i] = i;
            ones[i] = 0;
            zeros[i] = 1;
            comp[i].pb(i);
        }
    }

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

        if (size(a) < size(b)) swap(a, b);

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

        while (comp[b].size()) {
            comp[a].pb(comp[b].back());
            comp[b].pop_back();
        }
    }
};
int main(){
	#ifdef LOCAL
		freopen("in","r", stdin);
		freopen("out","w",stdout);
	#endif
	ios_base::sync_with_stdio(0); cin.tie(0);

    int n, m; cin >> n >> m;
    
    vector<int> from(m), to(m), col(2*n, 0);
    DSU dsu(2*n);

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

    set<int> comp;
    vector<pair<int, int>> process;

    auto go = [&](bitset<maxn>& dp, set<int>& a, vector<int>&& ig) -> void {
        dp[0] = 1;
        for(int u : a) {
            if(ig[0]==u || ig[1]==u) continue;

            dp = (dp<<(dsu.ones[u])) | (dp<<(dsu.zeros[u]));
        }
    };
    auto solve = [&]() -> void {
        set<int> other;
        for(int i=0; i<2*n; ++i) {
            if(!comp.count(dsu.find(i))) other.insert(dsu.find(i));
        }

        bitset<maxn> dpc, dpo;
        go(dpo, other, {-1, -1});

        for(pair<int, int>& p : process) {
            int u = dsu.find(p.ff), v = dsu.find(p.ss);
            if (u == v) continue;

            dpc = dpo;
            go(dpc, comp, {u, v});

            if (col[p.ff] == col[p.ss]) {
                int cnt = dsu.ones[u] + dsu.zeros[v];
                if (n - cnt >=0 && dpc[n - cnt]) {
                    for(int& w : dsu.comp[v]) col[w] ^=1;
                    swap(dsu.ones[u], dsu.zeros[v]);
                }
            } else {
                int cnt = dsu.ones[u] + dsu.ones[v];
                if (n - cnt < 0 || !dpc[n - cnt]) {
                    for(int& w : dsu.comp[v]) col[w] ^=1;
                    swap(dsu.ones[u], dsu.zeros[v]);
                }
            }

            dsu.merge(u, v);
            if (dsu.find(u) != u) comp.erase(u);
            if (dsu.find(v) != v) comp.erase(v);
        }

        comp.clear();
        process.clear();
    };

    for (int i=m-1; i>=0; --i) {
        process.pb({from[i], to[i]});
        comp.insert(from[i]);
        comp.insert(to[i]);

        if(comp.size()>=rootn) solve();
    }
    solve();

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3784kb

input:

2 4
1 3
2 4
1 4
1 2

output:

0111

result:

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