QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386508#8029. Traveling Merchant15owzLy1WA 40ms9772kbC++202.6kb2024-04-11 17:41:362024-04-11 17:41:37

Judging History

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

  • [2024-08-07 20:25:21]
  • hack成功,自动添加数据
  • (/hack/771)
  • [2024-04-11 17:41:37]
  • 评测
  • 测评结果:WA
  • 用时:40ms
  • 内存:9772kb
  • [2024-04-11 17:41:36]
  • 提交

answer

#include <bits/stdc++.h>

const int N = 2e5 + 5;

struct edge {
    int next, to;
} e[N << 1];
int ecnt, ehead[N];

std::string cl;
int tot;
int dfn[N], low[N], dep[N];
bool vis[N];

int fa[N][18];
int n, m;

inline void AddEdge(const int u, const int v) {
    e[++ecnt] = {ehead[u], v}, ehead[u] = ecnt;
    e[++ecnt] = {ehead[v], u}, ehead[v] = ecnt;
}

void init() {
    ecnt = 1;
    tot = 0;
    memset(ehead, 0, sizeof(int) * (n + 1));
    memset(dfn, 0, sizeof(int) * (n + 1));
    memset(low, 0, sizeof(int) * (n + 1));
}

void dfs(const int u) {
    for (int i = 1; i < 18; ++i) {
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    }
    dfn[u] = low[u] = ++tot;
    vis[u] = true;
    for (int i = ehead[u]; i; i = e[i].next) {
        int v = e[i].to;
        if (dfn[v] == 0) {
            fa[v][0] = u;
            dep[v] = dep[u] + 1;
            dfs(v);
            low[u] = std::min(low[u], low[v]);
        } else if (vis[v]) {
            low[u] = std::min(low[u], dfn[v]);
        }
    }
    vis[u] = false;
}

void Tarjan() {
    dep[0] = 1;
    memset(fa[0], 0, sizeof(fa[0]));
    dfs(0);
}

int GetLca(int u, int v) {
    if (dep[u] > dep[v]) {
        std::swap(u, v);
    }
    for (int i = 17; i >= 0; --i)
        if (dep[fa[v][i]] >= dep[u])
            v = fa[v][i];
    if (u == v) {
        return u;
    }
    for (int i = 17; i >= 0; --i)
        if (fa[u][i] != fa[v][i])
            u = fa[u][i], v = fa[v][i];
    return fa[u][0];
}

int main() {
    // freopen("in", "r", stdin);
    int ttt;
    std::cin >> ttt;
    while (ttt--) {
        std::vector<std::pair<int,int>> diff;
        std::cin >> n >> m;
        std::cin >> cl;
        init();
        for (int i = 0, u, v; i < m; ++i) {
            std::cin >> u >> v;
            if (cl[u] == cl[v]) {
                diff.emplace_back(u, v);
            } else {
                AddEdge(u, v);
            }
        }
        Tarjan();
        bool flag = false;
        for (auto &[u, v]: diff) {
            if (dfn[u] == 0 || dfn[v] == 0) {
                continue;
            }
            int w = GetLca(u, v);
            if (w == u || w == v) {
                flag = true;
                break;
            }
            // printf("%d %d %d  %d %d %d\n", w, u, v, dfn[w], low[u], low[v]);
            if (dep[low[v]] < dep[w] || dep[low[u]] < dep[w]) {
                flag = true;
                break;
            }
        }
        if (flag) {
            std::cout << "yes\n";
        } else {
            std::cout << "no\n";
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9772kb

input:

2
4 4
LHLH
0 1
1 2
1 3
2 3
3 3
LHH
0 1
0 2
1 2

output:

yes
no

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 40ms
memory: 7956kb

input:

12385
9 29
LHLLHLHLH
0 7
1 4
4 6
2 0
4 2
6 5
8 4
7 1
2 6
7 3
7 2
8 7
1 3
5 3
0 8
4 0
0 5
8 1
8 6
3 2
0 1
1 2
6 3
2 5
6 0
3 0
5 4
4 3
7 5
7 15
LLLLLHL
5 2
6 3
3 0
0 6
4 5
1 4
6 1
0 5
3 4
5 6
1 0
2 6
0 2
4 2
0 4
6 6
LHHHHH
5 0
0 4
2 5
1 4
1 3
3 4
9 8
LHLHLLHLL
5 7
5 4
7 4
7 8
1 5
0 1
1 8
1 2
5 4
LHHHH...

output:

yes
yes
no
no
no
yes
yes
yes
no
yes
yes
no
yes
yes
no
yes
yes
yes
yes
yes
no
no
no
yes
no
no
yes
yes
no
no
yes
no
no
yes
yes
yes
yes
yes
yes
no
no
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
yes
yes
yes
yes
yes
yes
no
yes
yes
no
yes
yes
yes
yes
yes
yes
no
no
no
yes
yes
yes
no
yes
yes
...

result:

wrong answer 15th lines differ - expected: 'yes', found: 'no'