QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#652576#4578. LabyrinthIllusionaryDominance#WA 1ms7708kbC++202.5kb2024-10-18 18:52:492024-10-18 18:52:50

Judging History

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

  • [2024-10-18 18:52:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7708kb
  • [2024-10-18 18:52:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200000 + 5;

int N, M, s;
struct Edge{
    int y, prev;
}e[MAX_N];
int elast[MAX_N], ecnt;
int son[MAX_N], bro[MAX_N], fa[MAX_N], sz[MAX_N], bs[MAX_N], dep[MAX_N], tp[MAX_N], vis[MAX_N], Tm;

void Build(int x, int y) {
    ecnt ++;
    e[ecnt].y = y;
    e[ecnt].prev = elast[x];
    elast[x] = ecnt;
}

void dfs1(int u, int fath) {
    sz[u] = 1;
    vis[u] = Tm;
    fa[u] = fath;
    dep[u] = dep[fath] + 1;
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (vis[v] != Tm) {
            dfs1(v, u);
            sz[u] += sz[v];
            bro[v] = son[u];
            son[u] = v;
            if (sz[bs[u]] < sz[v]) bs[u] = v;
        }
    }
}

void dfs2(int u, int gr) {
    tp[u] = gr;
    if (bs[u]) dfs2(bs[u], gr);
    for (int v = son[u]; v; v = bro[v]) {
        if (v != bs[u]) dfs2(v, v);
    }
}

int Lca(int u, int v) {
    while (tp[u] != tp[v]) {
        if (dep[tp[u]] < dep[tp[v]]) v = fa[tp[v]];
        else u = fa[tp[u]];
    }
    return dep[u] < dep[v] ? u : v;
}

vector <int> ans1, ans2;
bool dfs3(int u) {
    vis[u] = Tm;
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (vis[v] != Tm) {
            if (dfs3(v)) return true;
        }else if (u != s && v != s) {
            int lca = Lca(u, v);
            if (lca == s) {
                ans1.push_back(v);
                while (u) {
                    ans1.push_back(u);
                    u = fa[u];
                }
                reverse(ans1.begin(), ans1.end());
                while (v) {
                    ans2.push_back(v);
                    v = fa[v];
                }
                reverse(ans2.begin(), ans2.end());
                return true;
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    cin >> N >> M >> s;
    for (int i = 1; i <= M; i ++) {
        int x, y;
        cin >> x >> y;
        Build(x, y);
    }
    Tm = 1;
    dfs1(s, 0);
    dfs2(s, s);
    Tm = 2;
    if (dfs3(s)) {
        cout << "Possible\n";
        cout << ans1.size() << '\n';
        for (auto x : ans1) cout << x << ' ';
        cout << '\n' << ans2.size() << '\n';
        for (auto x : ans2) cout << x << ' ';
        cout << endl;
    }else {
        cout << "Impossible\n";
    }
    
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 7700kb

input:

5 5 1
1 2
2 3
1 4
4 3
3 5

output:

Possible
3
1 2 3 
3
1 4 3 

result:

ok n=5, m=5: possible

Test #2:

score: 0
Accepted
time: 1ms
memory: 5708kb

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: 1ms
memory: 7616kb

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: 7708kb

input:

5 5 1
1 2
2 3
3 4
4 5
1 5

output:

Possible
5
1 2 3 4 5 
2
1 5 

result:

ok n=5, m=5: possible

Test #5:

score: 0
Accepted
time: 1ms
memory: 5712kb

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: 5636kb

input:

2 0 2

output:

Impossible

result:

ok n=2, m=0: impossible

Test #7:

score: 0
Accepted
time: 1ms
memory: 5712kb

input:

2 1 2
2 1

output:

Impossible

result:

ok n=2, m=1: impossible

Test #8:

score: 0
Accepted
time: 1ms
memory: 5632kb

input:

2 2 2
1 2
2 1

output:

Impossible

result:

ok n=2, m=2: impossible

Test #9:

score: 0
Accepted
time: 1ms
memory: 5656kb

input:

3 0 3

output:

Impossible

result:

ok n=3, m=0: impossible

Test #10:

score: 0
Accepted
time: 1ms
memory: 5636kb

input:

3 1 3
3 2

output:

Impossible

result:

ok n=3, m=1: impossible

Test #11:

score: 0
Accepted
time: 0ms
memory: 7688kb

input:

3 2 1
1 2
3 1

output:

Impossible

result:

ok n=3, m=2: impossible

Test #12:

score: 0
Accepted
time: 0ms
memory: 5660kb

input:

3 3 3
2 3
1 2
3 1

output:

Impossible

result:

ok n=3, m=3: impossible

Test #13:

score: 0
Accepted
time: 1ms
memory: 5708kb

input:

3 4 2
3 2
2 1
1 3
2 3

output:

Possible
3
2 1 3 
2
2 3 

result:

ok n=3, m=4: possible

Test #14:

score: -100
Wrong Answer
time: 1ms
memory: 5656kb

input:

3 5 1
1 3
1 2
2 3
3 1
3 2

output:

Impossible

result:

wrong answer Jury has answer, but participant doesn't