QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386512#8029. Traveling Merchant15owzLy1WA 46ms10004kbC++202.8kb2024-04-11 17:45:542024-04-11 17:45:54

Judging History

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

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

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], low_id[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;
    low_id[u] = u;
    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);
            if (low[u] > low[v]) {
                low[u] = low[v];
                low_id[u] = low_id[v];
            }
        } else if (vis[v]) {
            if (low[u] > dfn[v]) {
                low[u] = dfn[v];
                low_id[u] = 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_id[v]] < dep[w] || dep[low_id[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: 0ms
memory: 10004kb

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: 46ms
memory: 9708kb

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
yes
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 1493rd lines differ - expected: 'yes', found: 'no'