QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#508181 | #8029. Traveling Merchant | Aria_Math | WA | 32ms | 35816kb | C++14 | 2.5kb | 2024-08-07 08:13:20 | 2024-08-07 08:13:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
struct Edge {
int u, v;
} E[N];
struct Node {
int nxt, v;
} G[N * 2];
int n, m, head[N], cnt = 1, seq[N], rnk[N], siz[N], fa[N];
int dfn[N], low[N], tim, in[N], sta[N], top, bcc, c[N];
char s[N];
vector<int> T[N], q[N];
void AddE(int u, int v) {
G[++cnt] = {head[u], v}, head[u] = cnt;
G[++cnt] = {head[v], u}, head[v] = cnt;
}
void Tarjan(int u, int fat) {
dfn[u] = low[u] = ++tim;
in[u] = 1, sta[++top] = u;
for(int i = head[u]; i; i = G[i].nxt) if(i != fat) {
int v = G[i].v;
if(!dfn[v]) {
Tarjan(v, i ^ 1), low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) {
int x; T[u].push_back(++bcc);
do x = sta[top--], in[x] = 0, T[bcc].push_back(x);
while(x != v);
}
}
else if(in[v]) low[u] = min(low[u], dfn[v]);
}
}
void Dfs(int u) {
seq[rnk[u] = ++rnk[0]] = u, siz[u] = 1;
for(int v : T[u]) fa[v] = u, Dfs(v), siz[u] += siz[v];
}
void Add(int pos, int k) {
while(pos <= rnk[0])
c[pos] += k, pos += pos & -pos;
}
int Sum(int pos) {
int ans = 0;
while(pos)
ans += c[pos], pos -= pos & -pos;
return ans;
}
void Mdf(int u, int k) {
Add(rnk[u], k);
Add(rnk[u] + siz[u], -k);
}
int Qry(int u) {
return Sum(rnk[u]);
}
void Solve() {
scanf("%d%d%s", &n, &m, &s[1]);
for(int i = 1; i <= m; ++i) {
int u, v; scanf("%d%d", &u, &v);
++u, ++v, E[i] = {u, v};
if(s[u] != s[v]) AddE(u, v);
}
bcc = n, Tarjan(1, 0);
Dfs(1);
bool flag = 0;
for(int i = 1; i <= m; ++i) {
int u = E[i].u, v = E[i].v;
if(s[u] == s[v]) {
if(u == 1 || v == 1) flag = 1;
q[u].push_back(v), q[v].push_back(u);
}
}
for(int u = 1; u <= n; ++u) if(dfn[u]) {
for(int x : T[u])
Mdf(x, 1);
if(fa[u]) Mdf(fa[u], 1);
for(int v : q[u])
if(Qry(v))
flag = 1;
for(int x : T[u])
Mdf(x, -1);
if(fa[u]) Mdf(fa[u], -1);
}
printf(flag ? "yes\n" : "no\n");
for(int i = 1; i <= m; ++i) E[i] = {0, 0};
for(int i = 2; i <= cnt; ++i) G[i] = {0, 0};
for(int i = 1; i <= n; ++i)
head[i] = dfn[i] = low[i] = in[i] = 0,
q[i].resize(0);
for(int i = 1; i <= rnk[0]; ++i)
seq[i] = 0;
for(int i = 1; i <= bcc; ++i)
siz[i] = fa[i] = 0,
T[i].resize(0);
cnt = 1, rnk[0] = top = tim = 0;
}
int main() {
//freopen("data.in", "r", stdin);
//freopen("code.out", "w", stdout);
int t; scanf("%d", &t);
while(t--) Solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 35340kb
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: 32ms
memory: 35816kb
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 yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes no 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 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 y...
result:
wrong answer 5th lines differ - expected: 'no', found: 'yes'