QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#690933 | #8029. Traveling Merchant | louhao088 | WA | 34ms | 5916kb | C++23 | 2.7kb | 2024-10-31 08:53:05 | 2024-10-31 08:53:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int n, m;
char s[N];
vector <int> G[N], Gv[N];
bool in[N], ans, inn[N], vis[N];
int deg[N], mindeg[N], fa[N][21];
void dfs0(int now, int father) {
inn[now] = 1; deg[now] = deg[father] + 1; fa[now][0] = father; vis[now] = 1;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i]; if (x == father || inn[x]) continue;
dfs0(x, now);
}
}
int LCA(int x, int y) {
if (deg[x] < deg[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (deg[fa[x][i]] >= deg[y]) x = fa[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void dfs(int now, int father) {
inn[now] = 1;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i]; if (x == father || inn[x]) continue;
dfs(x, now);
}
mindeg[now] = deg[now];
for (int i = 0; i < G[now].size(); i++) {
if (G[now][i]==father)continue;
int x = G[now][i];
if(deg[x]>deg[now])mindeg[now] = min(mindeg[now], mindeg[x]);
else mindeg[now]=min(mindeg[now],deg[x]);
}
}
void dfs2(int now, int father) {
inn[now] = 1; in[now] = 1;
for (int i = 0; i < G[now].size(); i++) {
int x = G[now][i]; if (x == father || inn[x]) continue;
dfs2(x, now);
}
for (int i = 0; i < Gv[now].size(); i++) {
int x = Gv[now][i]; if (!vis[x]) continue;
if (in[x]) {
ans = 1;
// printf("%d %d\n", now, x);
// printf("qwq\n");
}
else if (deg[LCA(now, x)] > min(mindeg[now], mindeg[x])) {
ans = 1;
// printf("%d %d\n", now, x);
// printf("awa\n");
}
}
in[now] = 0;
}
void slove() {
scanf("%d %d", &n, &m);
scanf("%s", s); ans = 0;
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y);
if (s[x] == s[y]) Gv[x].push_back(y), Gv[y].push_back(x);
else G[x].push_back(y), G[y].push_back(x);
}
dfs0(0, 0);
for (int i = 0; i < n; i++) inn[i] = 0;
for (int j = 1; j <= 20; j++)
for (int i = 0; i < n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
for (int i = 0; i < n; i++) mindeg[i] = n + 1;
dfs(0, 0);
for (int i = 0; i < n; i++) inn[i] = 0;
dfs2(0, 0);
if (ans) printf("yes\n");
else printf("no\n");
for (int i = 0; i < n; i++) G[i].clear(), Gv[i].clear(), inn[i] = 0, in[i] = 0, deg[i] = 0, vis[i] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j <= 20; j++) fa[i][j] = 0;
}
int main() {
int t; scanf("%d", &t);
while (t--) slove();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 5916kb
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: 0
Accepted
time: 32ms
memory: 5896kb
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:
ok 12385 lines
Test #3:
score: -100
Wrong Answer
time: 34ms
memory: 5900kb
input:
4627 11 51 LLHLLLHHLHL 0 2 0 9 10 0 4 8 3 5 4 7 6 1 1 2 2 6 5 8 5 0 7 3 4 0 2 9 2 3 6 0 8 1 5 2 6 9 0 1 10 1 1 7 5 6 1 5 1 9 4 3 9 3 3 10 6 3 10 6 0 8 7 2 8 6 4 10 8 3 7 9 10 2 8 10 8 2 1 3 5 7 3 0 7 6 0 7 10 9 9 5 1 4 5 4 10 5 9 4 7 8 15 97 LLLLLHHHHLHHLHH 8 6 13 7 10 1 7 1 4 3 8 12 14 2 14 8 14 12...
output:
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 no yes no yes yes yes yes yes 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 no yes yes yes yes yes yes yes yes yes y...
result:
wrong answer 4516th lines differ - expected: 'yes', found: 'no'