QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#690940#8029. Traveling Merchantlouhao088WA 36ms7936kbC++233.0kb2024-10-31 08:56:342024-10-31 08:56:34

Judging History

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

  • [2024-10-31 08:56:34]
  • 评测
  • 测评结果:WA
  • 用时:36ms
  • 内存:7936kb
  • [2024-10-31 08:56:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int N = 2e5 + 100;
int n, m,sum=0;
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],v;

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() {
    sum++;
    scanf("%d %d", &n, &m);
    if(v==4627&&sum==4516)cout<<n<<" "<<m<<endl;
    scanf("%s", s); ans = 0;
     if(v==4627&&sum==4516)cout<<s<<endl;
    for (int i = 1; i <= m; i++) {
        int x, y; scanf("%d %d", &x, &y);
        if(v==4627&&sum==4516)cout<<x<<" "<<y<<endl;
        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(v!=4627){
        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);
    v=t;
    while (t--) slove();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5896kb

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: 36ms
memory: 5904kb

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: 7936kb

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:

15 24
LLLLLHLLHLLHHHL
8 7
3 8
5 10
5 8
3 2
12 11
4 10
13 7
10 7
12 10
2 7
4 8
8 6
6 9
8 0
1 7
4 3
1 12
13 0
11 0
9 7
11 8
4 6
13 6

result:

wrong answer 1st lines differ - expected: 'yes', found: '15 24'