QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331170 | #4511. Wonderland Chase | hos_lyric | 0 | 1784ms | 25972kb | C++14 | 4.0kb | 2024-02-18 02:54:29 | 2024-02-18 02:54:30 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
int N, M, S, T;
vector<int> A, B;
vector<vector<int>> graph;
int zeit;
vector<int> par, dis, low;
vector<int> us;
void dfs(int u, int p) {
par[u] = p;
dis[u] = low[u] = zeit++;
us.push_back(u);
for (const int v : graph[u]) if (p != v) {
if (~dis[v]) {
chmin(low[u], dis[v]);
} else {
dfs(v, u);
chmin(low[u], low[v]);
}
}
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d%d%d", &N, &M, &S, &T);
--S;
--T;
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
graph.assign(N, {});
for (int i = 0; i < M; ++i) {
graph[A[i]].push_back(B[i]);
graph[B[i]].push_back(A[i]);
}
zeit = 0;
par.assign(N, -1);
dis.assign(N, -1);
low.assign(N, -1);
us.clear();
dfs(S, -1);
// cycle edges
int cnt = 0;
vector<int> dp(N, 0);
for (int i = 0; i < M; ++i) {
int u = A[i], v = B[i];
if (~dis[u] && ~dis[v]) {
if (dis[u] > dis[v]) swap(u, v);
if (u == par[v] && dis[v] <= low[v]) {
// bridge
// cerr<<"bridge "<<u<<" "<<v<<endl;
} else {
++cnt;
++dp[u];
}
}
}
reverse(us.begin(), us.end());
for (const int u : us) {
if (~par[u]) {
dp[par[u]] += dp[u];
}
}
int z = -1;
for (const int u : us) {
if (dp[u] == cnt) {
z = u;
break;
}
}
assert(~z);
// cerr<<"dp = "<<dp<<endl;
// cerr<<"z = "<<z<<endl;
vector<int> dist[2];
for (int h = 0; h < 2; ++h) {
const int src = h ? T : S;
queue<int> que;
dist[h].assign(N, -1);
dist[h][src] = 0;
que.push(src);
for (; que.size(); ) {
const int u = que.front();
que.pop();
for (const int v : graph[u]) {
if (!~dist[h][v]) {
dist[h][v] = dist[h][u] + 1;
que.push(v);
}
}
}
// cerr<<"dist("<<src<<", *) = "<<dist[h]<<endl;
}
assert(~dist[0][z]);
int ans = 0;
if (!~dist[1][z] || dist[0][z] < dist[1][z]) {
ans = -1;
} else {
for (int u = 0; u < N; ++u) if (~dist[0][u]) {
if (!~dist[1][z]) {
ans = -1;
} else if (dist[0][u] < dist[1][u]) {
chmax(ans, 2 * dist[1][u]);
}
}
}
printf("Case #%d: ", caseId);
if (~ans) {
printf("%d", ans);
} else {
printf("SAFE");
}
puts("");
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3888kb
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: SAFE 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: SAFE Case #21: 18 Case #22: 2 Case #23: 58 C...
result:
wrong answer 10th lines differ - expected: 'Case #10: 8', found: 'Case #10: SAFE'
Test #2:
score: 0
Wrong Answer
time: 1784ms
memory: 25972kb
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: SAFE Case #14: 16776 Case #15: 2 Case #16: 199998 Case #17: SAFE Case #18: SAFE Case #19: SAFE Case #20: SAFE Case #21: SAFE Case ...
result:
wrong answer 13th lines differ - expected: 'Case #13: 99998', found: 'Case #13: SAFE'