QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#84338#4511. Wonderland Chasea_z_c8 8ms6952kbC++233.0kb2023-03-06 11:10:102023-03-06 11:10:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 11:10:11]
  • 评测
  • 测评结果:8
  • 用时:8ms
  • 内存:6952kb
  • [2023-03-06 11:10:10]
  • 提交

answer

// Problem: #4511. Wonderland Chase
// Contest:
// URL: https://qoj.ac/problem/4511
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include <bits/stdc++.h>
using namespace std;
#define gh() getchar()
inline int read() {
    char ch = gh();
    int x = 0;
    bool t = 0;
    while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
    while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
    return t ? ~(x - 1) : x;
}
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
vector<int> G[maxn];
int disa[maxn], disb[maxn];
int a, b, n, m;
queue<int> q;
void bfs1() {
    q.emplace(a);
    disa[a] = 1;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v : G[u]) {
            if (!disa[v]) {
                disa[v] = disa[u] + 1;
                q.push(v);
            }
        }
    }
}
void bfs2() {
    q.emplace(b);
    disb[b] = 1;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v : G[u]) {
            if (!disb[v]) {
                disb[v] = disb[u] + 1;
                q.push(v);
            }
        }
    }
}
bool vis[maxn], safe[maxn];
int stk[maxn], cnt;
void dfs(int u, int fa) {
    vis[u] = 1;
    stk[++cnt] = u;
    for (int v : G[u]) {
        if (v == fa)
            continue;
        if (vis[v]) {
            for (int i = cnt; i > 0; i--) {
                safe[stk[i]] = true;
                if (stk[i] == v)
                    break;
            }
        } else
            dfs(v, u);
    }
    cnt--;
}
void solve(int cas) {
    memset(disa, 0, sizeof(disa));
    memset(disb, 0, sizeof(disb));
    memset(vis, 0, sizeof(vis));
    memset(safe, 0, sizeof(safe));
    cnt = 0;
    n = read(), m = read(), a = read(), b = read();
    for (int i = 1; i <= n; i++) G[i].clear();
    for (int i = 1; i <= m; i++) {
        int u = read(), v = read();
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    bfs1();
    bfs2();
    dfs(1, 1);
    if (disb[a] == 0) {
        printf("Case #%d: SAFE\n", cas);
        return;
    }
    // for (int i = 1; i <= n; i++) printf("%d ", disa[i]);
    // printf("\n");
    // for (int i = 1; i <= n; i++) printf("%d ", disb[i]);
    // printf("\n");
    // for (int i = 1; i <= n; i++) printf("%d ", (int)safe[i]);
    // printf("\n");
    int dep = 0;
    for (int i = 1; i <= n; i++) {
        if (safe[i] && disa[i] < disb[i]) {
            printf("Case #%d: SAFE\n", cas);
            return;
        }
        if (disa[i] < disb[i]) {
            dep = max(dep, disb[i]);
        }
    }
    printf("Case #%d: %d\n", cas, (dep - 1) * 2);
}
int t;
int main() {
    scanf("%d", &t);
    for (int i = 1; i <= t; i++) {
        solve(i);
    }
}

详细

Test #1:

score: 8
Accepted
time: 8ms
memory: 6952kb

input:

100
2 1 1 2
1 2
3 3 1 2
1 3
1 2
2 3
6 6 5 1
1 4
5 6
3 4
3 6
2 3
1 2
6 6 2 4
4 5
1 4
2 3
2 5
1 6
5 6
6 6 2 3
1 3
3 4
2 6
2 5
4 5
1 5
6 5 5 3
2 5
3 4
1 2
3 6
4 6
6 5 1 6
1 4
1 2
5 6
3 5
2 4
30 29 11 5
9 21
25 28
14 20
13 30
21 28
5 18
5 23
8 22
10 30
4 8
7 24
16 26
13 26
12 18
22 23
11 16
3 11
2 17
1 ...

output:

Case #1: 2
Case #2: SAFE
Case #3: 8
Case #4: 6
Case #5: SAFE
Case #6: SAFE
Case #7: SAFE
Case #8: SAFE
Case #9: SAFE
Case #10: 8
Case #11: 4
Case #12: 2
Case #13: SAFE
Case #14: 2
Case #15: 10
Case #16: 10
Case #17: 6
Case #18: 2
Case #19: 28
Case #20: 28
Case #21: 18
Case #22: 2
Case #23: 58
Case #...

result:

ok 100 lines

Test #2:

score: 0
Time Limit Exceeded

input:

100
100000 99999 32832 52005
67463 96972
10280 86580
12146 44520
41541 86634
46936 64223
22701 46291
9093 80967
52512 77386
51062 58931
2092 55026
2096 2384
85102 92986
39914 66949
33370 68952
41576 58836
27668 33997
5843 30705
44415 57721
15188 28706
23340 55082
20335 90872
16029 80328
4656 74633
8...

output:


result: