QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#122389#4511. Wonderland Chasetraining4usaco30 ✓3393ms32256kbC++144.0kb2023-07-10 12:15:052023-07-10 12:15:09

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-10 12:15:09]
  • 评测
  • 测评结果:30
  • 用时:3393ms
  • 内存:32256kb
  • [2023-07-10 12:15:05]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
 
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
 
#define int long long
using namespace std;

const int MAXN = 1e5 + 5;
const int INF = 1e18 + 7;

int n, m, a, b;
vector<int> adj[MAXN];
int par[MAXN];
int sz[MAXN];
int dists[MAXN][2];
int indeg[MAXN];

void init() {
    for(int i = 1; i <= n; ++i) {
        par[i] = i; sz[i] = 1;
    }
}

int find(int u) {
    if(par[u] == u) return u;
    return par[u] = find(par[u]);
}

bool merge(int u, int v) {
    u = find(u); v = find(v);
    if(u == v) return false;
    if(sz[u] > sz[v]) swap(u, v);
    par[u] = v; sz[v] += sz[u];
    return true;
}

void bfs(int start, int type) {
    dists[start][type] = 0;
    queue<int> q; q.push(start);
    while(q.size()) {
        int u = q.front(); q.pop();
        
        for(auto v : adj[u]) {
            if(dists[v][type] == INF) {
                dists[v][type] = dists[u][type] + 1;
                q.push(v);
            }
        }
    }
}

void solve(int t) {
    cin >> n >> m >> a >> b;
    init();
    for(int i = 0; i <= n; ++i) {
        indeg[i] = 0; dists[i][0] = dists[i][1] = INF;
        adj[i].clear();
    }
    
    for(int i = 1; i <= m; ++i) {
        int x, y; cin >> x >> y;
        adj[x].push_back(y); adj[y].push_back(x);
        ++indeg[x]; ++indeg[y];
        merge(x, y);
    }
    
    if(find(a) != find(b)) {
        cout << "Case #" << t << ": SAFE\n"; return;
    }
    
    bfs(a, 0); bfs(b, 1);
    queue<int> q;
    for(int i = 1; i <= n; ++i) {
        if(indeg[i] == 1) q.push(i);
    }
    
    while(q.size()) {
        int u = q.front(); q.pop();
        
        for(auto v : adj[u]) {
            --indeg[v];
            if(indeg[v] == 1) {
                q.push(v);
            }
        }
    }
    
    for(int i = 1; i <= n; ++i) {
        if(indeg[i] > 1 && dists[i][0] < dists[i][1]) {
            cout << "Case #" << t << ": SAFE\n"; return;
        }
    }
    
    int ans = 0;
    for(int i = 1; i <= n; ++i) {
        if(dists[i][0] < dists[i][1]) {
            ans = max(ans, dists[i][1]);    // choose furthest place for her to get captured
        }
    }
    cout << "Case #" << t << ": " << 2 * ans << endl;
}

signed main() {
    int t, tot; cin >> t; tot = t;
    while(t--) solve(tot - t);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 8
Accepted
time: 3ms
memory: 7768kb

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: 3393ms
memory: 32256kb

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