QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#618153#1869. Power Station of ArthcywoiAC ✓262ms18080kbC++232.5kb2024-10-06 19:15:102024-10-06 19:15:10

Judging History

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

  • [2024-10-06 19:15:10]
  • 评测
  • 测评结果:AC
  • 用时:262ms
  • 内存:18080kb
  • [2024-10-06 19:15:10]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < m; i ++ ) {
        int u, v;
        std::cin >> u >> v;
        u --, v -- ;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    std::vector<int> a(n), b(n);
    std::string c, d;
    for (int i = 0; i < n; i ++ ) {
        std::cin >> a[i];
    }
    std::cin >> c;
    for (int i = 0; i < n; i ++ ) {
        std::cin >> b[i];
    }
    std::cin >> d;

    bool bip;
    std::vector<int> vec;
    std::vector<int> col(n, -1);
    auto dfs = [&](auto self, int x, int c) -> void {
        col[x] = c;
        vec.push_back(x);
        for (auto y : adj[x]) {
            if (col[y] == -1) {
                self(self, y, c ^ 1);
            } else if (col[x] == col[y]) {
                bip = false;
            }
        }
    };

    for (int i = 0; i < n; i ++ ) {
        if (col[i] == -1) {
            vec.clear();
            bip = true;
            dfs(dfs, i, 0);
            if (bip) {
                std::vector<int> x[2], y[2];
                for (auto j : vec) {
                    x[col[j] ^ (c[j] == 'R')].push_back(a[j]);
                    y[col[j] ^ (d[j] == 'R')].push_back(b[j]);
                }
                std::sort(x[0].begin(), x[0].end());
                std::sort(x[1].begin(), x[1].end());
                std::sort(y[0].begin(), y[0].end());
                std::sort(y[1].begin(), y[1].end());
                if (x[0] != y[0] || x[1] != y[1]) {
                    std::cout << "NO\n";
                    return;
                }
            } else {
                int p = 0, q = 0;
                std::vector<int> x, y;
                for (auto j : vec) {
                    x.push_back(a[j]);
                    y.push_back(b[j]);
                    p += c[j] == 'R';
                    q += d[j] == 'R';
                }
                std::sort(x.begin(), x.end());
                std::sort(y.begin(), y.end());
                if (x != y || p % 2 != q % 2) {
                    std::cout << "NO\n";
                    return;
                }
            }
        }
    }
    std::cout << "YES\n";
}

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

    int t;
    std::cin >> t;

    while (t -- ) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 1
1 2
3 4
RR
4 3
BB
3 2
1 2
2 3
1 1 1
RBR
1 1 1
BBB
3 3
1 2
2 3
3 1
1 1 1
RBR
1 1 1
BBB

output:

YES
NO
YES

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 262ms
memory: 18080kb

input:

25054
5 10
4 5
4 2
4 3
4 1
5 2
5 3
5 1
2 3
2 1
3 1
12 2 9 10 7
RRRBB
10 2 9 7 12
RRBRB
1 0
4
R
4
R
8 4
4 3
1 5
8 5
6 5
7 2 11 10 9 6 3 5
RBRBBRBR
7 2 10 11 6 5 3 9
RBRBBRBR
7 4
7 2
5 1
5 6
5 4
12 5 9 14 5 12 12
RRBRBRR
14 12 9 5 12 12 5
RBBRBRB
10 1
4 6
5 3 2 9 7 3 11 11 6 8
RRRBRRBBBR
5 3 2 3 7 9 1...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
NO
NO
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
NO
NO...

result:

ok 25054 lines