QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125661#4511. Wonderland Chasenhuang68530 ✓2002ms17592kbC++202.1kb2023-07-17 09:28:102023-07-17 09:28:13

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 09:28:13]
  • 评测
  • 测评结果:30
  • 用时:2002ms
  • 内存:17592kb
  • [2023-07-17 09:28:10]
  • 提交

answer

/**
 * @file qoj4511F1.cpp
 * @author n685
 * @brief
 * @date 2023-07-16
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
std::ifstream cin;
std::ofstream cout;
using std::cerr;
#else
using std::cin;
using std::cout;
#define cerr                                                                   \
  if (false)                                                                   \
  std::cerr
#endif

void solve() {
  int n, m, a, q;
  cin >> n >> m >> a >> q;
  a--, q--;
  std::vector<std::vector<int>> adj(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  std::vector<int> da(n, (int)1e9), dq(n, (int)1e9);
  auto bfs = [&](int s, std::vector<int> &d) {
    std::queue<int> qq;
    d[s] = 0;
    qq.push(s);
    while (!qq.empty()) {
      int node = qq.front();
      qq.pop();
      for (int i : adj[node]) {
        if (d[i] > d[node] + 1) {
          d[i] = d[node] + 1;
          qq.push(i);
        }
      }
    }
  };
  bfs(a, da);
  bfs(q, dq);

  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    for (int j : adj[i]) {
      // edge i, j
      if ((da[i] < dq[i]) ^ (da[j] < dq[j]))
        cnt++;
    }
  }
  cnt /= 2;
  if (cnt != 1) {
    cout << "SAFE\n";
    return;
  }

  // cnt = 1

  std::vector<bool> v(n);
  int md = 0;
  auto dfs = [&](auto &self, int node, int par) -> bool {
    v[node] = true;
    md = std::max(md, dq[node]);
    bool g = false;
    for (int i : adj[node]) {
      if (i == par || da[i] >= dq[i])
        continue;
      if (v[i] || self(self, i, node))
        g = true;
    }
    return g;
  };
  if (dfs(dfs, a, -1)) {
    cout << "SAFE\n";
  } else
    cout << 2 * md << '\n';
}

int main() {
#ifdef LOCAL
  cin.open("input.txt");
  cout.rdbuf()->pubsetbuf(0, 0);
  cout.open("output.txt");
#else
  cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int t;
  cin >> t;
  for (int tt = 1; tt <= t; ++tt) {
    cout << "Case #" << tt << ": ";
    solve();
  }
}

詳細信息

Test #1:

score: 8
Accepted
time: 0ms
memory: 3492kb

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: 22
Accepted
time: 2002ms
memory: 17592kb

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:

Case #1: SAFE
Case #2: SAFE
Case #3: 8
Case #4: 4
Case #5: 2
Case #6: SAFE
Case #7: 2
Case #8: 39998
Case #9: 39998
Case #10: 19192
Case #11: 2
Case #12: 99998
Case #13: 99998
Case #14: 16776
Case #15: 2
Case #16: 199998
Case #17: 199998
Case #18: 141806
Case #19: SAFE
Case #20: SAFE
Case #21: SAFE
...

result:

ok 100 lines