QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508230#8029. Traveling MerchanttutamizWA 16ms21724kbC++142.6kb2024-08-07 10:09:372024-08-07 10:09:37

Judging History

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

  • [2024-08-07 20:25:21]
  • hack成功,自动添加数据
  • (/hack/771)
  • [2024-08-07 10:09:37]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:21724kb
  • [2024-08-07 10:09:37]
  • 提交

answer

#include <bits/stdc++.h>
const int N = 2e5 + 10;
std::vector<int> G[N], T[N];
int idf[N * 2], odf[N * 2], dfn[N], low[N];
int sta[N], top;
char str[N];
int scc[N * 2], cov[N], fa[N * 2];
int idx, tot, n;
struct Edge { int u, v; };
void tarjan (int u) {
    dfn[u] = low[u] = ++idx;
    sta[++top] = u;
    for (int v : G[u]) {
        if (!dfn[v]) {
            tarjan(v);
            low[u] = std::min(low[u], low[v]);
            if (low[v] >= dfn[u]) {
                ++tot;
                scc[tot] = tot;
                T[u].push_back(tot);
                auto ins = [&](int x) { 
                    ++cov[x], scc[x] = tot;
                    T[tot].push_back(x);
                };
                ins(u);
                do { ins(sta[top]); } while (sta[top--] != v);
            }
        }
        else low[u] = std::min(low[u], dfn[v]);
    }
}
void Dfs (int u) {
    idf[u] = ++idx;
    for (int v : T[u]) {
        if (v == fa[u]) continue;
        fa[v] = u;
        Dfs(v);
    }
    odf[u] = idx;
}
void solve () {
    for (int i = 1; i <= tot; ++i) idf[i] = odf[i] = 0;
    for (int i = 1; i <= n; ++i) dfn[i] = low[i] = cov[i] = 0;
    for (int i = 1; i <= tot; ++i) scc[i] = 0;
    for (int i = 1; i <= n; ++i) G[i].clear();
    for (int i = 1; i <= tot; ++i) T[i].clear();
    idx = top = 0;
    int m;
    std::cin >> n >> m;
    tot = n;
    std::vector<Edge> vec;
    std::cin >> (str + 1);
    for (int i = 1; i <= m; ++i) {
        int u, v;
        std::cin >> u >> v;
        ++u, ++v;
        if (str[u] == str[v]) vec.push_back({u, v});
        else {
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }
    tarjan(1);
    // printf("scc: %d\n", scc[1]);
    idx = 0, fa[1] = 0;
    Dfs(1);
    // printf("%d\n", (int)vec.size());
    for (Edge t : vec) {
        int u = t.u, v = t.v;
        // printf("%d %d\n", u, v);
        if (!idf[u] || !idf[v]) continue;
        int x = scc[u], y = scc[v];
        // printf("xy: %d %d\n", u, v);
        // printf("fxy: %d %d\n", fa[u], fa[v]);
        // printf("cxy: %d %d\n", cov[u], cov[v]);
        if (cov[u] > 1 && fa[u]) x = scc[fa[u]];
        if (cov[v] > 1 && fa[v]) y = scc[fa[v]];
        if (idf[x] > idf[y]) std::swap(x, y);
        // printf("xy: %d %d\n", x, y);
        if (odf[x] >= idf[y])
            { puts("yes"); return; }
    }
    puts("no");
}
int main () {
    // freopen("travel.in", "r", stdin);
    // freopen("travel.out", "w", stdout);
    std::cin.tie(0)->sync_with_stdio(0);
    int T;
    std::cin >> T;
    while (T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 16ms
memory: 20704kb

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
no
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
no
yes
no
yes
yes
yes
yes
yes
yes
no
no
no
yes
yes
yes
no
yes
yes
y...

result:

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