QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#508229 | #8029. Traveling Merchant | tutamiz | RE | 0ms | 0kb | C++14 | 2.6kb | 2024-08-07 10:08:13 | 2024-08-07 10:08:13 |
Judging History
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 * 2], 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();
}
详细
Test #1:
score: 0
Dangerous Syscalls
input:
2 4 4 LHLH 0 1 1 2 1 3 2 3 3 3 LHH 0 1 0 2 1 2