QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708719#9449. New School TermMher777WA 2ms9900kbC++207.8kb2024-11-04 04:31:302024-11-04 04:31:31

Judging History

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

  • [2024-11-04 04:31:31]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9900kb
  • [2024-11-04 04:31:30]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(time(nullptr));

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);

/* -------------------- Constants -------------------- */

const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);

/* -------------------- Functions -------------------- */

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

void precision(int x) {
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(x);
}

ll gcd(ll a, ll b) {
    if (a == 0 || b == 0) return(max(a, b));
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

ll range_sum(ll a, ll b) {
    if (a > b) return 0ll;
    ll dif = a - 1, cnt = b - a + 1;
    ll ans = ((b - a + 1) * (b - a + 2)) / 2;
    ans += ((b - a + 1) * dif);
    return ans;
}

string dec_to_bin(ll a) {
    string s = "";
    for (ll i = a; i > 0; ) {
        ll k = i % 2;
        i /= 2;
        char c = k + 48;
        s += c;
    }
    if (a == 0) {
        s = "0";
    }
    reverse(all(s));
    return s;
}

ll bin_to_dec(string s) {
    ll num = 0;
    for (int i = 0; i < s.size(); i++) {
        num *= 2ll;
        num += (s[i] - '0');
    }
    return num;
}

ll factorial_by_mod(ll n, ll mod) {
    ll ans = 1;
    ll num;
    for (ll i = 1; i <= n; ++i) {
        num = i % mod;
        ans *= num;
        ans %= mod;
    }
    return ans;
}

bool isPrime(ll a) {
    if (a == 1) return false;
    for (ll i = 2; i * i <= a; i++) {
        if (a % i == 0) return false;
    }
    return true;
}

ll binpow(ll a, ll b) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
        }
        b >>= 1;
        a *= a;
    }
    return ans;
}

ll binpow_by_mod(ll a, ll b, ll mod) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
            ans %= mod;
        }
        b >>= 1;
        a *= a;
        a %= mod;
    }
    return ans;
}

/* -------------------- Solution -------------------- */

const int N = 100005, M = 1000005;
pii edge[M];
unsigned long long dp[N];
int clr[N], sz[N], p[N], cnt[N][2], used[N], usedd[N], ans[N], dpp[N], from[N];
vi comp[N];
int n, m, last, s, cur_ind;

void add(int x) {
    if (!x) return;
    for (int i = n; i >= x; --i) {
        dp[i] += dp[i - x];
    }
}

void del(int x) {
    if (!x) return;
    for (int i = x; i <= n; ++i) {
        dp[i] -= dp[i - x];
    }
}

void make_set(int x) {
    comp[x] = { x };
    cnt[x][0] = 1;
    cnt[x][1] = 0;
    clr[x] = 0;
    sz[x] = 1;
    p[x] = x;
}

int find_set(int x) {
    if (x == p[x]) return x;
    return p[x] = find_set(p[x]);
}

void union_sets(int x, int y, bool f) {
    if (sz[x] < sz[y]) swap(x, y);
    del(max(cnt[x][0], cnt[x][1]) - min(cnt[x][0], cnt[x][1]));
    s -= min(cnt[x][0], cnt[x][1]);
    del(max(cnt[y][0], cnt[y][1]) - min(cnt[y][0], cnt[y][1]));
    s -= min(cnt[y][0], cnt[y][1]);
    if (f) {
        swap(cnt[y][0], cnt[y][1]);
    }
    cnt[x][0] += cnt[y][0];
    cnt[x][1] += cnt[y][1];
    add(max(cnt[x][0], cnt[x][1]) - min(cnt[x][0], cnt[x][1]));
    s += min(cnt[x][0], cnt[x][1]);
    if (!dp[n - s]) {
        del(max(cnt[x][0], cnt[x][1]) - min(cnt[x][0], cnt[x][1]));
        s -= min(cnt[x][0], cnt[x][1]);
        cnt[x][0] -= cnt[y][0];
        cnt[x][1] -= cnt[y][1];
        if (f) {
            swap(cnt[y][0], cnt[y][1]);
        }
        add(max(cnt[x][0], cnt[x][1]) - min(cnt[x][0], cnt[x][1]));
        s += min(cnt[x][0], cnt[x][1]);
        add(max(cnt[y][0], cnt[y][1]) - min(cnt[y][0], cnt[y][1]));
        s += min(cnt[y][0], cnt[y][1]);
        return;
    }
    last = cur_ind;
    sz[x] += sz[y];
    p[y] = x;
    for (auto elem : comp[y]) {
        comp[x].pub(elem);
        if (f) {
            clr[elem] ^= 1;
        }
    }
}

void slv() {
    dp[0] = 1;
    cin >> n >> m;
    last = m + 1, cur_ind = m;
    for (int i = 1; i <= m; ++i) {
        cin >> edge[i].ff >> edge[i].ss;
    }
    for (int i = 1; i <= 2 * n; ++i) {
        make_set(i);
        add(1);
    }
    for (int i = m; i >= 1; --i) {
        cur_ind = i;
        int x = edge[i].ff, y = edge[i].ss;
        int x_comp = find_set(x), y_comp = find_set(y);
        if (x_comp != y_comp) {
            union_sets(x_comp, y_comp, (clr[x] == clr[y]));
            continue;
        }
        if (clr[x] != clr[y] && dp[n - s]) {
            last = i;
        }
    }
    vi v;
    for (int i = 1; i <= 2 * n; ++i) {
        int u = find_set(i);
        if (used[u]) continue;
        used[u] = 1;
        if (cnt[u][0] != cnt[u][1]) {
            v.pub(max(cnt[u][0], cnt[u][1]) - min(cnt[u][0], cnt[u][1]));
        }
    }
    dpp[0] = 1;
    for (auto elem : v) {
        for (int i = n - s; i >= elem; --i) {
            if (dpp[i - elem]) {
                dpp[i] = 1;
                from[i] = elem;
            }
        }
    }
    int node = n - s;
    while (node) {
        ++usedd[from[node]];
        node -= from[node];
    }
    for (int i = 1; i <= 2 * n; ++i) {
        used[i] = 0;
    }
    int m1 = 0, m2 = 0;
    for (int i = 1; i <= 2 * n; ++i) {
        int u = find_set(i);
        if (used[u]) continue;
        used[u] = 1;
        int val = max(cnt[u][0], cnt[u][1]) - min(cnt[u][0], cnt[u][1]), mn = min(cnt[u][0], cnt[u][1]);
        m1 += mn, m2 += mn;
        if (cnt[u][0] < cnt[u][1]) swap(cnt[u][0], cnt[u][1]);
        if (usedd[val]) m1 += val;
        else m2 += val;
        for (auto elem : comp[u]) {
            if (clr[elem] == 0) {
                if (usedd[val]) {
                    ans[elem] = 0;
                }
                else {
                    ans[elem] = 1;
                }
            }
            if (clr[elem] == 1) {
                if (!usedd[val]) {
                    ans[elem] = 0;
                }
                else {
                    ans[elem] = 1;
                }
            }
        }
        if (usedd[val]) --usedd[val];
    }
    assert(m1 == m2);
    for (int i = 1; i <= 2 * n; ++i) {
        cout << ans[i];
    }
}

void cs() {
    int tstc = 1;
    //cin >> tstc;
    while (tstc--) {
        slv();
    }
}

void precalc() {
    return;
}

int main() {
    fastio();
    precalc();
    //precision(0);
    cs();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 9900kb

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.