mxqz,Labyrinth这题,WA#44
查看原帖
mxqz,Labyrinth这题,WA#44
362750
TernaryTree楼主2022/6/25 10:59
#include <bits/stdc++.h>

using namespace std;

const int maxn = 4e5 + 1;
const int maxm = 4e5 + 1;

struct edge {
	int to, next;
};

int n, m, s, t;
int head[maxn];
edge e[maxm];
int cnt;

bool vis1[maxn];
bool vis2[maxn];
int fat1[maxn];
int fat2[maxn];

void add_edge(int u, int v) {
	e[++cnt].to = v;
	e[cnt].next = head[u];
	head[u] = cnt;
}

void dfs1(int u, int fa) {
	vis1[u] = true;
	fat1[u] = fa;
	for (int i = head[u]; i; i = e[i].next) {
		int v = e[i].to;
		if (!vis1[v]) {
			dfs1(v, u);
		}
	} 
}

void dfs2(int u, int fa) {
	if (t) return;
	vis2[u] = true;
	fat2[u] = fa;
	if (vis1[u]) {
		t = u;
		return;
	}
	for (int i = head[u]; i; i = e[i].next) {
		int v = e[i].to;
		if (!vis2[v]) {
			dfs2(v, u);
			if (t) break;
		}
	} 
}

int a, b;
int sa[maxn], sb[maxn];

void ab() {
	int cur = t;
	while (cur != s) {
		sa[++a] = cur;
		cur = fat1[cur];
	}
	sa[++a] = s;
	cur = t;
	while (cur != s) {
		sb[++b] = cur;
		cur = fat2[cur];
	}
	sb[++b] = s;
}

int main() {
	cin >> n >> m >> s;
	int u, v;
	for (int i = 1; i <= m; i++) {
		cin >> u >> v;
		add_edge(u, v);
	} 
	vis1[s] = vis2[s] = true;
	dfs1(e[head[s]].to, s);
	for (int i = e[head[s]].next; i; i = e[i].next) {
		int v = e[i].to;
		dfs2(v, s);
		if (t) break;
	}
	if (!t) {
		cout << "Impossible" << endl;
	} else {
		cout << "Possible" << endl;
		ab();
		cout << a << endl;
		for (int i = 1; i <= a; i++) {
			cout << sa[a - i + 1] << " ";
		}
		cout << endl;
		cout << b << endl;
		for (int i = 1; i <= b; i++) {
			cout << sb[b - i + 1] << " ";
		}
		cout << endl;
	}
	return 0;
}
2022/6/25 10:59
加载中...