QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#508209#8029. Traveling MerchanthrhWA 22ms9976kbC++142.3kb2024-08-07 09:31:412024-08-07 09:31:41

Judging History

你现在查看的是最新测评结果

  • [2024-08-07 20:25:21]
  • hack成功,自动添加数据
  • (/hack/771)
  • [2024-08-07 09:31:41]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:9976kb
  • [2024-08-07 09:31:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 2e5 + 5, B = 17;
int T, n, m, fir[N], nxt[M << 1], son[M << 1], tot, to[N], idx, x, y, z;
int fa[N][18], dfn[N], low[N], stk[N], t, dep[N];
char s[N];
struct node{
    int x, y;
};
vector<node> vec;
inline void add(int x, int y){
    nxt[++tot] = fir[x];
    fir[x] = tot;
    son[tot] = y;
}
inline void init(){
    vec.clear();
    for(int i = 1; i <= n; i++){
        fir[i] = to[i] = 0;
        dfn[i] = 0;
    }
    for(int i = 1; i <= n; i++) stk[i] = 0;
    tot = idx = t = 0;
}
inline void tarjan(int x){
    dfn[x] = low[x] = ++idx;
    stk[++t] = x;
    dep[x] = dep[fa[x][0]] + 1;
    for(int i = 1; i <= B; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
    for(int i = fir[x]; i ; i = nxt[i]){
        if(dfn[son[i]]){
            low[x] = min(low[x], dfn[son[i]]);
            continue ;
        }
        fa[son[i]][0] = x;
        tarjan(son[i]);
        low[x] = min(low[x], low[son[i]]);
        if(low[son[i]] >= dfn[x]){
            while(stk[t + 1] ^ son[i]){
                to[stk[t]] = x;
                t--;
            }
        }
    }
}
inline int lca(int x, int y){
    if(dep[x] > dep[y]) swap(x, y);
    for(int i = B; ~i; i--){
        if(dep[fa[y][i]] >= dep[x]){
            y = fa[y][i];
        }
    }
    if(x == y) return x;
    for(int i = B; ~i; i--){
        if(fa[x][i] ^ fa[y][i]){
            x = fa[x][i], y = fa[y][i];
        }
    }
    return fa[x][0];
}
inline void solve(){
    init();
    scanf("%d%d%s", &n, &m, s + 1);
    for(int i = 1; i <= m; i++){
        scanf("%d%d", &x, &y);
        if(s[x] == s[y]){
            vec.push_back((node){x, y});
        }
        else{
            add(x, y), add(y, x);
        }
    }
    dep[1] = 1;
    tarjan(1);
    for(node e : vec){
        x = e.x, y = e.y;
        if(!dfn[x] || !dfn[y]) continue ;
        z = lca(x, y);
        // cerr << x << ' ' << y << ' ' << z << ' ' << to[x] << ' ' << to[y] << ' ' << to[z] << endl;
        if(z == x || z == y || to[z] == to[x] || to[z] == to[y]){
            puts("yes");
            return ;
        }
    }
    puts("no");
}
int main(){
    scanf("%d", &T);
    while(T--){
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9976kb

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: 22ms
memory: 9876kb

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
no
yes
no
no
yes
yes
yes
no
yes
yes
no
yes
yes
no
yes
yes
yes
yes
no
no
no
no
yes
no
no
no
yes
no
yes
yes
no
yes
yes
yes
no
yes
yes
no
yes
no
no
yes
no
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
yes
yes
yes
yes
yes
yes
yes
no
no
yes
no
yes
no
yes
yes
yes
yes
no
no
yes
yes
yes
yes
yes
yes
no
yes
...

result:

wrong answer 2nd lines differ - expected: 'yes', found: 'no'