QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#508200 | #8029. Traveling Merchant | yaoyanfeng | RE | 0ms | 0kb | C++14 | 1.9kb | 2024-08-07 09:01:39 | 2024-08-07 09:01:39 |
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];
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]) {
++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() {
freopen("travel.in", "r", stdin);
freopen("travel.out", "w", stdout);
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] = id[i] = dep[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), ++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;
}
Details
Tip: Click on the bar to expand more detailed information
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