QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#386983 | #8029. Traveling Merchant | 15owzLy1 | WA | 76ms | 9828kb | C++20 | 3.6kb | 2024-04-11 22:20:08 | 2024-04-11 22:20:08 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 2e5 + 5;
struct edge {
int next, to;
} e[N << 1];
int ecnt, ehead[N];
int dfn_tot, bel_tot;
int dfn[N], low[N], bel[N], dep[N];
bool vis[N], bridge[N];
int fa[N][20];
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;
dfn_tot = bel_tot = 0;
memset(ehead, 0, sizeof(int) * (n + 1));
memset(dfn, 0, sizeof(int) * (n + 1));
memset(bel, 0, sizeof(int) * (n + 1));
memset(bridge, false, sizeof(bool) * (m + 1));
}
void dfs(const int u) {
for (int i = 1; i < 20; ++i) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
dfn[u] = low[u] = ++dfn_tot;
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_tot;
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_tot;
Shrink(i);
}
}
}
int GetLca(int u, int v) {
if (dep[u] > dep[v]) {
std::swap(u, v);
}
for (int i = 19; i >= 0; --i)
if (dep[fa[v][i]] >= dep[u])
v = fa[v][i];
if (u == v) {
return u;
}
for (int i = 19; 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);
// freopen("out", "w", stdout);
int ttt;
std::cin >> ttt;
while (ttt--) {
std::vector<std::pair<int,int>> diff;
std::string cl;
std::cin >> n >> m;
std::cin >> cl;
cl = "*" + cl;
init();
std::set<std::pair<int,int>> mp;
for (int i = 0, u, v; i < m; ++i) {
std::cin >> u >> v;
++u, ++v;
if (u > v) {
std::swap(u, v);
}
assert(mp.count({u, v}) == 0);
mp.insert({u, v});
if (cl[u] == cl[v]) {
diff.emplace_back(u, v);
//for (auto &[x, y]: diff) {
// std::cout << x << ' ' << y << std::endl;
//}
} 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);
// printf("%d %d %d %d %d %d\n", w, u, v, fa[w][0], bel[u], bel[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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 9828kb
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: 76ms
memory: 9764kb
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 no yes yes no yes yes yes yes yes yes yes yes no no no yes no no yes yes no no yes no no yes yes yes yes yes yes no no yes yes yes yes yes yes yes yes yes yes yes yes yes yes no yes yes yes yes yes yes no yes yes no yes yes yes yes yes yes no no no yes yes yes no yes yes...
result:
wrong answer 244th lines differ - expected: 'no', found: 'yes'