QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#618313#7798. Colorful VillageTokaiZaopenRE 163ms25440kbC++204.9kb2024-10-06 20:53:072024-10-06 20:53:09

Judging History

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

  • [2024-10-06 20:53:09]
  • 评测
  • 测评结果:RE
  • 用时:163ms
  • 内存:25440kb
  • [2024-10-06 20:53:07]
  • 提交

answer

#include <bits/stdc++.h>

#define int ll
using ll = long long;

// #define MING_LE

const int inf = 1e18;
const int mod = 998244353;

using namespace std;

struct SCC
{
    vector<vector<int>> e;
    vector<int> siz;
    vector<int> scc;
    vector<int> dfn;
    vector<int> low;
    vector<bool> ins;
    stack<int> sck;
    int sccn;
    int id;
    int n;

    void dfs(int x)
    {
        ins[x] = 1;
        dfn[x] = ++id;
        low[x] = dfn[x];
        sck.push(x);

        for (auto it : e[x])
        {
            if (!dfn[it])
            {
                dfs(it);
                low[x] = min(low[x], low[it]);
            }
            else if (ins[it])
            {
                low[x] = min(low[x], dfn[it]);
            }
        }

        if (low[x] == dfn[x])
        {
            scc[x] = ++sccn;
            ins[x] = 0;
            siz[sccn] = 1;
            while (sck.top() != x)
            {
                scc[sck.top()] = sccn;
                ins[sck.top()] = 0;
                siz[sccn]++;
                sck.pop();
            }
            sck.pop();
        }
    }

    SCC(vector<vector<int>> &a, int len)
    {
        e = a;
        siz.resize(len + 5);
        scc.resize(len + 5);
        dfn.resize(len + 5);
        low.resize(len + 5);
        ins.resize(len + 5);
        n = len;
        sccn = 0;
        while (!sck.empty())
            sck.pop();
    }

    SCC(int len)
    {
        e.resize(len + 5);
        siz.resize(len + 5);
        scc.resize(len + 5);
        dfn.resize(len + 5);
        low.resize(len + 5);
        ins.resize(len + 5);
        n = len;
        id = 0;
        sccn = 0;
        while (!sck.empty())
            sck.pop();
    }

    SCC() {}

    void addedge(int from, int to)
    {
        e[from].push_back(to);
    }

    void GetScc()
    {
        for (int i = 1; i <= n; i++)
        {
            if (!dfn[i])
                dfs(i);
        }
    }
};

int n;
void dfs(int x, int fa, SCC &graph);
vector<int> a[200010];
pair<int, int> cor[100010];
int ch[200010];

void solve();

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int __ = 1;
    cin >> __;

    while (__--)
        solve();

    return 0;
}

void solve()
{
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
    {
        a[i].clear();
        ch[i] = 0;
        cor[i] = make_pair(0, 0);
    }
    SCC graph(n * 4);
    for (int i = 1; i <= 2 * n; i++)
    {
        int c;
        cin >> c;
        ch[i] = c;
        if (cor[c].first == 0)
            cor[c].first = i;
        else
            cor[c].second = i;
    }
    for (int i = 1; i <= 2 * n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }

    for (int i = 2; i <= n; i++)
    {
        graph.addedge(cor[i].first, cor[i].second + 2 * n);
        graph.addedge(cor[i].first + 2 * n, cor[i].second);
        graph.addedge(cor[i].second, cor[i].first + 2 * n);
        graph.addedge(cor[i].second + 2 * n, cor[i].first);
    }

    SCC now;
    now = graph;

    now.addedge(cor[1].first + 2 * n, cor[1].first);
    now.addedge(cor[1].second, cor[1].second + 2 * n);
    dfs(cor[1].first, 0, now);
    now.GetScc();

    bool flag = 1;
    for (int i = 1; i <= 2 * n; i++)
    {
        if (now.scc[i] == now.scc[i + 2 * n])
        {
            flag = 0;
            break;
        }
    }
    if (flag)
    {
        vector<int> res;
        res.clear();
        for (int i = 1; i <= 2 * n; i++)
        {
            if (now.scc[i] < now.scc[i + 2 * n])
                res.push_back(i);
        }

        // cout << "YES\n";
        for (auto it : res)
            cout << it << " ";
        cout << '\n';
        return;
    }

    now = graph;
    swap(cor[1].first, cor[1].second);

    now.addedge(cor[1].first + 2 * n, cor[1].first);
    now.addedge(cor[1].second, cor[1].second + 2 * n);
    dfs(cor[1].first, 0, now);
    now.GetScc();

    flag = 1;
    for (int i = 1; i <= 2 * n; i++)
    {
        if (now.scc[i] == now.scc[i + 2 * n])
        {
            flag = 0;
            break;
        }
    }
    if (flag)
    {
        vector<int> res;
        res.clear();
        for (int i = 1; i <= 2 * n; i++)
        {
            if (now.scc[i] < now.scc[i + 2 * n])
                res.push_back(i);
        }

        // cout << "YES\n";
        for (auto it : res)
            cout << it << " ";
        cout << '\n';
        return;
    }

    cout << "-1\n";
}

void dfs(int x, int fa, SCC &graph)
{

    for (auto it : a[x])
    {
        if (fa == it)
            continue;
        graph.addedge(it, x);
        graph.addedge(x + 2 * n, it + 2 * n);
        // if (ch[it] == ch[x])
        //     continue;

        dfs(it, x, graph);
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5848kb

input:

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

output:

1 2 5 7 
-1

result:

ok ok, 1 yes, 1 no (2 test cases)

Test #2:

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

input:

1
1
1 1
1 2

output:

1 

result:

ok ok, 1 yes, 0 no (1 test case)

Test #3:

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

input:

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

output:

1 
1 
1 2 4 
2 4 5 7 
1 

result:

ok ok, 5 yes, 0 no (5 test cases)

Test #4:

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

input:

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

output:

3 4 6 
-1
-1
-1
-1
1 2 3 5 6 
1 2 5 7 8 
2 4 7 9 12 13 14 16 
1 2 5 8 9 13 14 16 18 19 21 
-1

result:

ok ok, 5 yes, 5 no (10 test cases)

Test #5:

score: 0
Accepted
time: 2ms
memory: 6012kb

input:

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

output:

-1
1 2 
1 4 6 
1 2 3 7 9 11 13 16 
1 2 6 
-1
1 
1 2 3 4 
1 3 5 6 9 10 11 14 16 19 20 22 24 27 
3 4 5 7 8 9 11 16 
-1
1 2 3 4 7 8 10 13 14 20 24 25 26 28 30 31 34 36 38 39 
-1
1 
1 2 3 4 7 9 13 15 17 19 
2 3 5 
-1
-1
-1
1 
1 
1 2 3 5 7 9 11 15 
1 2 5 6 
1 2 
1 
1 
-1
-1
1 2 4 5 9 
2 5 6 
4 6 7 8 9 11...

result:

ok ok, 65 yes, 35 no (100 test cases)

Test #6:

score: 0
Accepted
time: 10ms
memory: 5840kb

input:

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

output:

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

result:

ok ok, 617 yes, 383 no (1000 test cases)

Test #7:

score: 0
Accepted
time: 75ms
memory: 3592kb

input:

100000
1
1 1
2 1
1
1 1
2 1
1
1 1
2 1
1
1 1
1 2
1
1 1
2 1
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
2 1
1
1 1
2 1
1
1 1
2 1
1
1 1
2 1
1
1 1
1 2
1
1 1
1 2
1
1 1
2 1
1
1 1
2 1
1
1 1
1 2
1
1 1
2 1
1
1 1
1 2
1
1 1
1 2
1
1 1
1 2
1
1 1
2 1
1
1 1
1 2
1
1 1
2 1
1
1...

output:

1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
...

result:

ok ok, 100000 yes, 0 no (100000 test cases)

Test #8:

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

input:

10000
8
2 8 3 1 5 7 6 2 5 3 1 6 4 7 4 8
14 10
7 10
15 7
14 6
14 13
8 7
16 1
5 7
9 14
4 5
11 12
13 16
2 13
11 10
3 4
6
3 3 6 5 2 4 4 1 6 2 5 1
7 6
9 1
2 12
4 6
8 4
12 10
5 10
11 7
8 10
3 4
9 7
56
29 10 48 24 2 5 45 30 18 28 51 14 18 55 50 5 19 38 43 14 28 7 46 1 7 20 38 31 20 22 33 30 32 12 55 15 4 5...

output:

1 4 5 7 10 13 14 16 
-1
-1
2 4 5 7 
1 4 6 8 10 
1 
-1
2 3 4 
3 4 6 7 10 11 13 15 18 20 21 22 
-1
1 
-1
1 
1 
1 2 3 4 10 
1 2 3 4 5 6 8 13 18 19 
1 3 5 7 8 9 10 13 14 21 22 
1 3 4 7 8 
-1
1 
-1
1 2 3 4 10 
-1
-1
2 3 6 7 9 11 13 15 
-1
3 6 7 8 9 13 15 20 21 22 25 27 28 29 31 32 34 
1 3 4 5 6 9 10 14 1...

result:

ok ok, 6241 yes, 3759 no (10000 test cases)

Test #9:

score: 0
Accepted
time: 91ms
memory: 6356kb

input:

1000
116
50 59 41 49 19 85 9 94 34 77 78 3 113 32 109 17 103 56 47 41 64 99 28 114 14 55 97 12 63 105 84 81 54 5 13 15 101 20 39 96 48 112 14 97 76 88 67 75 6 32 108 11 87 39 57 11 49 18 36 110 104 31 58 73 72 66 89 115 8 61 55 62 6 10 25 4 115 1 112 21 53 111 51 69 27 56 82 24 30 106 30 80 35 65 10...

output:

-1
-1
-1
-1
-1
-1
-1
-1
3 4 5 10 11 15 16 17 18 19 20 22 25 27 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 3 4 7 8 9 10 13 14 16 20 22 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2 3 4 5 6 12 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2 3 5...

result:

ok ok, 114 yes, 886 no (1000 test cases)

Test #10:

score: 0
Accepted
time: 104ms
memory: 8508kb

input:

100
1751
1420 521 90 1373 1093 161 1200 1386 1091 1264 1265 1065 1378 1149 306 545 479 1579 1078 151 934 904 1574 1199 1581 952 544 386 867 547 401 1218 1444 652 1486 826 137 860 1050 832 738 455 352 24 1462 1581 532 150 913 213 748 465 1057 539 838 617 1150 1335 573 1692 1590 571 981 752 1697 1452 ...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok ok, 1 yes, 99 no (100 test cases)

Test #11:

score: 0
Accepted
time: 163ms
memory: 25440kb

input:

10
5923
677 2765 1180 642 3593 131 5590 3281 4786 2499 1862 247 239 3798 3174 2761 1281 1765 937 144 5408 4039 5442 2407 2669 5018 434 1962 319 3944 2812 3472 94 788 3672 5233 3722 4465 3786 2110 2508 1725 518 2460 1594 2334 4106 2411 3758 3669 5343 5236 5481 4963 2978 1374 5119 4233 872 5860 1969 4...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok ok, 0 yes, 10 no (10 test cases)

Test #12:

score: -100
Runtime Error

input:

5
792
82 199 76 52 611 658 522 660 521 464 542 179 170 151 363 514 408 538 159 309 476 681 224 384 238 14 551 357 291 226 91 655 520 467 40 314 128 233 157 355 364 394 386 59 104 72 614 153 145 342 288 300 174 481 407 588 479 262 594 562 732 766 549 285 626 197 599 441 677 574 760 265 150 341 379 55...

output:


result: