QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#361092 | #4578. Labyrinth | mc020207# | TL | 1ms | 4088kb | C++14 | 1.5kb | 2024-03-22 19:27:06 | 2024-03-22 19:27:06 |
Judging History
answer
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = (a); i <= (b);i++)
#define Rof(i, a, b) for(int i = (a); i >= (b);i--)
using namespace std;
#define maxn 200005
int n, m, s;
struct node{
int to, next;
}bian[maxn * 2];
int head[maxn];
int cnt = 1;
int dis[maxn];
int pre[maxn];
int tag[maxn];
int t = 0;
void add(int u, int v)
{
cnt++;
bian[cnt] = {v, head[u]};
head[u] = cnt;
}
void pr(int u)
{
printf("%d\n", dis[u] + 1);
stack<int> st;
while(u) st.push(u), u = pre[u];
while(!st.empty()) printf("%d ", st.top()), st.pop();
printf("\n");
}
bool dfs(int u, int fa)
{
if(tag[u] == t) return 0;
tag[u] = t;
if(dis[u]) {
printf("Possible\n");
pr(u);
pre[u] = fa, dis[u] = dis[fa] + 1;
pr(u);
return 1;
}
pre[u] = fa, dis[u] = dis[fa] + 1;
for(int i = head[u];i;i = bian[i].next) {
int v = bian[i].to;
if(v == 1) continue;
if(dfs(v, u)) return 1;
}
return 0;
}
int main()
{
scanf("%d%d%d", &n, &m, &s);
For(i, 1, m) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
}
for(int i = head[s];i;i = bian[i].next) {
int v = bian[i].to;
t = v;
if(dfs(v, s)) return 0;
}
printf("Impossible");
return 0;
}
/*
5 5 1
1 2
2 3
1 4
4 3
3 5
5 5 1
1 2
2 3
3 4
2 5
5 4
3 3 2
1 2
2 3
3 1
10 16 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3748kb
input:
5 5 1 1 2 2 3 1 4 4 3 3 5
output:
Possible 3 1 4 3 3 1 2 3
result:
ok n=5, m=5: possible
Test #2:
score: 0
Accepted
time: 1ms
memory: 4024kb
input:
5 5 1 1 2 2 3 3 4 2 5 5 4
output:
Impossible
result:
ok n=5, m=5: impossible
Test #3:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
3 3 2 1 2 2 3 3 1
output:
Impossible
result:
ok n=3, m=3: impossible
Test #4:
score: 0
Accepted
time: 1ms
memory: 4088kb
input:
5 5 1 1 2 2 3 3 4 4 5 1 5
output:
Possible 2 1 5 5 1 2 3 4 5
result:
ok n=5, m=5: possible
Test #5:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
2 2 1 1 2 2 1
output:
Impossible
result:
ok n=2, m=2: impossible
Test #6:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
2 0 2
output:
Impossible
result:
ok n=2, m=0: impossible
Test #7:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
2 1 2 2 1
output:
Impossible
result:
ok n=2, m=1: impossible
Test #8:
score: 0
Accepted
time: 1ms
memory: 3976kb
input:
2 2 2 1 2 2 1
output:
Impossible
result:
ok n=2, m=2: impossible
Test #9:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
3 0 3
output:
Impossible
result:
ok n=3, m=0: impossible
Test #10:
score: 0
Accepted
time: 0ms
memory: 4028kb
input:
3 1 3 3 2
output:
Impossible
result:
ok n=3, m=1: impossible
Test #11:
score: 0
Accepted
time: 0ms
memory: 4020kb
input:
3 2 1 1 2 3 1
output:
Impossible
result:
ok n=3, m=2: impossible
Test #12:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
3 3 3 2 3 1 2 3 1
output:
Impossible
result:
ok n=3, m=3: impossible
Test #13:
score: -100
Time Limit Exceeded
input:
3 4 2 3 2 2 1 1 3 2 3