QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#719546#9449. New School TermMher777TL 1763ms260768kbC++208.5kb2024-11-07 03:29:552024-11-07 03:29:56

Judging History

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

  • [2024-11-07 03:29:56]
  • 评测
  • 测评结果:TL
  • 用时:1763ms
  • 内存:260768kb
  • [2024-11-07 03:29:55]
  • 提交

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 = 1000005, MX = 10005;
pii edge[N];
pii dp[MX];
int clr[MX], sz[MX], p[MX], cnt[MX][2], used[MX], usedd[MX], dpp[MX], from[MX];
vi comp[MX];
pii M = { 1000000007, 1000000009 };
unordered_map<int, unordered_map<int, int>> not_con;
int n, m, last, s, cur_ind;

pair<int, int> sum(const pair<int, int>& a, const pair<int, int>& b) {
    return make_pair((a.ff + b.ff) % M.ff, (a.ss + b.ss) % M.ss);
}

pair<int, int> dif(const pair<int, int>& a, const pair<int, int>& b) {
    return make_pair((a.ff - b.ff + M.ff) % M.ff, (a.ss - b.ss + M.ss) % M.ss);
}

pair<int, int> pro(const pair<int, int>& a, const pair<int, int>& b) {
    return make_pair((a.ff * 1LL * b.ff) % M.ff, (a.ss * 1LL * b.ss) % M.ss);
}

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

void del(int x) {
    if (!x) return;
    for (int i = x; i <= n; ++i) {
        dp[i] = dif(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].ff && !dp[n - s].ss)) {
        not_con[x][y] = not_con[y][x] = 1;
        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 : not_con[y]) {
        not_con[x][elem.ff] = 1;
        not_con[elem.ff][x] = 1;
    }
    for (auto elem : comp[y]) {
        comp[x].pub(elem);
        if (f) {
            clr[elem] ^= 1;
        }
    }
}


void slv() {
    dp[0] = { 1,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 (!not_con[x_comp][y_comp] && !not_con[y_comp][x_comp] && x_comp != y_comp) {
            union_sets(x_comp, y_comp, (clr[x] == clr[y]));
            continue;
        }
        if (clr[x] != clr[y] && (dp[n - s].ff || dp[n - s].ss)) {
            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;
    sort(all(v));
    for (auto elem : v) {
        for (int i = n - s; i >= elem; --i) {
            if (dpp[i]) continue;
            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]);
        bool f = false;
        if (cnt[u][0] < cnt[u][1]) {
            swap(cnt[u][0], cnt[u][1]);
            for (auto elem : comp[u]) {
                clr[elem] ^= 1;
            }
        }
        if (!usedd[val]) {
            for (auto elem : comp[u]) {
                clr[elem] ^= 1;
            }
        }
        for (auto elem : comp[u]) {
            if (clr[elem] == 0) ++m1;
            else ++m2;
        }
        if (usedd[val]) --usedd[val];
    }
    for (int i = 1; i <= 2 * n; ++i) {
        cout << clr[i];
    }
    cout << '\n';
}

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

input:

2 4
1 3
2 4
1 4
1 2

output:

1010

result:

ok Output is valid. OK

Test #2:

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

input:

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

output:

110010

result:

ok Output is valid. OK

Test #3:

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

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

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

input:

1 1
1 2

output:

10

result:

ok Output is valid. OK

Test #5:

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

input:

2 3
2 4
3 4
1 2

output:

1001

result:

ok Output is valid. OK

Test #6:

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

input:

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

output:

101010

result:

ok Output is valid. OK

Test #7:

score: 0
Accepted
time: 1ms
memory: 5724kb

input:

4 9
4 7
3 8
1 5
2 7
2 8
6 8
7 8
1 4
1 6

output:

01010110

result:

ok Output is valid. OK

Test #8:

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

input:

5 16
3 6
9 10
2 7
1 10
1 5
2 10
3 5
5 6
3 4
2 5
4 5
3 8
4 7
6 8
1 6
7 10

output:

0010111010

result:

ok Output is valid. OK

Test #9:

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

input:

6 13
4 5
2 9
3 8
4 8
4 11
10 12
3 4
3 9
5 11
2 8
5 10
5 8
1 11

output:

110110001001

result:

ok Output is valid. OK

Test #10:

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

input:

12 153
1 24
16 18
7 14
1 16
20 21
9 14
21 22
4 5
17 24
4 12
5 17
13 24
14 15
12 23
12 16
8 11
14 24
9 16
2 5
6 19
11 17
4 22
4 7
6 16
7 20
8 15
5 24
2 10
10 21
21 24
1 12
11 19
18 21
18 24
12 17
13 22
7 9
13 23
4 9
11 13
15 21
5 7
2 4
15 16
17 19
11 16
11 20
7 8
4 15
13 14
6 18
2 19
9 13
23 24
4 21
...

output:

111100011001101110000001

result:

ok Output is valid. OK

Test #11:

score: 0
Accepted
time: 20ms
memory: 9868kb

input:

259 33757
472 500
65 336
138 469
307 442
427 458
43 239
17 508
460 466
108 393
79 92
250 483
44 277
17 132
35 57
155 499
184 474
246 272
274 418
457 458
338 372
196 514
31 208
117 187
90 229
153 284
189 355
16 337
146 456
269 271
279 412
305 336
303 441
399 472
85 286
91 97
157 437
137 379
71 360
27...

output:

000111001101000110010001111000111111100000011101010011011101110001010010000001111000111010000011101001010001000011100101010011000101101100010101100101100101100010011000110100001110011000111101010100011001001110110101101101010101101001100110011110101001111011110001010001111101001110101101111001001100...

result:

ok Output is valid. OK

Test #12:

score: 0
Accepted
time: 278ms
memory: 68884kb

input:

811 265557
217 1153
383 1609
165 177
612 1602
1057 1428
37 436
135 1200
368 684
448 722
145 1583
325 1052
246 480
74 148
122 1111
1256 1327
304 1070
1285 1542
802 813
454 1563
265 1193
94 848
432 1156
429 1194
427 1230
1152 1406
1329 1355
702 845
591 1232
877 1288
1257 1549
340 659
1080 1333
910 137...

output:

001000000010111000100000000010100001011010111010110111111110000111010000101011011010001011001010111000001011000011011001000001011010001010000011100001010101100110010101011001101100100000100100001010000000100111110011110010001000000001011101100011100001000000101110100000001001100000011110101100100010...

result:

ok Output is valid. OK

Test #13:

score: 0
Accepted
time: 1763ms
memory: 260768kb

input:

1691 323743
1246 2397
1445 2647
2010 2806
2001 2896
802 2258
2679 2976
2203 2875
2445 2698
137 3004
536 1800
2316 2520
594 1517
279 1558
1934 2871
57 1358
357 976
1764 2672
869 2137
1694 2201
491 1906
1177 1414
1304 1377
2454 2653
626 2637
1425 1677
620 876
1326 2085
404 874
626 1565
136 597
2885 31...

output:

000101110111111001001111011011101100010010100110100011010001111100011101110000111110111101011010000101000111000111001100011011001111001110110010001101001000001001001101011010101010111011011101000110100100000001101100111100001010111000100101101001010100100110110001011011001110011010000011000100010101...

result:

ok Output is valid. OK

Test #14:

score: -100
Time Limit Exceeded

input:

2891 285302
2273 3206
2376 4737
1075 5673
2493 5453
548 1902
603 1376
1948 2985
108 4730
2172 2948
947 1758
762 1558
2813 5701
2287 3502
297 1501
568 4247
4569 5071
832 3005
412 4226
1813 4519
726 3017
1658 3990
1771 3230
1705 2149
765 4782
5420 5652
3089 4727
4362 5054
1578 3729
1111 5740
2234 5691...

output:


result: