QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#628839#9449. New School TermtovarischWA 170ms11552kbC++233.2kb2024-10-10 22:40:122024-10-10 22:40:12

Judging History

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

  • [2024-10-10 22:40:12]
  • 评测
  • 测评结果:WA
  • 用时:170ms
  • 内存:11552kb
  • [2024-10-10 22:40:12]
  • 提交

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] = 1;
            zeros[i] = 0;
            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, to, col(2*n);
    DSU dsu(2*n);

    for (int i=0; i+1<2*n; ++i) from.pb(i), to.pb(i+1);
    for(int i=0; i<m; ++i) {
        int u, v; cin >> u >> v;
        from.pb(u-1), to.pb(v-1);
    }

    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 flip = [&](int& u) {
        for(int& w : dsu.comp[u]) col[w] ^=1;
        swap(dsu.ones[u], 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;
        dpo.reset();
        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});
            int cnt = col[p.ff] == col[p.ss] ? dsu.zeros[u] + dsu.ones[v] : dsu.zeros[u] + dsu.zeros[v];

            if (n - cnt >=0 && dpc[n - cnt]) {
                if (col[p.ff] == col[p.ss]) flip(v);
            } else {
                if (col[p.ff] != col[p.ss]) flip(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();
    };

    while (from.size()) {
        int u=from.back(), v = to.back();
        from.pop_back(), to.pop_back();
        
        process.pb({u, v});
        comp.insert(dsu.find(u));
        comp.insert(dsu.find(v));

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

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

input:

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

output:

011010

result:

ok Output is valid. OK

Test #3:

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

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

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

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

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

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

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

input:

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

output:

010101

result:

ok Output is valid. OK

Test #7:

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

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

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:

1101000101

result:

ok Output is valid. OK

Test #9:

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

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: 1ms
memory: 3572kb

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:

000011100110010001111110

result:

ok Output is valid. OK

Test #11:

score: 0
Accepted
time: 18ms
memory: 4136kb

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: 77ms
memory: 10308kb

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:

110111111101000111011111111101011110100101000101001000000001111000101111010100100101110100110101000111110100111100100110111110100101110101111100011110101010011001101010100110010011011111011011110101111111011000001100001101110111111110100010011100011110111111010001011111110110011111100001010011011101...

result:

ok Output is valid. OK

Test #13:

score: -100
Wrong Answer
time: 170ms
memory: 11552kb

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:

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