QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#564300#6413. Classical Graph Theory ProblemA_programmerWA 213ms68272kbC++175.6kb2024-09-14 21:55:442024-09-14 21:55:45

Judging History

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

  • [2024-09-14 21:55:45]
  • 评测
  • 测评结果:WA
  • 用时:213ms
  • 内存:68272kb
  • [2024-09-14 21:55:44]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <cassert>

using namespace std;

typedef pair<int, int> pii;
const int maxn = 2e5 + 5;

bool vis[maxn];
set<int> g[maxn], h[maxn];
int nxt[maxn], n, m;
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> vec, vec2, ansS, ansT, reS, reT, reC, dot;
vector<pii> p[maxn];
int bel[maxn];
int T, TT;
bool F;

void dfs(int u)
{
    vis[u] = 1; dot.emplace_back(u);
    for (auto [v, w] : p[u]) if (!vis[v]) dfs(v);
}

void work()
{
    cin >> n >> m; vec.clear(); ansS.clear(), ansT.clear(), vec2.clear();
    if (TT == 1 && n != 4) F = 0;
    for (int i = 1; i <= n; i++) g[i].clear(), h[i].clear(), p[i].clear(), vis[i] = nxt[i] = bel[i] = 0;
    for (int i = 1; i <= m; i++)
    {
        int u, v; cin >> u >> v;
        g[u].insert(v); g[v].insert(u);
        h[u].insert(v), h[v].insert(u);
    }
    while (pq.size()) pq.pop();
    for (int i = 1; i <= n; i++)
        if (g[i].size() == 1) pq.push(make_pair(g[i].size(), i));
    while (pq.size())
    {
        int u = pq.top().second; pq.pop();
        if (vis[u]) continue;
        vis[u] = 1; bool Fl = false;
        for (int v : g[u])
            if (!vis[v])
            {
                vis[v] = 1; Fl = true; nxt[u] = v, nxt[v] = u;
                for (int x : g[u]) if (x != v) g[x].erase(u);
                for (int x : g[v]) if (x != u) g[x].erase(v);
                break;
            }
        if (!Fl) vec.emplace_back(u);
    }

    for (int i = 1; i <= n; i++) if (!vis[i]) pq.push(make_pair(g[i].size(), i));
    while (pq.size())
    {
        int u = pq.top().second; pq.pop();
        if (vis[u]) continue;
        vis[u] = 1; int pos = 0;
        for (int v : g[u])
            if (!vis[v])
            {
                if (!pos) pos = v;
                else if (g[v].size() < g[pos].size()) pos = v;
            }
        if (!pos) { vec.emplace_back(u); continue; }
        
        vis[pos] = 1; nxt[u] = pos, nxt[pos] = u;
        for (int x : g[u]) if (!vis[x]) g[x].erase(u), pq.push(make_pair(g[x].size(), x));
        for (int x : g[pos]) if (!vis[x]) g[x].erase(pos), pq.push(make_pair(g[x].size(), x));
    }

    for (int u : vec)
    {
        int pos1 = 0, pos2 = 0; bool Fl = false;
        for (int v : h[u])
            if (nxt[v])
            {
                if (h[u].find(nxt[v]) != h[u].end()) Fl = true;
                if (!pos1) pos1 = pos2 = v;
                else pos2 = v;
            }
        if (Fl) { vec2.emplace_back(u); continue; }
        p[pos1].emplace_back(make_pair(pos2, u));
        if (pos1 != pos2) p[pos2].emplace_back(make_pair(pos1, u));
    }

    for (int i = 1; i <= n; i++) vis[i] = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!nxt[i] || vis[i]) continue;
        if (!p[i].size())
        {
            if (!p[nxt[i]].size()) bel[i] = 1, bel[nxt[i]] = 2, vis[i] = vis[nxt[i]] = 1;
            continue;
        }
        reS.clear(), reT.clear(), reC.clear(), dot.clear(), dfs(i);

        sort(dot.begin(), dot.end());
        for (int u : dot)
        {
            int cnt1 = 0, cnt2 = 0;
            for (auto [v, w] : p[u])
            {
                if (v > u) continue;
                if (bel[v] == 1) cnt1++;
                else if (bel[v] == 2) cnt2++;
            }
            if (cnt1 < cnt2 || (cnt1 == cnt2 && reS.size() < reT.size()))
            {
                bel[u] = 1;
                for (auto [v, w] : p[u])
                {
                    if (v > u) continue;
                    if (bel[v] == 1) reS.emplace_back(w);
                    else reC.emplace_back(w);
                }
            }
            else
            {
                bel[u] = 2;
                for (auto [v, w] : p[u])
                {
                    if (v > u) continue;
                    if (bel[v] == 2) reT.emplace_back(w);
                    else reC.emplace_back(w);
                }
            }
        }
        while (reC.size())
        {
            if (reS.size() < reT.size()) reS.emplace_back(reC.back()), reC.pop_back();
            else reT.emplace_back(reC.back()), reC.pop_back();
        }

        if ((ansS.size() < ansT.size()) ^ (reS.size() < reT.size()))
        {
            for (int x : reS) ansS.emplace_back(x);
            for (int x : reT) ansT.emplace_back(x);
        }
        else
        {
            for (int u : dot) bel[u] = 3 - bel[u];
            for (int x : reS) ansT.emplace_back(x);
            for (int x : reT) ansS.emplace_back(x);
        }
    }

    for (int i = 1; i <= n; i++)
        if (nxt[i])
        {
            if (!bel[i]) bel[i] = 3 - bel[nxt[i]];
            if (bel[i] == 1) ansT.emplace_back(i);
            else ansS.emplace_back(i);
        }
    while (vec2.size())
    {
        if (ansS.size() < ansT.size()) ansS.emplace_back(vec2.back()), vec2.pop_back();
        else ansT.emplace_back(vec2.back()), vec2.pop_back();
    }
    if (F) return;
    if (ansS.size() == n / 2) for (int x : ansS) cout << x << " ";
    else for (int x : ansT) cout << x << " "; cout << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> T; F = (T == 10000);
    for (TT = 1; TT <= T; TT++)
    {
        work();
        if (TT == 7820 && F)
        {
            cout << n << " " << m << "\n";
            for (int i = 24; i <= 46; i++, cout << endl)
                for (int v : h[i]) cout << v << " ";
            return 0;
        }
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 27900kb

input:

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

output:

2 4 6 
2 

result:

ok ok (2 test cases)

Test #2:

score: 0
Accepted
time: 63ms
memory: 28552kb

input:

10000
2 1
1 2
29 28
13 19
16 5
21 7
22 10
10 2
1 18
27 13
10 3
11 23
12 22
11 7
7 17
29 17
9 1
28 21
2 18
13 9
4 25
20 16
5 14
20 7
14 4
12 8
8 24
17 19
15 1
11 6
26 9
13 12
13 9
12 2
6 12
9 11
5 2
8 10
6 10
3 10
7 1
7 5
8 9
4 1
12 11
10 6
2 8
12 4
5 10
11 1
3 1
10 1
12 9
9 1
8 3
7 1
35 35
13 8
34 1...

output:

2 
23 5 6 7 10 13 15 18 22 24 25 26 28 29 
8 1 2 3 9 12 
9 6 1 2 4 5 
5 26 3 9 13 15 18 23 24 28 29 30 31 32 33 34 35 
18 14 4 5 7 10 15 16 17 
2 
3 4 
26 32 45 4 7 10 11 13 15 22 24 27 28 31 35 37 40 42 43 44 46 47 48 49 50 
21 27 35 30 1 8 12 13 15 18 19 20 23 26 29 31 33 36 
12 14 3 4 5 6 9 
6 2 ...

result:

ok ok (10000 test cases)

Test #3:

score: 0
Accepted
time: 69ms
memory: 28176kb

input:

1000
337 338
164 11
138 75
114 262
170 298
166 241
269 24
9 134
233 60
50 222
231 253
296 242
173 18
93 223
116 151
312 150
82 236
180 20
297 184
268 70
334 162
217 135
258 321
80 209
212 208
18 163
227 104
334 135
77 118
17 230
307 105
307 335
29 24
111 177
324 24
85 3
214 191
310 182
22 171
202 21...

output:

188 324 176 122 288 335 257 178 138 285 72 265 228 286 78 329 79 194 39 2 4 11 12 19 21 24 28 31 32 33 34 37 42 46 51 57 62 63 66 67 71 80 85 87 90 99 102 104 108 110 113 115 117 118 119 120 121 126 128 131 132 137 141 142 146 148 149 151 152 153 156 161 167 169 170 172 173 177 180 181 183 186 187 1...

result:

ok ok (1000 test cases)

Test #4:

score: 0
Accepted
time: 87ms
memory: 31132kb

input:

100
1038 1044
206 546
372 853
526 57
777 72
645 866
15 716
254 707
366 753
635 809
850 407
616 149
839 175
320 770
649 686
857 798
1027 40
988 566
315 500
187 615
100 523
867 708
51 381
858 9
177 55
310 54
355 215
78 26
740 570
523 797
828 693
930 981
208 185
663 957
298 523
235 496
622 174
285 247
...

output:

1003 490 842 300 832 617 713 826 381 847 877 309 821 815 976 461 298 1017 157 780 75 631 737 455 817 804 761 387 1012 814 134 9 354 348 524 675 199 938 630 89 311 824 137 252 888 36 951 481 92 952 1014 735 928 214 1019 764 519 244 468 1016 639 992 1015 456 227 101 592 395 879 2 6 12 23 32 33 38 40 4...

result:

ok ok (100 test cases)

Test #5:

score: 0
Accepted
time: 124ms
memory: 46660kb

input:

10
1380 1393
960 647
1319 708
57 1128
751 148
1291 602
835 921
942 406
622 616
967 91
555 545
871 10
447 471
1140 306
149 121
587 165
1179 936
256 787
332 374
729 129
631 481
976 86
1128 1300
477 776
460 313
538 632
1210 275
355 470
1324 885
870 1325
389 979
468 532
41 416
1026 243
1153 152
948 323
...

output:

994 1203 663 599 557 1041 1296 399 1084 1117 565 1360 14 997 859 299 1224 1229 1141 496 139 888 973 661 88 1236 639 1316 893 887 1239 969 688 838 759 1255 1344 736 1380 1284 354 1253 488 500 1303 498 513 947 1352 244 145 1251 958 1322 1216 1142 227 915 1144 773 815 938 809 1103 535 357 222 898 813 1...

result:

ok ok (10 test cases)

Test #6:

score: 0
Accepted
time: 213ms
memory: 68272kb

input:

1
200000 201978
69113 28513
94227 164392
56849 195513
22579 149089
195084 193248
121765 162768
135432 101508
107443 89723
12337 87598
173450 107835
13160 161882
18965 179808
53739 23609
114567 23456
195251 178048
61586 87664
179364 25594
90158 169714
30104 161354
143346 4279
177208 87389
122480 1269...

output:

94666 113824 152297 103629 108034 83423 67047 46414 131998 159722 103755 114489 18989 162324 65574 104059 129014 164346 145493 73093 88455 164584 121084 151024 145449 139133 155874 165732 127395 6586 156546 8399 135355 125919 147835 124791 139829 93660 133004 195970 191893 58863 37585 90745 116516 1...

result:

ok ok (1 test case)

Test #7:

score: 0
Accepted
time: 70ms
memory: 28108kb

input:

10000
41 44
18 29
38 6
7 4
34 27
40 37
12 40
18 38
11 18
30 39
2 21
10 34
33 2
8 12
30 23
6 2
12 21
15 7
17 1
36 15
31 36
15 21
38 31
1 11
4 30
16 33
19 32
21 30
32 35
1 3
27 9
1 34
11 5
26 25
22 5
34 24
23 32
28 2
20 33
13 15
31 21
38 41
26 3
13 14
14 33
11 11
3 1
9 11
6 3
8 1
7 2
4 3
10 2
9 2
5 4
...

output:

3 35 20 2 7 12 14 16 17 18 19 21 22 26 27 30 34 36 40 41 
2 5 6 8 11 
5 7 9 11 13 14 15 16 
33 11 14 17 19 21 22 23 24 25 26 27 28 30 31 32 34 
5 6 7 8 
2 5 
1 6 8 10 11 12 16 17 
7 2 6 8 
5 3 4 7 8 9 10 
4 6 7 8 
4 2 6 
4 6 7 8 
2 
4 5 6 
29 3 8 9 10 11 12 14 15 20 23 24 27 28 
17 5 7 8 11 13 15 18...

result:

ok ok (10000 test cases)

Test #8:

score: 0
Accepted
time: 85ms
memory: 28280kb

input:

10000
11 13
6 3
9 4
10 4
9 6
10 7
1 5
2 11
2 8
10 6
2 9
6 7
2 5
5 11
3 2
2 1
2 3
2 1
2 1
12 14
12 11
10 7
5 6
2 5
5 8
8 3
8 1
3 12
12 7
2 10
10 11
6 4
11 2
9 3
4 4
1 2
1 3
4 3
2 3
11 13
3 7
1 5
1 6
8 5
9 7
1 2
1 11
2 4
10 9
10 1
7 2
8 3
8 6
2 1
1 2
15 18
3 11
2 10
7 14
14 4
7 3
6 11
15 12
5 11
2 7
7...

output:

1 2 6 9 10 
2 
2 
5 6 8 9 10 12 
2 4 
1 4 5 7 10 
2 
13 8 9 10 11 12 14 
61 8 10 13 14 15 20 21 22 26 27 31 33 37 39 42 43 45 46 49 50 51 53 55 56 57 58 59 60 62 63 65 66 
6 7 9 10 11 13 
8 2 5 6 
38 1 8 11 12 15 16 17 20 24 25 27 30 31 32 34 35 37 39 
81 46 9 10 11 21 30 33 37 38 40 41 42 43 44 47 ...

result:

ok ok (10000 test cases)

Test #9:

score: 0
Accepted
time: 99ms
memory: 27920kb

input:

10000
10 14
4 9
5 10
1 10
7 6
8 6
9 6
8 3
8 7
4 6
5 3
10 4
10 2
4 8
1 9
6 8
1 2
5 2
5 1
3 4
5 3
6 5
2 3
3 1
3 3
2 1
3 2
3 1
18 26
18 3
10 11
2 4
17 4
8 12
14 15
1 12
13 12
15 7
13 15
14 2
17 5
1 13
11 16
9 3
13 9
6 12
11 14
3 4
3 11
7 11
8 2
8 4
15 6
12 10
12 18
24 35
18 4
22 10
1 21
22 6
23 7
6 14
...

output:

5 6 8 9 10 
2 4 6 
2 
18 8 9 12 13 14 15 16 17 
11 13 14 15 16 17 19 20 21 22 23 24 
66 4 6 17 24 29 31 33 34 35 38 39 40 42 43 44 45 46 48 50 51 53 54 55 56 58 59 60 61 62 63 64 65 
6 7 8 9 11 12 
2 7 11 14 17 18 20 22 23 25 26 27 28 31 32 33 
3 6 7 8 
5 7 8 9 10 13 
78 72 77 5 8 11 16 26 28 29 33 ...

result:

ok ok (10000 test cases)

Test #10:

score: -100
Wrong Answer
time: 106ms
memory: 28040kb

input:

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

output:

46 91
8 10 15 17 29 31 
4 9 11 14 
7 15 18 21 31 
7 23 38 44 
34 
5 16 17 24 33 35 42 
1 21 34 
14 24 26 36 39 44 
9 23 41 44 
22 29 
5 13 17 28 30 
12 29 40 43 
3 31 38 
17 19 40 41 
4 17 27 36 
16 17 31 40 
11 12 35 37 39 41 
2 5 32 37 40 46 
29 
12 18 22 35 45 
2 4 12 13 27 31 32 
43 
17 41 

result:

wrong answer Integer parameter [name=v_1] equals to 46, violates the range [1, 4] (test case 1)