QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#84766 | #4511. Wonderland Chase | fzj2007 | 30 ✓ | 1597ms | 22816kb | C++14 | 2.5kb | 2023-03-06 18:25:11 | 2023-03-06 18:25:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace IO{
template<typename T>inline bool read(T &x){
x=0;
char ch=getchar();
bool flag=0,ret=0;
while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(),ret=1;
x=flag?-x:x;
return ret;
}
template<typename T,typename ...Args>inline bool read(T& a,Args& ...args){
return read(a)&&read(args...);
}
template<typename T>void prt(T x){
if(x>9) prt(x/10);
putchar(x%10+'0');
}
template<typename T>inline void put(T x){
if(x<0) putchar('-'),x=-x;
prt(x);
}
template<typename T>inline void put(char ch,T x){
if(x<0) putchar('-'),x=-x;
prt(x);
putchar(ch);
}
template<typename T,typename ...Args>inline void put(T a,Args ...args){
put(a);
put(args...);
}
template<typename T,typename ...Args>inline void put(const char ch,T a,Args ...args){
put(ch,a);
put(ch,args...);
}
inline void put(string s){
for(int i=0,sz=s.length();i<sz;i++) putchar(s[i]);
}
inline void put(const char* s){
for(int i=0,sz=strlen(s);i<sz;i++) putchar(s[i]);
}
}
using namespace IO;
#define N 200005
#define inf 0x3f3f3f3f
int T,n,m,A,B,deg[N];
vector<int> e[N];
struct dsu{
int fa[N];
dsu(){memset(fa,0,sizeof(fa));}
inline void prework(){
for(int i=1;i<=n;i++) fa[i]=i;
}
inline int getf(int x){
return fa[x]==x?x:fa[x]=getf(fa[x]);
}
inline void merge(int x,int y){
x=getf(x),y=getf(y);
if(x!=y) fa[x]=y;
}
}D;
int f[N],g[N];
inline void solve(){
queue<int> q;
read(n,m,A,B),D.prework();
for(int i=1;i<=n;i++) e[i].clear(),f[i]=g[i]=inf;
for(int i=1,u,v;i<=m;i++)
read(u,v),D.merge(u,v),e[u].emplace_back(v),e[v].emplace_back(u);
for(int i=1;i<=n;i++){
deg[i]=e[i].size();
if(deg[i]==1) q.push(i);
}
if(D.getf(A)!=D.getf(B)) return puts("SAFE"),void();
while(!q.empty()){
int x=q.front();
q.pop();
for(auto v:e[x])
if(--deg[v]==1) q.push(v);
}
q.push(A),f[A]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(auto v:e[x])
if(f[v]>f[x]+1) q.push(v),f[v]=f[x]+1;
}
q.push(B),g[B]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(auto v:e[x])
if(g[v]>g[x]+1) q.push(v),g[v]=g[x]+1;
}
for(int i=1;i<=n;i++)
if(deg[i]>1&&f[i]<g[i]) return puts("SAFE"),void();
int ans=0;
for(int i=1;i<=n;i++)
if(f[i]<g[i]) ans=max(ans,g[i]<<1);
put('\n',ans);
}
int main(){
read(T);
for(int test=1;test<=T;test++)
put("Case #"),put(':',test),putchar(' '),solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 8
Accepted
time: 2ms
memory: 8816kb
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: 1597ms
memory: 22816kb
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