QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508831#8029. Traveling MerchantyaoyanfengWA 0ms10048kbC++143.2kb2024-08-07 20:31:592024-08-07 20:32:00

Judging History

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

  • [2024-08-07 20:32:00]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:10048kb
  • [2024-08-07 20:31:59]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10, M = 2e5 + 10;
int head[N], ver[M << 1], Next[M << 1], tot;

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

struct E_DCC {
    int dfn[N], low[N], num, c[N], dcc;
    bool bridge[M << 1];

    inline void init(int n) {
        num = dcc = 0;
        memset(bridge, false, sizeof(bool) * (tot + 1));
        memset(dfn, 0, sizeof(int) * (n + 1));
        memset(c, 0, sizeof(int) * (n + 1));
        tarjan(1, 0);
        for (int i = 1; i <= n; i++)
            if (!c[i] && dfn[i])
                ++dcc, dfs(i);
    }

    void tarjan(int x, int in_edge) {
        dfn[x] = low[x] = ++num;
        for (int i = head[x]; i; i = Next[i]) {
            int y = ver[i];
            if (!dfn[y]) {
                tarjan(y, i);
                low[x] = min(low[x], low[y]);
                if (low[y] > dfn[x])
                    bridge[i] = bridge[i ^ 1] = true;
            } else if (i != (in_edge ^ 1))
                low[x] = min(low[x], dfn[y]);
        }
    }

    void dfs(int x) {
        c[x] = dcc;
        for (int i = head[x]; i; i = Next[i]) {
            int y = ver[i];
            if (c[y] || bridge[i]) continue;
            dfs(y);
        }
    }
} E;

struct LCA {
    int t, f[N][20], d[N];

    inline void init(int r, int n) {
        memset(d, 0, sizeof(int) * (n + 1));
        memset(f[1], 0, sizeof(f[1]));
        d[r] = 1, t = __lg(n), dfs(r);
    }

    void dfs(int x) {
        for (int i = head[x], y; i; i = Next[i]) {
            if (d[y = ver[i]])continue;
            d[y] = d[x] + 1, f[y][0] = x;
            for (int j = 1; j <= t; j++)
                f[y][j] = f[f[y][j - 1]][j - 1];
            dfs(y);
        }
    }

    inline int lca(int x, int y) {
        if (d[x] > d[y])swap(x, y);
        for (int i = t; i >= 0; i--)
            if (d[f[y][i]] >= d[x])
                y = f[y][i];
        if (x == y)return x;
        for (int i = t; i >= 0; i--)
            if (f[x][i] != f[y][i])
                x = f[x][i], y = f[y][i];
        return f[x][0];
    }
} L;

int T, n, m;
vector<pair<int, int>> edge;
char c[N];

inline bool check(int x, int y) {
    if (!E.dfn[x] || !E.dfn[y])return false;
    int lca = L.lca(x, y);
    if (lca == x || lca == y)return true;
    return E.c[L.f[lca][0]] == E.c[x] || E.c[L.f[lca][0]] == E.c[y];
}

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        scanf("%s", c + 1);
        memset(head, 0, sizeof(int) * (n + 1));
        tot = 1, edge.clear();
        for (int i = 1, x, y; i <= m; i++) {
            scanf("%d%d", &x, &y), x++, y++;
            if (c[x] != c[y])add(x, y), add(y, x), printf("Added %d %d\n", x, y);
            else edge.emplace_back(x, y), printf("Checked %d %d\n", x, y);
        }
        E.init(n), L.init(1, n);
        for (int i = 1; i <= n; ++i) printf("%d ", E.c[i]);
        puts("");
        bool flag = false;
        for (auto e:edge)
            if (check(e.first, e.second)) {
                flag = true;
                break;
            }
        puts(flag ? "yes" : "no");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 10048kb

input:

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

output:

Added 1 2
Added 2 3
Checked 2 4
Added 3 4
1 2 3 4 
yes
Added 1 2
Added 1 3
Checked 2 3
1 2 3 
no

result:

wrong answer 1st lines differ - expected: 'yes', found: 'Added 1 2'