QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#386972#8029. Traveling Merchant15owzLy1WA 47ms11852kbC++203.4kb2024-04-11 22:08:352024-04-11 22:08:36

Judging History

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

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

answer

#include <bits/stdc++.h>

const int N = 2e5 + 5;

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

int dfn_tot, bel_tot;
int dfn[N], low[N], bel[N], dep[N];
int vis[N], bridge[N];

int fa[N][20];
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;
    dfn_tot = bel_tot = 0;
    memset(ehead, 0, sizeof(int) * (n + 1));
    memset(dfn, 0, sizeof(int) * (n + 1));
    memset(bel, 0, sizeof(int) * (n + 1));
    memset(bridge, 0, sizeof(int) * (m + 1));
}

void dfs(const int u) {
    for (int i = 1; i < 20; ++i) {
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    }
    dfn[u] = low[u] = ++dfn_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]);
            if (low[v] > dfn[u]) {
                bridge[i / 2] = true;
            }
        } else if (vis[v]) {
            low[u] = std::min(low[u], dfn[v]);
        }
    }
    vis[u] = false;
}

void Shrink(const int u) {
    bel[u] = bel_tot;
    for (int i = ehead[u]; i; i = e[i].next) {
        auto v = e[i].to;
        if (bel[v] == 0 && !bridge[i / 2]) {
            Shrink(v);
        }
    }
}

void Tarjan() {
    dep[1] = 1;
    memset(fa[1], 0, sizeof(fa[1]));
    dfs(1);
    for (int i = 1; i <= n; ++i) {
        if (bel[i] == 0 && dfn[i] != 0) {
            ++bel_tot;
            Shrink(i);
        }
    }
}

int GetLca(int u, int v) {
    if (dep[u] > dep[v]) {
        std::swap(u, v);
    }
    for (int i = 19; i >= 0; --i)
        if (dep[fa[v][i]] >= dep[u])
            v = fa[v][i];
    if (u == v) {
        return u;
    }
    for (int i = 19; 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);
    // freopen("out", "w", stdout);
    int ttt;
    std::cin >> ttt;
    while (ttt--) {
        std::string cl;
        std::cin >> n >> m;
        std::cin >> cl;
        cl = "*" + cl;
        init();
        std::vector<std::pair<int,int>> diff;
        for (int i = 0, u, v; i < m; ++i) {
            std::cin >> u >> v;
            ++u, ++v;
            if (cl[u] == cl[v]) {
                diff.emplace_back(u, v);
                //for (auto &[x, y]: diff) {
                //    std::cout << x << ' ' << y << std::endl;
                //}
            } 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);
            // printf("%d %d %d  %d %d %d\n", w, u, v, fa[w][0], bel[u], bel[v]);
            if (w == u || w == v) {
                flag = true;
                break;
            }
            if (bel[fa[w][0]] == bel[u] || bel[fa[w][0]] == bel[v]) {
                flag = true;
                break;
            }
        }
        if (flag) {
            std::cout << "yes\n";
        } else {
            std::cout << "no\n";
        }
    }
    return 0;
}

詳細信息

Test #1:

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

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: 47ms
memory: 11708kb

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