QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#386607 | #8029. Traveling Merchant | 15owzLy1 | WA | 47ms | 3584kb | C++20 | 3.1kb | 2024-04-11 18:35:00 | 2024-04-11 18:35:00 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 2e5 + 5;
struct edge {
int next, to;
} e[N << 1];
int ecnt, ehead[N];
std::string cl;
int dfn[N], low[N], bel[N], dep[N];
bool vis[N], bridge[N];
int fa[N][18];
int n, m;
inline void AddEdge(const int u, const int v) {
e[++ecnt] = {ehead[u], v}, ehead[u] = ecnt;
e[++ecnt] = {ehead[v], u}, ehead[v] = ecnt;
}
void init() {
ecnt = 1;
memset(ehead, 0, sizeof(int) * (n + 1));
memset(dfn, 0, sizeof(int) * (n + 1));
memset(low, 0, sizeof(int) * (n + 1));
memset(bel, 0, sizeof(int) * (n + 1));
memset(bridge, 0, sizeof(bool) * (m + 1));
}
void dfs(const int u) {
for (int i = 1; i < 18; ++i) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
dfn[u] = low[u] = ++dfn[0];
vis[u] = true;
for (int i = ehead[u]; i; i = e[i].next) {
int v = e[i].to;
if (dfn[v] == 0) {
fa[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
low[u] = std::min(low[u], low[v]);
if (low[v] > dfn[u]) {
bridge[i / 2] = true;
}
} else if (vis[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
vis[u] = false;
}
void shrink(const int u) {
bel[u] = bel[0];
for (int i = ehead[u]; i; i = e[i].next) {
auto v = e[i].to;
if (bel[v] == 0 && !bridge[i / 2]) {
shrink(v);
}
}
}
void tarjan() {
dep[1] = 1;
memset(fa[1], 0, sizeof(fa[1]));
dfs(1);
for (int i = 1; i <= n; ++i) {
if (bel[i] == 0 && dfn[i] != 0) {
++bel[0];
shrink(i);
}
}
}
int GetLca(int u, int v) {
if (dep[u] > dep[v]) {
std::swap(u, v);
}
for (int i = 17; i >= 0; --i)
if (dep[fa[v][i]] >= dep[u])
v = fa[v][i];
if (u == v) {
return u;
}
for (int i = 17; i >= 0; --i)
if (fa[u][i] != fa[v][i])
u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int main() {
// freopen("in", "r", stdin);
int ttt;
std::cin >> ttt;
while (ttt--) {
std::vector<std::pair<int,int>> diff;
std::cin >> n >> m;
std::cin >> cl;
init();
for (int i = 0, u, v; i < m; ++i) {
std::cin >> u >> v;
if (cl[u] == cl[v]) {
diff.emplace_back(u, v);
} else {
AddEdge(u, v);
}
}
tarjan();
bool flag = false;
for (auto &[u, v]: diff) {
if (dfn[u] == 0 || dfn[v] == 0) {
continue;
}
int w = GetLca(u, v);
if (w == u || w == v) {
flag = true;
break;
}
if (bel[fa[w][0]] == bel[u] && bel[fa[w][0]] == bel[v]) {
flag = true;
break;
}
}
if (flag) {
std::cout << "yes\n";
} else {
std::cout << "no\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3520kb
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: 47ms
memory: 3584kb
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 yes no no no yes yes yes yes yes yes yes yes no yes no yes no no yes yes yes yes yes yes yes yes yes yes yes yes yes no yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes no yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes ...
result:
wrong answer 9th lines differ - expected: 'no', found: 'yes'