QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#508199 | #8029. Traveling Merchant | yaoyanfeng | WA | 21ms | 15804kb | C++14 | 1.9kb | 2024-08-07 08:54:32 | 2024-08-07 08:54:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int N = 200200, M = 20;
int T, n, m;
char a[N];
bool res;
vector<pii> q;
vector<int> to[N];
int dfn[N], low[N], nw, st[N], top, ct;
int id[N];
bool cut[N];
int fa[N][M], dep[N];
int vis[N], cls;
void dfs(int u, int dps) {
vis[u] = cls, dep[u] = dps;
for (int v : to[u]) {
if (vis[v]) continue;
fa[v][0] = u;
for (int j = 1; j < M; ++j) fa[v][j] = fa[fa[v][j - 1]][j - 1];
dfs(v, dps + 1);
}
return;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = M - 1; i >= 0; --i) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
if (u == v) return u;
for (int i = M - 1; i >= 0; --i) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
void tarjan(int u, int rt) {
dfn[u] = low[u] = ++nw, st[++top] = u;
int s = 0;
for (int v : to[u]) {
if (!dfn[v]) {
tarjan(v, rt), low[u] = min(low[u], low[v]), ++s;
if (low[v] == dfn[u]) {
if (s > 1 || u != rt) cut[u] = 1;
++ct;
while (top && st[top] != v) {
id[st[top]] = ct;
--top;
}
id[v] = ct, --top;
}
}
else low[u] = min(low[u], dfn[v]);
}
return;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d%s", &n, &m, a + 1);
for (int i = 1; i <= n; ++i) vis[i] = dfn[i] = low[i] = cut[i] = 0, to[i].clear();
res = cls = ct = top = nw = 0, q.clear();
for (int i = 1, u, v; i <= m; ++i) {
scanf("%d%d", &u, &v);
if (a[u] != a[v]) to[u].push_back(v), to[v].push_back(u);
else q.emplace_back(u, v);
}
cls = 1, dfs(1, 1), tarjan(1, 1);
for (pii cc : q) {
int u = cc.first, v = cc.second;
if (!vis[u] || !vis[v]) continue;
int w = lca(u, v), ww = fa[w][0];
if (w == u || w == v || id[ww] && (id[ww] == id[u] || id[ww] == id[v])) {
res = 1;
break;
}
}
puts(res ? "yes" : "no");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 15356kb
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: 21ms
memory: 15804kb
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 no no no no yes yes yes no yes yes no yes yes yes no yes yes yes no no no no yes no no no yes no no yes no yes yes yes no no yes no yes no no yes no yes yes yes yes no yes yes no no yes yes yes yes yes yes yes yes yes no no yes no yes no no yes yes yes no no yes yes yes yes yes yes yes yes yes n...
result:
wrong answer 2nd lines differ - expected: 'yes', found: 'no'