text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e3 + 100, OO = 1e12 + 7, M = 1e9 + 7, P = 6151, sq = 500,
lg = 23;
vector<long long> v[N];
pair<long long, long long> pr[N][N];
bool is[N][N];
int32_t main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
is[1][n] = 1;
pr[1][n] = {0, 0};
queue<pair<long long, long long> > q;
q.push({1, n});
while (q.size()) {
long long u1 = q.front().first, u2 = q.front().second;
q.pop();
if (u1 == u2) continue;
for (auto v1 : v[u1]) {
for (auto v2 : v[u2]) {
if (is[v1][v2]) continue;
is[v1][v2] = true;
q.push({v1, v2});
pr[v1][v2] = {u1, u2};
}
}
}
long long a = n, b = 1;
if (!is[a][b]) return cout << -1, 0;
vector<long long> ans1, ans2;
while (a > 0 && b > 0) {
ans1.push_back(a), ans2.push_back(b);
pair<long long, long long> p = pr[a][b];
a = p.first, b = p.second;
}
cout << (long long)ans1.size() - 1 << endl;
for (auto u : ans2) cout << u << " ";
cout << endl;
for (auto u : ans1) cout << u << " ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 510;
struct node {
int x, y, t;
};
int N, M;
vector<int> adj[MAXV];
int D[MAXV][MAXV][2];
node P[MAXV][MAXV][2];
int main() {
int x, y, x2, y2, t;
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
memset(D, -1, sizeof(D));
D[1][N][0] = 0;
P[1][N][0] = (node){-1, -1, -1};
queue<node> q;
q.push((node){1, N, 0});
bool ok = 0;
while (!q.empty()) {
node u = q.front();
q.pop();
x = u.x, y = u.y;
t = u.t;
if (t == 0 && x == N && y == 1) {
ok = 1;
break;
}
if (t == 0) {
for (int i = 0; i < adj[x].size(); i++) {
x2 = adj[x][i];
if (D[x2][y][1] != -1) continue;
D[x2][y][1] = D[x][y][0];
P[x2][y][1] = u;
q.push((node){x2, y, 1});
}
} else {
for (int i = 0; i < adj[y].size(); i++) {
y2 = adj[y][i];
if (x == y2 || D[x][y2][0] != -1) continue;
D[x][y2][0] = D[x][y][1] + 1;
P[x][y2][0] = (node){P[x][y][1].x, y, 0};
q.push((node){x, y2, 0});
}
}
}
if (!ok)
puts("-1");
else {
int K = D[N][1][0];
printf("%d\n", K);
vector<int> RET1, RET2;
x = N, y = 1;
while (K--) {
RET1.push_back(x);
RET2.push_back(y);
int x2 = P[x][y][0].x, y2 = P[x][y][0].y;
x = x2;
y = y2;
}
reverse(RET1.begin(), RET1.end());
reverse(RET2.begin(), RET2.end());
printf("1");
for (int i = 0; i < RET1.size(); i++) printf(" %d", RET1[i]);
printf("\n%d", N);
for (int i = 0; i < RET2.size(); i++) printf(" %d", RET2[i]);
putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 505;
int n, m, dp[N][N];
queue<pair<int, int>> q;
vector<int> ans[2], adj[N];
pair<int, int> par[N][N], zero;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
adj[--u].push_back(--v);
adj[v].push_back(u);
}
memset(dp, 127, sizeof dp);
for (q.push({0, n - 1}), dp[0][n - 1] = 0; q.size(); q.pop()) {
auto [u, v] = q.front();
if (u == n - 1 && v == 0) {
cout << dp[u][v] << endl;
for (pair<int, int> x = {u, v}; x != zero; x = par[x.first][x.second]) {
ans[1].push_back(x.first);
ans[0].push_back(x.second);
}
for (int i = 0; i < 2; i++, cout << endl)
for (int x : ans[i]) cout << x + 1 << ' ';
return 0;
}
for (int x : adj[u])
for (int y : adj[v])
if (x ^ y && dp[u][v] + 1 < dp[x][y]) {
dp[x][y] = dp[u][v] + 1;
par[x][y] = {u, v};
q.push({x, y});
}
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500 + 4;
const int maxlog = 22;
const int mod = 1e9 + 7;
const int sq = 350;
int n, m;
int ans[maxn][maxn][2];
pair<int, int> par[maxn][maxn][2];
queue<pair<pair<int, int>, bool> > q;
vector<int> edge[maxn];
void solve() {
while (!q.empty()) {
int u = q.front().first.first;
int v = q.front().first.second;
bool b = q.front().second;
q.pop();
if (b == 0) {
for (int i = 0; i < edge[u].size(); i++) {
int k = edge[u][i];
if (!ans[k][v][1]) {
par[k][v][1] = make_pair(u, v);
ans[k][v][1] = ans[u][v][0] + 1;
q.push(make_pair(make_pair(k, v), 1));
}
}
} else {
for (int i = 0; i < edge[v].size(); i++) {
int k = edge[v][i];
if (!ans[u][k][0] && k != u) {
par[u][k][0] = make_pair(u, v);
ans[u][k][0] = ans[u][v][1] + 1;
q.push(make_pair(make_pair(u, k), 0));
}
}
}
}
}
vector<pair<int, int> > res;
void print(int x, int y, bool b) {
if (x == 0 && y == 0) return;
res.push_back(make_pair(x, y));
print(par[x][y][b].first, par[x][y][b].second, b ^ 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back(v), edge[v].push_back(u);
}
ans[1][n][0] = 1;
q.push(make_pair(make_pair(1, n), 0));
solve();
if (ans[n][1][0] / 2 == 0) {
cout << -1;
return 0;
}
cout << ans[n][1][0] / 2 << endl;
print(n, 1, 0);
reverse(res.begin(), res.end());
for (int i = 0; i < res.size(); i += 2) cout << res[i].first << " ";
cout << endl;
for (int i = 0; i < res.size(); i += 2) cout << res[i].second << " ";
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000,100000000")
using namespace std;
const int N = 505;
struct Edge {
int v, next;
} e[N * N];
struct Node {
int a, b, k;
Node() {}
Node(int _a, int _b, int _k) : a(_a), b(_b), k(_k) {}
};
int mat[N][N] = {0};
int n, m, tot, start[N], vis[N][N][2];
int dist[N][N][2], pre[N][N][2];
void add(int u, int v) {
e[tot].v = v;
e[tot].next = start[u];
start[u] = tot++;
}
void bfs() {
dist[1][n][0] = 0;
queue<Node> que;
que.push(Node(1, n, 0));
while (!que.empty()) {
Node now = que.front();
que.pop();
int u = now.k == 0 ? now.a : now.b;
for (int i = start[u]; i != -1; i = e[i].next) {
Node next = now;
int v = e[i].v;
if (now.k == 0)
next.a = v;
else if (now.k == 1)
next.b = v;
if (now.k == 1 && next.a == next.b) continue;
next.k = 1 - now.k;
if (vis[next.a][next.b][next.k]) continue;
vis[next.a][next.b][next.k] = 1;
dist[next.a][next.b][next.k] = dist[now.a][now.b][now.k] + now.k;
if (now.k == 0)
pre[next.a][next.b][next.k] = now.a;
else
pre[next.a][next.b][next.k] = now.b;
que.push(next);
}
}
}
vector<int> ret[2];
int main() {
tot = 0;
memset(start, -1, sizeof(start));
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
mat[u][v] = mat[v][u] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (mat[i][j]) {
add(i, j);
add(j, i);
}
}
}
bfs();
if (!vis[n][1][0]) {
puts("-1");
return 0;
}
printf("%d\n", dist[n][1][0]);
int a = n, b = 1, k = 0;
while (a != 1 || b != n || k != 0) {
ret[1 - k].push_back(k == 1 ? a : b);
if (k == 1)
a = pre[a][b][k];
else
b = pre[a][b][k];
k = 1 - k;
}
ret[0].push_back(1);
ret[1].push_back(n);
for (int i = ret[0].size() - 1; i >= 0; i--)
printf("%d%c", ret[0][i], i == 0 ? '\n' : ' ');
for (int i = ret[1].size() - 1; i >= 0; i--)
printf("%d%c", ret[1][i], i == 0 ? '\n' : ' ');
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MOD = 1000000007;
set<int> tab[20005];
pair<pair<int, int>, int> p[501][20005][2];
bool vis[501][20005][2];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, a, b, c;
cin >> n >> m;
vector<vector<int> > vec(n);
for (int i = 0; i < m; ++i) {
cin >> a >> b;
a--, b--;
vec[a].push_back(b), vec[b].push_back(a);
}
queue<pair<pair<int, int>, int> > q;
q.push({{0, n - 1}, 0});
p[0][n - 1][0] = {{-1, -1}, -1};
vis[0][n - 1][0] = 1;
while (!q.empty()) {
pair<pair<int, int>, int> f = q.front();
q.pop();
a = f.first.first, b = f.first.second, c = f.second;
if (a == (n - 1) && !b && !c) break;
if (!c) {
for (auto it : vec[a])
if (!vis[it][b][1 - c]) {
vis[it][b][1 - c] = 1;
p[it][b][1 - c] = f;
q.push({{it, b}, 1 - c});
}
} else {
for (auto it : vec[b])
if (it != a && !vis[a][it][1 - c]) {
vis[a][it][1 - c] = 1;
p[a][it][1 - c] = f;
q.push({{a, it}, 1 - c});
}
}
}
if (!vis[n - 1][0][0]) {
cout << -1;
return 0;
}
pair<pair<int, int>, int> node = {{n - 1, 0}, 0};
vector<int> path, path2;
path.push_back(n - 1), path2.push_back(0);
while (p[node.first.first][node.first.second][node.second].second != -1) {
node = p[node.first.first][node.first.second][node.second];
if (node.second)
path2.push_back(node.first.second);
else
path.push_back(node.first.first);
}
reverse((path).begin(), (path).end()),
reverse((path2).begin(), (path2).end());
cout << (long long)path.size() - 1 << "\n";
for (auto it : path) cout << it + 1 << " ";
cout << "\n";
for (auto it : path2) cout << it + 1 << " ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 505, INF = 1e9 + 7;
int dis[N][N];
int prvx[N][N], prvy[N][N];
vector<int> adj[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) dis[i][j] = INF;
dis[1][n] = 0;
queue<int> q;
q.push(1);
q.push(n);
while (q.size()) {
int u = q.front();
q.pop();
int v = q.front();
q.pop();
for (int x : adj[u])
for (int y : adj[v])
if (dis[x][y] == INF && x != y) {
dis[x][y] = 1 + dis[u][v];
q.push(x);
q.push(y);
prvx[x][y] = u;
prvy[x][y] = v;
}
if (dis[n][1] < INF) break;
}
if (dis[n][1] == INF)
cout << -1 << endl;
else {
cout << dis[n][1] << endl;
int ca = n, cb = 1;
vector<int> ra, rb;
while (ca != 1 || cb != n) {
ra.push_back(ca);
rb.push_back(cb);
int na = prvx[ca][cb];
int nb = prvy[ca][cb];
ca = na;
cb = nb;
assert(ca && cb);
}
ra.push_back(1);
rb.push_back(n);
reverse(ra.begin(), ra.end());
reverse(rb.begin(), rb.end());
for (int x : ra) cout << x << " ";
cout << endl;
for (int x : rb) cout << x << " ";
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, u, v, f_q, l_q, dist[501][501][2], n_st = 0, m, tag[501][501][2],
q[600001][3], ans[2001][2], n_ans = 0;
vector<int> edge[501];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
f_q = l_q = 0;
tag[1][n][0] = -1;
q[l_q][0] = 1;
q[l_q][1] = n;
q[l_q++][2] = 0;
while (!tag[n][1][0] && f_q < l_q) {
if (q[f_q][2] == 0) {
for (int i = 0; i < edge[q[f_q][0]].size(); i++) {
if (!tag[edge[q[f_q][0]][i]][q[f_q][1]][1]) {
tag[edge[q[f_q][0]][i]][q[f_q][1]][1] = q[f_q][0];
dist[edge[q[f_q][0]][i]][q[f_q][1]][1] =
dist[q[f_q][0]][q[f_q][1]][q[f_q][2]] + 1;
q[l_q][0] = edge[q[f_q][0]][i];
q[l_q][1] = q[f_q][1];
q[l_q][2] = 1;
l_q++;
}
}
} else {
for (int i = 0; i < edge[q[f_q][1]].size(); i++) {
if (!tag[q[f_q][0]][edge[q[f_q][1]][i]][0] &&
edge[q[f_q][1]][i] != q[f_q][0]) {
tag[q[f_q][0]][edge[q[f_q][1]][i]][0] = q[f_q][1];
dist[q[f_q][0]][edge[q[f_q][1]][i]][0] =
dist[q[f_q][0]][q[f_q][1]][q[f_q][2]] + 1;
q[l_q][0] = q[f_q][0];
q[l_q][1] = edge[q[f_q][1]][i];
q[l_q][2] = 0;
l_q++;
}
}
}
f_q++;
}
if (tag[n][1][0]) {
printf("%d\n", dist[n][1][0] / 2);
int now1, now2;
now1 = n;
now2 = 1;
for (int i = 0; i <= dist[n][1][0] / 2; i++) {
ans[n_ans][0] = now1;
ans[n_ans][1] = now2;
n_ans++;
now2 = tag[now1][now2][0];
now1 = tag[now1][now2][1];
}
for (int i = n_ans - 1; i >= 0; i--) printf("%d ", ans[i][0]);
printf("\n");
for (int i = n_ans - 1; i >= 0; i--) printf("%d ", ans[i][1]);
printf("\n");
} else
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:67108864")
using namespace std;
struct state {
int f, s, w;
};
bool operator<(const state& a, const state& b) {
if (a.f < b.f)
return true;
else if (a.f == b.f && a.s < b.s)
return true;
else if (a.f == b.f && a.s == b.s && a.w < b.w)
return true;
return false;
}
bool operator==(const state& a, const state& b) {
return (a.f == b.f && a.s == b.s && a.w == b.w);
}
const int INF = 1000 * 1000 * 1000;
const long double EPS = 1e-9;
const int NMAX = 505;
int n, m;
vector<int> g[NMAX];
vector<int> ans[2];
bool f = false;
void bfs() {
state init;
init.f = 0;
init.s = n - 1;
init.w = 1;
queue<state> q;
map<state, state> p;
bool used[NMAX][NMAX][2];
q.push(init);
p[init] = init;
memset(used, false, sizeof(used));
used[0][n - 1][1] = true;
while (!q.empty()) {
state t = q.front();
q.pop();
if (t.f == n - 1 && t.s == 0 && t.w == 1) {
f = true;
break;
}
int v = t.w ? t.f : t.s;
for (int i = 0; i != int(g[v].size()); ++i) {
state to = t;
if (to.w == 0)
to.s = g[v][i];
else
to.f = g[v][i];
to.w ^= 1;
if (to.w == 1 && to.f == to.s) continue;
if (!used[to.f][to.s][to.w]) {
q.push(to);
p[to] = t;
used[to.f][to.s][to.w] = true;
}
}
}
init.f = n - 1;
init.s = 0;
init.w = 1;
while (!(p[init] == init)) {
ans[init.w].push_back(init.w ? init.s : init.f);
init = p[init];
}
ans[0].push_back(0);
ans[1].push_back(n - 1);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < int(m); ++i) {
int x, y;
scanf("%d %d", &x, &y);
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
bfs();
if (!f) {
puts("-1");
return 0;
}
printf("%d\n", (int)ans[0].size() - 1);
for (int i = int(ans[0].size()) - 1; i >= 0; --i)
printf("%d ", ans[0][i] + 1);
cout << endl;
for (int i = int(ans[1].size()) - 1; i >= 0; --i)
printf("%d ", ans[1][i] + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, MOD = INF + 7;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int mod(int n) {
while (n < 0) n += MOD;
return n % MOD;
}
const int N = 5e2 + 20;
int d[N][N];
vector<int> g[N];
pair<int, int> path[N][N];
bool ok[N][N];
bool bfs(int n) {
memset(d, 63, sizeof d);
queue<pair<int, int> > q;
d[1][n] = 0;
ok[1][n] = true;
q.push(make_pair(1, n));
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
for (int bob : g[v.first]) {
for (int alex : g[v.second]) {
if (bob == alex) continue;
if (d[v.first][v.second] + 1 < d[bob][alex]) {
d[bob][alex] = d[v.first][v.second] + 1;
q.push(make_pair(bob, alex));
path[bob][alex] = v;
ok[bob][alex] = true;
if (bob == n && alex == 1) return true;
}
}
}
}
return false;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
if (!bfs(n)) {
printf("-1");
return 0;
}
vector<int> bobPath, alexPath;
pair<int, int> status = make_pair(n, 1);
while (ok[status.first][status.second]) {
bobPath.push_back(status.first);
alexPath.push_back(status.second);
status = path[status.first][status.second];
}
printf("%d\n", d[n][1]);
for (int i = bobPath.size() - 1; i >= 0; i--) printf("%d ", bobPath[i]);
printf("\n");
for (int i = alexPath.size() - 1; i >= 0; i--) printf("%d ", alexPath[i]);
printf("\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 510;
vector<long long> adj[MAXN];
vector<long long> v1, v2;
bool mark[MAXN][MAXN][3];
long long par[MAXN][MAXN][3];
queue<pair<pair<long long, long long>, bool>> q;
long long n, m;
void bfs() {
while (q.size()) {
pair<pair<long long, long long>, bool> p = q.front();
long long a = p.first.first, b = p.first.second, c = p.second;
if (!c) {
for (long long i = 0; i < adj[a].size(); i++) {
long long u = adj[a][i];
if (!mark[u][b][1]) {
mark[u][b][1] = true;
q.push(make_pair(make_pair(u, b), true));
par[u][b][1] = a;
}
}
}
if (c) {
for (long long i = 0; i < adj[b].size(); i++) {
long long u = adj[b][i];
if (a != u && !mark[a][u][0]) {
mark[a][u][0] = true;
q.push(make_pair(make_pair(a, u), false));
par[a][u][0] = b;
}
}
}
q.pop();
}
}
void pp(long long a, long long c, bool b) {
if (c == n && !b && a == 1) return;
if (!b) {
pp(a, par[a][c][b], true);
v2.push_back(c);
}
if (b) {
pp(par[a][c][b], c, false);
v1.push_back(a);
}
}
int32_t main() {
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
mark[1][n][0] = true;
pair<pair<long long, long long>, bool> p = {{1, n}, 0};
q.push(p);
bfs();
if (!mark[n][1][0]) {
cout << -1 << endl;
return 0;
}
pp(n, 1, 0);
cout << v1.size() << endl << 1 << " ";
for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " ";
cout << endl << n << " ";
for (long long i = 0; i < v2.size(); i++) cout << v2[i] << " ";
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 512, INF = 1e9;
int n, m;
vector<int> ad[maxn];
int dis[maxn][maxn];
pair<int, int> par[maxn][maxn];
vector<int> an1, an2;
queue<pair<int, int> > q;
pair<int, int> mp(int a, int b) { return make_pair(a, b); }
void BFS() {
q.push(mp(1, n));
dis[1][n] = 0;
while (q.size()) {
int v = q.front().first;
int u = q.front().second;
q.pop();
for (auto x : ad[v]) {
for (auto y : ad[u]) {
if (x == y) continue;
if (dis[x][y] > dis[v][u] + 1) {
dis[x][y] = dis[v][u] + 1;
q.push(mp(x, y));
par[x][y] = mp(v, u);
}
}
}
}
if (dis[n][1] == INF) {
cout << "-1";
exit(0);
}
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
dis[i][j] = INF;
}
}
BFS();
pair<int, int> now = mp(n, 1);
while (now.first || now.second) {
an1.push_back(now.first);
an2.push_back(now.second);
now = par[now.first][now.second];
}
cout << dis[n][1] << '\n';
for (auto x : an2) cout << x << ' ';
cout << '\n';
for (auto x : an1) cout << x << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 550 + 5, INF = 1e9 + 1, MOD = 1e9 + 7, SQ = 3;
long long int a[N];
int n, m;
std::vector<int> v[N];
std::vector<pair<pair<int, int>, int>> adj;
int d[N][N][2];
pair<pair<int, int>, int> p[N][N][2];
void find_adj(pair<pair<int, int>, int> pp) {
adj.clear();
int u = pp.first.first, w = pp.first.second, t = pp.second;
if (t) {
for (auto x : v[w]) {
if (x != u) adj.push_back({{u, x}, 0});
}
} else {
for (auto x : v[u]) {
adj.push_back({{x, w}, 1});
}
}
}
int main() {
cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
int u, w, c, tp;
cin >> n >> m;
while (m--) {
cin >> u >> w;
v[u].push_back(w);
v[w].push_back(u);
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) d[i][j][0] = INF, d[i][j][1] = INF;
queue<pair<pair<int, int>, int>> q;
q.push({{1, n}, 0});
d[1][n][0] = 0;
while (q.size()) {
auto u = q.front();
q.pop();
find_adj(u);
for (auto x : adj) {
if (d[x.first.first][x.first.second][x.second] >
d[u.first.first][u.first.second][u.second] + 1) {
d[x.first.first][x.first.second][x.second] =
d[u.first.first][u.first.second][u.second] + 1;
p[x.first.first][x.first.second][x.second] = u;
q.push(x);
}
}
}
if (d[n][1][0] == INF) return cout << -1 << '\n', 0;
pair<pair<int, int>, int> x = {{n, 1}, 0};
std::vector<int> va, vb;
va.push_back(1);
vb.push_back(n);
while (x != make_pair(pair<int, int>{1, n}, 0)) {
x = p[x.first.first][x.first.second][x.second];
if (x.second)
va.push_back(x.first.second);
else
vb.push_back(x.first.first);
}
cout << d[n][1][0] / 2 << '\n';
for (auto x : va) cout << x << ' ';
cout << '\n';
for (auto x : vb) cout << x << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool cmp_pair_F_2(pair<int, int> x, pair<int, int> y) {
return x.first > y.first;
}
bool cmp_pair_S_2(pair<int, int> x, pair<int, int> y) {
return x.second > y.second;
}
bool cmp_pair_S_1(pair<int, int> x, pair<int, int> y) {
return x.second < y.second;
}
bool cmp(int x, int y) { return x > y; }
int n, m;
const int N = 510;
vector<int> vv;
vector<vector<int>> adj(N, vv);
pair<int, int> par[2][N][N];
int mark[2][N];
int is[2][N][N];
int two_bfs(int v1, int v2, bool turn) {
int v[2] = {v1, v2};
queue<pair<bool, pair<int, int>>> q;
q.push(make_pair(turn, make_pair(v1, v2)));
while (q.size()) {
turn = q.front().first;
v[0] = q.front().second.first;
v[1] = q.front().second.second;
q.pop();
is[turn][v[0] - 1][v[1] - 1] = 1;
int u = v[turn];
if (v[0] == n && v[1] == 1 && !turn) {
vector<int> ans1, ans2;
while (v[0] != 1 || v[1] != n) {
ans1.push_back(v[0]);
ans2.push_back(v[1]);
int u1 = v[0];
int u2 = v[1];
v[0] = par[turn][u1 - 1][u2 - 1].first;
v[1] = par[turn][u1 - 1][u2 - 1].second;
turn = !turn;
}
ans1.push_back(1);
ans2.push_back(n);
ans1.resize(unique(ans1.begin(), ans1.end()) - ans1.begin());
ans2.resize(unique(ans2.begin(), ans2.end()) - ans2.begin());
int t = ans1.size();
cout << t - 1 << endl;
for (int i = 0; i < t; i++) cout << ans2[i] << ' ';
cout << endl;
for (int i = 0; i < t; i++) cout << ans1[i] << ' ';
exit(0);
}
int cc = 0;
for (auto i : adj[u - 1]) {
if (turn == 1)
if (!is[!turn][v[0] - 1][i - 1]) {
par[!turn][v[0] - 1][i - 1] = make_pair(v[0], v[1]);
q.push(make_pair(!turn, make_pair(v[0], i)));
cc = 1;
is[!turn][v[0] - 1][i - 1] = 1;
} else
;
else if (!is[!turn][i - 1][v[1] - 1]) {
par[!turn][i - 1][v[1] - 1] = make_pair(v[0], v[1]);
q.push(make_pair(!turn, make_pair(i, v[1])));
cc = 1;
is[!turn][i - 1][v[1] - 1] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i, j;
cin >> n >> m;
int x, y;
for (i = 0; i < m; i++) {
cin >> x >> y, adj[x - 1].push_back(y), adj[y - 1].push_back(x);
}
par[0][1][n] = make_pair(0, 0);
par[1][n][1] = make_pair(0, 0);
for (i = 0; i < n; i++) is[0][i][i] = 1;
two_bfs(1, n, 0);
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 510;
vector<int> way[MAXN];
struct node {
int x, y, s;
node(int _x = 0, int _y = 0, int _s = 0) {
x = _x;
y = _y;
s = _s;
}
};
node q[MAXN * MAXN * 10];
bool used[MAXN][MAXN][2];
int dad[MAXN * MAXN * 10];
vector<int> res[2];
int main() {
int n, m, at = 0, al = 0;
scanf("%d %d", &n, &m);
for (int i = 1, a, b; i <= m; i++) {
scanf(" %d %d", &a, &b);
way[a].push_back(b);
way[b].push_back(a);
}
q[++at] = node(1, n, 0);
while (at != al) {
node tmp = q[++al];
if (tmp.s == 0 && tmp.x == tmp.y) continue;
if (tmp.s == 0 && tmp.x == n && tmp.y == 1) {
for (int i = al; i; i = dad[i]) {
if (q[i].s == 0) res[0].push_back(q[i].x);
if (q[i].s == 1) res[1].push_back(q[i].y);
}
cout << ((int)res[0].size()) - 1 << endl;
for (int i = ((int)res[0].size()) - 1; i >= 0; i--)
printf("%d ", res[0][i]);
puts("");
for (int i = ((int)res[1].size()) - 1; i >= 0; i--)
printf("%d ", res[1][i]);
printf("1");
puts("");
return 0;
}
if (tmp.s == 0) {
for (int i = 0; i < ((int)way[tmp.x].size()); i++)
if (!used[way[tmp.x][i]][tmp.y][1]) {
used[way[tmp.x][i]][tmp.y][1] = true;
q[++at] = node(way[tmp.x][i], tmp.y, 1);
dad[at] = al;
}
} else {
for (int i = 0; i < ((int)way[tmp.y].size()); i++)
if (!used[tmp.x][way[tmp.y][i]][0] && tmp.x != way[tmp.y][i]) {
used[tmp.x][way[tmp.y][i]][0] = true;
q[++at] = node(tmp.x, way[tmp.y][i], 0);
dad[at] = al;
}
}
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
const int maxn = 510;
int dp[maxn][maxn][2];
pair<int, int> pr[maxn][maxn][2];
vector<int> a[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int fr, to;
scanf("%d%d", &fr, &to);
fr--;
to--;
a[fr].push_back(to);
a[to].push_back(fr);
}
memset(dp, -1, sizeof(dp));
dp[0][n - 1][0] = 0;
pr[0][n - 1][0] = make_pair(-1, -1);
queue<pair<pair<int, int>, int>> q;
q.push(make_pair(make_pair(0, n - 1), 0));
while (int((q).size())) {
int ci = q.front().first.first;
int cj = q.front().first.second;
int ct = q.front().second;
q.pop();
if (ct == 0) {
for (int i = 0; i < int((a[ci]).size()); ++i) {
int to = a[ci][i];
if (dp[to][cj][1 - ct] == -1) {
dp[to][cj][1 - ct] = dp[ci][cj][ct] + 1;
pr[to][cj][1 - ct] = make_pair(ci, cj);
q.push(make_pair(make_pair(to, cj), 1 - ct));
}
}
} else {
for (int i = 0; i < int((a[cj]).size()); ++i) {
int to = a[cj][i];
if (to != ci && dp[ci][to][1 - ct] == -1) {
dp[ci][to][1 - ct] = dp[ci][cj][ct] + 1;
pr[ci][to][1 - ct] = make_pair(ci, cj);
q.push(make_pair(make_pair(ci, to), 1 - ct));
}
}
}
}
if (dp[n - 1][0][0] == -1)
printf("-1\n");
else {
printf("%d\n", dp[n - 1][0][0] / 2);
vector<int> ans1, ans2;
int ci = n - 1;
int cj = 0;
int ct = 0;
while (ci != -1) {
ans1.push_back(ci);
ans2.push_back(cj);
int ti = pr[ci][cj][ct].first;
int tj = pr[ci][cj][ct].second;
ci = ti;
cj = tj;
ct = 1 - ct;
}
reverse(ans1.begin(), ans1.end());
ans1.erase(unique(ans1.begin(), ans1.end()), ans1.end());
reverse(ans2.begin(), ans2.end());
ans2.erase(unique(ans2.begin(), ans2.end()), ans2.end());
for (int i = 0; i < int((ans1).size()); ++i) printf("%d ", ans1[i] + 1);
printf("\n");
for (int i = 0; i < int((ans2).size()); ++i) printf("%d ", ans2[i] + 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600;
int adj[maxn][maxn];
int n, m, tot = 0;
int q[maxn * maxn][3], A[maxn * maxn], B[maxn * maxn];
int pre[maxn][maxn][3], u[maxn][maxn], deg[maxn];
int print(int x, int y) {
if (pre[x][y][1]) print(pre[x][y][1], pre[x][y][2]);
A[++tot] = x;
B[tot] = y;
}
int main() {
cin >> n >> m;
memset(deg, 0, sizeof(deg));
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
adj[x][++deg[x]] = y;
adj[y][++deg[y]] = x;
}
memset(u, 0, sizeof(u));
int f = 0, r = 1, flag = 0;
q[1][1] = 1;
q[1][2] = n;
pre[1][n][1] = 0;
u[1][n] = 1;
while (f < r) {
f++;
int x = q[f][1], y = q[f][2];
for (int i = 1; i <= deg[x]; i++) {
for (int j = 1; j <= deg[y]; j++) {
int X = adj[x][i], Y = adj[y][j];
if (!u[X][Y] && X != Y) {
u[X][Y] = 1;
pre[X][Y][1] = x;
pre[X][Y][2] = y;
r++;
q[r][1] = X;
q[r][2] = Y;
if (X == n && Y == 1) {
flag = 1;
break;
}
}
}
if (flag) break;
}
if (flag) break;
}
if (!u[n][1])
cout << -1 << endl;
else {
print(n, 1);
cout << tot - 1 << endl;
for (int i = 1; i <= tot; i++) cout << A[i] << " ";
cout << endl;
for (int i = 1; i <= tot; i++) cout << B[i] << " ";
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 500 + 7;
const long long mod = 1e9 + 7;
vector<long long> adj[N];
long long d[N][N], used[N][N];
pair<long long, long long> p[N][N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (long long i = 0; i < N; i++) {
for (long long j = 0; j < N; j++) {
d[i][j] = 1e9;
}
}
queue<pair<long long, long long> > q;
q.push({1, n});
d[1][n] = 0;
while (!q.empty()) {
auto f = q.front();
q.pop();
if (d[n][1] != 1e9) {
break;
}
for (auto x : adj[f.first]) {
if (used[x][f.second]) {
continue;
}
used[x][f.second] = 1;
for (auto y : adj[f.second]) {
if (d[x][y] != 1e9 || x == y) {
continue;
}
d[x][y] = d[f.first][f.second] + 1;
p[x][y] = f;
q.push({x, y});
}
}
}
if (d[n][1] == 1e9) {
cout << -1;
return 0;
}
pair<long long, long long> st = {n, 1};
vector<long long> vc1, vc2;
while (1) {
if (st.first == 1 && st.second == n) {
break;
}
vc1.push_back(st.first);
vc2.push_back(st.second);
st = p[st.first][st.second];
}
vc1.push_back(1);
vc2.push_back(n);
cout << (long long)vc1.size() - 1 << '\n';
reverse(vc1.begin(), vc1.end());
reverse(vc2.begin(), vc2.end());
for (auto i : vc1) {
cout << i << ' ';
}
cout << '\n';
for (auto i : vc2) {
cout << i << ' ';
}
cout << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > L;
vector<vector<int> > L2;
queue<int> q;
stack<int> s;
int num[501][501][2];
bool odw[500 * 500 * 2 + 7];
int odl[500 * 500 * 2 + 7];
int R[500 * 500 * 2 + 7];
int t[501 * 500 * 2], t2[501 * 500 * 2];
void BFS(int a) {
q.push(a);
odw[a] = 1;
while (!q.empty()) {
a = q.front();
q.pop();
for (int i = 0; i < L2[a].size(); i++) {
int x = L2[a][i];
if (!odw[x]) {
odw[x] = 1;
odl[x] = odl[a] + 1;
q.push(x);
R[x] = a;
}
}
}
}
int main(void) {
int n, m, x, c, d;
scanf("%d %d", &n, &m);
L.resize(n + 1);
L2.resize(n * n * 2 + 7);
for (int i = 0; i < m; i++) {
scanf("%d %d", &c, &d);
L[c].push_back(d);
L[d].push_back(c);
}
int licznik = 1;
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= n; a++) {
for (int b = 0; b <= 1; b++) {
num[i][a][b] = licznik;
t[licznik] = i;
t2[licznik] = a;
licznik++;
}
}
}
for (int i = 1; i <= n; i++) {
odw[num[i][i][0]] = 1;
}
for (int i = 1; i <= n; i++) {
for (int a = 0; a < L[i].size(); a++) {
for (int b = 1; b <= n; b++) {
c = num[i][b][0];
d = num[L[i][a]][b][1];
L2[c].push_back(d);
c = num[b][i][1];
d = num[b][L[i][a]][0];
L2[c].push_back(d);
}
}
}
BFS(num[1][n][0]);
if (odl[num[n][1][0]]) {
x = odl[num[n][1][0]];
printf("%d\n", x / 2);
int b = num[n][1][0];
printf("1 ");
for (int i = 0; i < x / 2; i++) {
s.push(t[b]);
b = R[b];
b = R[b];
}
while (!s.empty()) {
printf("%d ", s.top());
s.pop();
}
printf("\n");
b = num[n][1][0];
printf("%d ", n);
for (int i = 0; i < x / 2; i++) {
s.push(t2[b]);
b = R[b];
b = R[b];
}
while (!s.empty()) {
printf("%d ", s.top());
s.pop();
}
} else {
printf("-1");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e2;
pair<pair<int, int>, bool> pr[maxn][maxn][2];
queue<pair<pair<int, int>, bool> > q;
vector<int> neighbor[maxn];
void bfs(int s, int t) {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
for (int k = 0; k < 2; k++)
pr[i][j][k] = make_pair(make_pair(-1, -1), false);
q.push(make_pair(make_pair(s, t), 0));
while (!q.empty()) {
int v = q.front().first.second, u = q.front().first.first;
bool b = q.front().second;
q.pop();
if (!b) {
for (int i = 0; i < neighbor[u].size(); i++)
if (pr[neighbor[u][i]][v][!b].first.first == -1) {
pr[neighbor[u][i]][v][!b] = make_pair(make_pair(u, v), b);
q.push(make_pair(make_pair(neighbor[u][i], v), !b));
}
} else {
for (int i = 0; i < neighbor[v].size(); i++)
if (pr[u][neighbor[v][i]][!b].first.first == -1 &&
neighbor[v][i] != u && (u != s || neighbor[v][i] != t)) {
pr[u][neighbor[v][i]][!b] = make_pair(make_pair(u, v), b);
q.push(make_pair(make_pair(u, neighbor[v][i]), !b));
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
neighbor[u - 1].push_back(v - 1);
neighbor[v - 1].push_back(u - 1);
}
bfs(0, n - 1);
if (pr[n - 1][0][0].first.first == -1)
cout << -1;
else {
vector<int> fi, se;
pair<pair<int, int>, bool> kl = make_pair(make_pair(n - 1, 0), 0);
while (kl.first.first != -1) {
if (kl.second)
se.push_back(kl.first.second);
else
fi.push_back(kl.first.first);
kl = pr[kl.first.first][kl.first.second][kl.second];
}
cout << fi.size() - 1 << endl;
for (int i = fi.size() - 1; i > -1; i--) cout << fi[i] + 1 << " ";
cout << endl;
for (int i = se.size() - 1; i > -1; i--) cout << se[i] + 1 << " ";
cout << 1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 510 + 5;
long long mark[maxn][maxn][3], h[maxn][maxn][3];
pair<pair<long long, long long>, long long> par[maxn][maxn][3];
queue<pair<pair<long long, long long>, long long> > q;
vector<long long> yal[maxn], ans1, ans2;
void bfs(long long n) {
q.push(pair<pair<long long, long long>, long long>(
pair<long long, long long>(1, n), 1));
mark[1][n][1] = 1;
while (q.size()) {
pair<pair<long long, long long>, long long> p = q.front();
q.pop();
if (p == pair<pair<long long, long long>, long long>(
pair<long long, long long>(n, 1), 1))
return;
long long u = p.first.first, v = p.first.second, t = p.second;
if (t == 1) {
for (long long i = 0; i < yal[u].size(); i++) {
long long next = yal[u][i];
if (mark[next][v][2]) continue;
q.push(pair<pair<long long, long long>, long long>(
pair<long long, long long>(next, v), 2));
mark[next][v][2] = 1;
h[next][v][2] = h[u][v][t] + 1;
par[next][v][2] = pair<pair<long long, long long>, long long>(
pair<long long, long long>(u, v), t);
}
} else {
for (long long i = 0; i < yal[v].size(); i++) {
long long next = yal[v][i];
if (mark[u][next][1] or next == u) continue;
q.push(pair<pair<long long, long long>, long long>(
pair<long long, long long>(u, next), 1));
mark[u][next][1] = 1;
h[u][next][1] = h[u][v][t] + 1;
par[u][next][1] = pair<pair<long long, long long>, long long>(
pair<long long, long long>(u, v), t);
}
}
}
return;
}
int32_t main() {
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
long long u, v;
cin >> u >> v;
yal[u].push_back(v);
yal[v].push_back(u);
}
bfs(n);
long long u = n, v = 1, t = 1;
long long ans = h[u][v][t] / 2;
cout << (ans ? ans : -1) << endl;
if (!ans) return 0;
do {
if (t == 1)
ans2.push_back(v);
else
ans1.push_back(u);
pair<pair<long long, long long>, long long> p = par[u][v][t];
u = p.first.first;
v = p.first.second;
t = p.second;
} while (par[u][v][t] != pair<pair<long long, long long>, long long>(
pair<long long, long long>(0, 0), 0));
ans1.push_back(1);
ans2.push_back(n);
for (long long i = ans; i >= 0; i--) cout << ans1[i] << ' ';
cout << endl;
for (long long i = ans; i >= 0; i--) cout << ans2[i] << ' ';
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long INFLL = 1e18 + 1;
const int MAX = 200001;
const long long MOD = 1000000007;
long long inq(long long k, long long q) {
if (q == 0) return 1;
long long l = inq(k, q / 2);
if (q % 2 == 0)
return l * l % MOD;
else
return (l * l) % MOD * k % MOD;
}
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
return gcd(b, a % b);
}
long long cubr(long long a) {
long long l = -1, r = 1e6 + 2;
while (l < r - 1) {
long long mid = (l + r) / 2;
if (mid * mid * mid > a)
r = mid;
else
l = mid;
}
return l;
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long min(long long a, long long b) { return -1 * max(-a, -b); }
long long possible(long long q) {
if (q == INF) return -1;
return q;
}
bool correct(int x, int xx) {
if (x < 0) return 0;
if (x >= xx) return 0;
return 1;
}
long long dsumm(long long x, long long k) {
long long y = 1;
long long z = 0;
for (int i = 0; y < 1e18; i++) {
z += x / y % k;
y *= k;
}
return z;
}
long long dcount(long long x) {
long long y = 1;
long long z = 0;
int c[100];
for (int i = 0; i < 10; i++) c[i] = 0;
for (int i = 0; x > 0; i++) {
if (c[x / y % 10] == 0) z++;
c[x / y % 10] = 1;
x /= 10;
}
return z;
}
long long lg10(long long x) {
if (x == 0) return 0;
return lg10(x / 10) + 1;
}
long long n, m;
int a[501][501];
vector<int> graph[501];
int dp[501][501][2];
pair<pair<int, int>, int> prev1234[501][501][2];
int main() {
memset((dp), -1, sizeof(dp));
;
cin >> n >> m;
for (int(i) = 0; (i) != (m); i++) {
int x, y;
cin >> x >> y;
a[x][y] = 1;
a[y][x] = 1;
graph[x].push_back(y);
graph[y].push_back(x);
}
dp[1][n][0] = 0;
vector<pair<pair<int, int>, int>> q;
q.push_back({{1, n}, 0});
for (int i = 0; i < q.size(); i++) {
int x = q[i].first.first;
int y = q[i].first.second;
int now = q[i].second;
if (now == 0) {
for (int j = 0; j < graph[x].size(); j++) {
int u = graph[x][j];
if (dp[u][y][1] == -1) {
dp[u][y][1] = dp[x][y][0] + 1;
prev1234[u][y][1] = {{x, y}, 0};
q.push_back({{u, y}, 1});
}
}
}
if (now == 1) {
for (int j = 0; j < graph[y].size(); j++) {
int u = graph[y][j];
if (u != x && dp[x][u][0] == -1) {
dp[x][u][0] = dp[x][y][1] + 1;
prev1234[x][u][0] = {{x, y}, 1};
q.push_back({{x, u}, 0});
}
}
}
}
if (dp[n][1][0] == -1) {
cout << -1;
return 0;
}
cout << dp[n][1][0] / 2 << endl;
pair<pair<int, int>, int> xy = {{n, 1}, 0};
pair<pair<int, int>, int> ab = {{1, n}, 0};
vector<int> ans1, ans2;
while (xy != ab) {
int x = xy.first.first;
int y = xy.first.second;
int now = xy.second;
if (now == 0) {
ans1.push_back(x);
} else {
ans2.push_back(y);
}
xy = prev1234[x][y][now];
}
int x = xy.first.first;
int y = xy.first.second;
ans1.push_back(x);
reverse((ans1).begin(), (ans1).end());
reverse((ans2).begin(), (ans2).end());
ans2.push_back(1);
for (int(i) = 0; (i) != (ans1.size()); i++) {
cout << ans1[i] << " ";
}
cout << endl;
for (int(i) = 0; (i) != (ans2.size()); i++) {
cout << ans2[i] << " ";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
vector<int> adj[600];
int N, M;
int rest;
int best[600][600];
pair<int, int> last[600][600];
bool vis[600];
bool used[600][600];
bool rek(int n) {
if (n == N) return true;
if (vis[n]) return false;
vis[n] = true;
for (int i = (0); i < (int((adj[n]).size())); i++)
if (rek(adj[n][i])) return true;
return false;
}
bool possible() {
memset(vis, false, sizeof(vis));
rest = N;
return rek(1);
}
int main() {
scanf("%d%d", &N, &M);
for (int i = (0); i < (N + 1); i++) adj[i].clear();
memset(used, false, sizeof(used));
for (int i = (0); i < (M); i++) {
int a, b;
scanf("%d%d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(best, -1, sizeof(best));
best[1][N] = 0;
queue<pair<int, int> > ac = queue<pair<int, int> >();
if (possible()) ac.push(pair<int, int>(1, N));
pair<int, int> n;
int t1, t2, n1, n2;
while (!ac.empty()) {
pair<int, int> n = ac.front();
n1 = n.first;
n2 = n.second;
ac.pop();
for (int i1 = (0); i1 < (int((adj[n1]).size())); i1++) {
t1 = adj[n1][i1];
if (used[n2][t1]) continue;
used[n2][t1] = true;
for (int i2 = (0); i2 < (int((adj[n2]).size())); i2++) {
t2 = adj[n2][i2];
if (t1 != t2) {
if (best[t1][t2] == -1) {
ac.push(pair<int, int>(t1, t2));
best[t1][t2] = 1 + best[n1][n2];
last[t1][t2] = n;
if (t1 == N && t2 == 1) goto endCase;
}
}
}
}
}
endCase:
if (best[N][1] == -1) {
printf("-1\n");
} else {
printf("%d\n", best[N][1]);
stack<int> st[2];
for (int i = (0); i < (2); i++) st[i] = stack<int>();
int c1 = N, c2 = 1;
st[0].push(c1);
st[1].push(c2);
while (c1 != 1 || c2 != N) {
pair<int, int> pre = last[c1][c2];
c1 = pre.first;
c2 = pre.second;
st[0].push(c1);
st[1].push(c2);
}
for (int i = (0); i < (2); i++) {
printf("%d", st[i].top());
st[i].pop();
while (!st[i].empty()) {
printf(" %d", st[i].top());
st[i].pop();
}
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int pw(long long int a, long long int b, long long int mod) {
if (!b) return 1;
if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod;
return pw(a * a % mod, b / 2, mod) % mod;
}
const long long int MAXN = 510;
const int INF = 1e9;
const long long int MOD = 1e9 + 7;
struct node {
int u, v, turn;
};
int n, m, dist[MAXN][MAXN][2];
vector<int> adj[MAXN], A, B;
node par[MAXN][MAXN][2];
queue<node> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < MAXN; i++) {
for (int j = 0; j < MAXN; j++) {
dist[i][j][0] = dist[i][j][1] = INF;
}
}
q.push(node{1, n, 0});
dist[1][n][0] = 0;
par[1][n][0] = {-1, -1, -1};
while (!q.empty()) {
node u = q.front();
q.pop();
int x = (u.turn == 0 ? u.u : u.v);
for (int y : adj[x]) {
node v = (u.turn == 0 ? node{y, u.v, 1} : node{u.u, y, 0});
if (u.turn == 1 && v.u == v.v) continue;
if (dist[v.u][v.v][v.turn] < INF) continue;
dist[v.u][v.v][v.turn] = dist[u.u][u.v][u.turn] + 1;
par[v.u][v.v][v.turn] = (u.turn == 0 ? u : par[u.u][u.v][u.turn]);
q.push(v);
}
}
if (dist[n][1][0] == INF) return cout << -1 << '\n', 0;
node u = {n, 1, 0};
while (u.u != -1) {
A.push_back(u.u);
B.push_back(u.v);
u = par[u.u][u.v][u.turn];
}
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
cout << A.size() - 1 << '\n';
for (int u : A) cout << u << ' ';
cout << '\n';
for (int u : B) cout << u << ' ';
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, MOD = INF + 7;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int mod(int n) {
while (n < 0) n += MOD;
return n % MOD;
}
const int N = 5e2 + 20;
int d[N][N];
vector<int> g[N];
pair<int, int> path[N][N];
bool ok[N][N];
bool bfs(int n) {
memset(d, 63, sizeof d);
queue<pair<int, int> > q;
d[1][n] = 0;
ok[1][n] = true;
q.push(make_pair(1, n));
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
for (int bob : g[v.first]) {
for (int alex : g[v.second]) {
if (bob == alex) continue;
if (d[v.first][v.second] + 1 < d[bob][alex]) {
d[bob][alex] = d[v.first][v.second] + 1;
q.push(make_pair(bob, alex));
path[bob][alex] = v;
ok[bob][alex] = true;
if (bob == n && alex == 1) return true;
}
}
}
}
return false;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (!bfs(n)) return cout << -1 << "\n", 0;
vector<int> bobPath, alexPath;
pair<int, int> status = make_pair(n, 1);
while (ok[status.first][status.second]) {
bobPath.push_back(status.first);
alexPath.push_back(status.second);
status = path[status.first][status.second];
}
cout << d[n][1] << "\n";
for (int i = bobPath.size() - 1; i >= 0; i--) cout << bobPath[i] << " ";
cout << "\n";
for (int i = alexPath.size() - 1; i >= 0; i--) cout << alexPath[i] << " ";
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename input_type>
input_type get() {
input_type input;
cin >> input;
return input;
}
const int max_sz = (5e2 + 5), inf = 2e9;
int dstnc[max_sz][max_sz];
pair<int, int> sz, prnt[max_sz][max_sz];
vector<int> nghbr[max_sz];
void BFS() {
queue<pair<int, int>> bfs_queue;
for (int i = 0; i < max_sz; i++) {
for (int j = 0; j < max_sz; j++) {
dstnc[i][j] = inf;
}
}
bfs_queue.push({0, sz.first - 1});
dstnc[0][sz.first - 1] = 0;
while (not bfs_queue.empty()) {
pair<int, int> vrtx = bfs_queue.front();
for (auto ngh1 : nghbr[vrtx.first]) {
for (auto ngh2 : nghbr[vrtx.second]) {
if ((ngh1 != ngh2) and
(dstnc[ngh1][ngh2] > (dstnc[vrtx.first][vrtx.second] + 1))) {
dstnc[ngh1][ngh2] = (dstnc[vrtx.first][vrtx.second] + 1);
prnt[ngh1][ngh2] = {vrtx.first, vrtx.second};
bfs_queue.push({ngh1, ngh2});
}
}
}
bfs_queue.pop();
}
return;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
sz = {get<int>(), get<int>()};
for (int i = 0; i < sz.second; i++) {
pair<int, int> endpnt = {(get<int>() - 1), (get<int>() - 1)};
nghbr[endpnt.first].push_back(endpnt.second);
nghbr[endpnt.second].push_back(endpnt.first);
}
BFS();
if (dstnc[sz.first - 1][0] == inf) {
cout << -1 << "\n";
} else {
pair<int, int> vrtx = {(sz.first - 1), 0};
pair<vector<int>, vector<int>> ans;
while (vrtx.first or vrtx.second) {
pair<int, int> x = prnt[vrtx.first][vrtx.second];
ans.first.push_back(vrtx.second + 1);
ans.second.push_back(vrtx.first + 1);
vrtx.first = x.first;
vrtx.second = x.second;
}
cout << dstnc[sz.first - 1][0] << "\n";
for (auto pth : ans.first) {
cout << pth << ' ';
}
cout << "\n";
for (auto pth : ans.second) {
cout << pth << ' ';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int A[505][505];
vector<int> G[505];
pair<int, int> mp[505][505];
queue<pair<int, int> > q;
int n, m;
pair<int, pair<int, int> > bfs() {
int t, x, y, z, w;
q.push({1, n});
mp[1][n] = {0, 1};
A[1][n] = 1;
while (!q.empty()) {
pair<int, int> a = q.front();
q.pop();
x = a.first;
y = a.second;
for (int i = 0; i < G[x].size(); i++) {
z = G[x][i];
for (int j = 0; j < G[y].size(); j++) {
w = G[y][j];
if (z != w) {
if (A[z][w] != 0) {
continue;
} else {
A[z][w] = A[x][y] + 1;
mp[z][w] = a;
if (z == n && w == 1) {
return {A[x][y], {z, w}};
}
q.push({z, w});
}
}
}
}
}
return {-1, {0, 0}};
}
vector<int> ans1;
vector<int> ans2;
signed main() {
scanf("%d", &(n));
scanf("%d", &(m));
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d", &(a));
scanf("%d", &(b));
G[a].push_back(b);
G[b].push_back(a);
}
pair<int, pair<int, int> > x = bfs();
pair<int, int> _ = x.second;
cout << x.first << '\n';
if (x.first == -1) {
return 0;
}
while (_.first) {
ans1.push_back(_.first);
ans2.push_back(_.second);
_ = mp[_.first][_.second];
}
for (int i = ans1.size() - 1; i >= 0; i--) {
cout << ans1[i] << ' ';
}
cout << '\n';
for (int i = ans1.size() - 1; i >= 0; i--) {
cout << ans2[i] << ' ';
}
cout << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int best[1000000];
vector<pair<int, int> > dp;
vector<int> e[505];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int s, t;
scanf("%d%d", &s, &t);
e[s].push_back(t);
e[t].push_back(s);
}
memset(best, -1, sizeof(best));
best[(((1 << 9) | n) << 1) | 0] = 0;
dp.push_back(make_pair((((1 << 9) | n) << 1) | 0, -1));
bool done = false;
for (int i = 0; i < (int)dp.size() && !done; i++) {
int status = dp[i].first;
int s = (status >> 1) >> 9;
int t = (status >> 1) & 0x1ff;
if (status & 1) {
for (int j = 0; j < (int)e[t].size() && !done; j++) {
if (s != e[t][j]) {
int ns = ((((s << 9) | e[t][j]) << 1) | 0);
if (best[ns] == -1) {
best[ns] = best[status];
dp.push_back(make_pair(ns, dp[i].second));
if (s == n && e[t][j] == 1) {
done = true;
}
}
}
}
} else {
for (int j = 0; j < (int)e[s].size() && !done; j++) {
int ns = ((((e[s][j] << 9) | t) << 1) | 1);
if (best[ns] == -1) {
best[ns] = best[status] + 1;
dp.push_back(make_pair(ns, i));
}
}
}
}
if (done) {
printf("%d\n", best[dp.back().first]);
vector<int> res;
for (int i = (int)dp.size() - 1; i != -1; i = dp[i].second) {
res.push_back(dp[i].first);
}
reverse(res.begin(), res.end());
for (size_t i = 0; i < res.size(); i++) {
printf("%d%c", (res[i] >> 1) >> 9, i + 1 == res.size() ? '\n' : ' ');
}
for (size_t i = 0; i < res.size(); i++) {
printf("%d%c", (res[i] >> 1) & 0x1ff, i + 1 == res.size() ? '\n' : ' ');
}
} else {
puts("-1");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 520;
vector<int> v[maxn];
queue<pair<long long, pair<long long, long long> > > q;
bool e;
pair<long long, pair<long long, long long> > k[maxn][maxn][2];
bool d[maxn][maxn][2];
vector<pair<long long, long long> > ans;
int main() {
ios_base::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
for (long long y = 0; y < m; y++) {
long long i, j;
cin >> i >> j;
v[i].push_back(j);
v[j].push_back(i);
}
d[1][n][0] = 1;
q.push(pair<long long, pair<long long, long long> >(
0, pair<long long, long long>(1, n)));
while (q.size()) {
pair<long long, pair<long long, long long> > x = q.front();
q.pop();
if (x == pair<long long, pair<long long, long long> >(
0, pair<long long, long long>(n, 1))) {
e = 1;
while (x.second.second) {
ans.push_back(
pair<long long, long long>(x.second.first, x.second.second));
x = k[x.second.first][x.second.second][x.first];
}
cout << (ans.size() - 1) / 2 << endl;
for (long long y = ans.size() - 1; y >= 0; y -= 2)
cout << ans[y].first << ' ';
cout << endl;
for (long long y = ans.size() - 1; y >= 0; y -= 2)
cout << ans[y].second << ' ';
break;
}
if (x.first == 0) {
long long i = x.second.first;
for (long long y = 0; y < v[i].size(); y++) {
long long s = v[i][y];
if (!d[s][x.second.second][1])
d[s][x.second.second][1] = 1, k[s][x.second.second][1] = x,
q.push(pair<long long, pair<long long, long long> >(
1, pair<long long, long long>(s, x.second.second)));
}
} else {
long long i = x.second.second;
for (long long y = 0; y < v[i].size(); y++) {
long long s = v[i][y];
if (!d[x.second.first][s][0] && s != x.second.first)
d[x.second.first][s][0] = 1, k[x.second.first][s][0] = x,
q.push(pair<long long, pair<long long, long long> >(
0, pair<long long, long long>(x.second.first, s)));
}
}
}
if (!e) cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 0.00001;
const long double PI = 3.14159265358979323846;
inline void file_io(string in_file, string out_file, bool mark) {
if (!mark) return;
freopen(in_file.c_str(), "r", stdin);
}
struct pi {
pi() {}
pi(int a, int b, bool c) : first(a), second(b), whoy(c) {}
int first, second;
bool whoy;
};
const int ml = 501;
const int inf = 1e9;
vector<vector<int>> g(ml);
int d[ml][ml][2];
bool used[ml][ml][2];
pi p[ml][ml][2];
int main() {
file_io("input.in", "distance.out", 0);
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0; i < ml; ++i)
for (int j = 0; j < ml; ++j)
d[i][j][0] = d[i][j][1] = inf, used[i][j][0] = used[i][j][1] = 0;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back((v)), g[v].push_back((u));
}
d[1][n][0] = 0;
used[1][n][0] = 0;
queue<pi> q;
q.push(pi(1, n, 0));
while (!q.empty()) {
int u = q.front().first, v = q.front().second;
bool whoy = q.front().whoy;
if (u == n && v == 1 && whoy == 0) break;
q.pop();
if (whoy == 0) {
for (int i = 0; i < g[u].size(); ++i) {
int to = g[u][i];
if (!used[to][v][1]) {
used[to][v][1] = 1;
d[to][v][1] = d[u][v][whoy] + 1;
p[to][v][1] = pi(u, v, whoy);
q.push(pi(to, v, 1));
}
}
} else {
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (u == to) continue;
if (!used[u][to][0]) {
used[u][to][0] = 1;
d[u][to][0] = d[u][v][whoy] + 1;
p[u][to][0] = pi(u, v, whoy);
q.push(pi(u, to, 0));
}
}
}
}
if (d[n][1][0] == inf)
cout << -1 << endl;
else {
pi j = pi(n, 1, 0);
vector<int> p1, p2;
while (!(j.first == 1 && j.second == n)) {
if (j.whoy)
p2.push_back((j.second));
else
p1.push_back((j.first));
j = p[j.first][j.second][j.whoy];
}
p1.push_back((1));
reverse((p1).begin(), (p1).end()), reverse((p2).begin(), (p2).end());
p2.push_back((1));
cout << p1.size() - 1 << endl;
for (int i = 0; i < p1.size(); ++i) cout << p1[i] << " ";
cout << endl;
for (int i = 0; i < p2.size(); ++i) cout << p2[i] << " ";
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 501;
int n, m;
bool edge[maxn][maxn];
bool f[maxn][maxn][maxn], g[maxn][maxn], h[maxn][maxn];
int V[2][maxn];
void input() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge[u][v] = edge[v][u] = true;
}
}
void solve() {
h[1][n] = f[0][1][n] = true;
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(g, false, sizeof(g));
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (f[i - 1][j][k]) {
for (int l = 1; l <= n; l++)
if (edge[j][l]) g[l][k] = true;
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (g[j][k]) {
for (int l = 1; l <= n; l++)
if (j != l && edge[k][l] && !h[j][l]) h[j][l] = f[i][j][l] = true;
}
if (f[i][n][1]) {
ans = i;
break;
}
}
if (ans == 0) {
printf("-1\n");
return;
}
printf("%d\n", ans);
V[0][ans + 1] = n;
V[1][ans + 1] = 1;
for (int i = ans, a = n, b = 1; i >= 1; i--) {
bool fin = false;
for (int j = 1; j <= n && !fin; j++)
for (int k = 1; k <= n && !fin; k++)
if (f[i - 1][j][k] && edge[j][a] && edge[k][b]) {
fin = true;
a = j;
b = k;
}
V[0][i] = a;
V[1][i] = b;
}
for (int i = 1; i <= ans + 1; i++) printf("%d ", V[0][i]);
printf("\n");
for (int i = 1; i <= ans + 1; i++) printf("%d ", V[1][i]);
printf("\n");
}
int main() {
input();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, ans, prex[505][505][2], prey[505][505][2], prez[505][505][2];
bool vis[505][505][2];
vector<int> eg[505];
struct node {
int u, v, d, st;
};
void prt(int a, int b, int c) {
int a2 = prex[a][b][c];
if (a2 == -1) {
if (c == 1) printf("%d ", a);
return;
}
int b2 = prey[a][b][c];
int c2 = prez[a][b][c];
prt(a2, b2, c2);
if (c == 1) printf("%d ", a);
}
void prt2(int a, int b, int c) {
int a2 = prex[a][b][c];
if (a2 == -1) {
if (!c) printf("%d ", b);
return;
}
int b2 = prey[a][b][c];
int c2 = prez[a][b][c];
prt2(a2, b2, c2);
if (!c) printf("%d ", b);
}
void bfs() {
queue<struct node> q;
struct node st;
st.u = 1;
st.v = n;
st.d = 0;
st.st = 0;
vis[1][n][0] = 1;
q.push(st);
while (!q.empty()) {
struct node u = q.front();
q.pop();
if (u.u == n && u.v == 1 && !u.st) {
ans = u.d;
printf("%d\n1 ", u.d);
prt(u.u, u.v, u.st);
printf("\n");
prt2(u.u, u.v, u.st);
printf("\n");
return;
}
if (!u.st) {
for (int i = 0; i < eg[u.u].size(); i++) {
int v = eg[u.u][i];
if (!vis[v][u.v][1]) {
vis[v][u.v][1] = 1;
struct node tt;
tt.u = v;
tt.v = u.v;
tt.st = 1;
tt.d = u.d;
prex[tt.u][tt.v][tt.st] = u.u;
prey[tt.u][tt.v][tt.st] = u.v;
prez[tt.u][tt.v][tt.st] = u.st;
q.push(tt);
}
}
} else {
for (int i = 0; i < eg[u.v].size(); i++) {
int v = eg[u.v][i];
if (!vis[u.u][v][0] && v != u.u) {
vis[u.u][v][0] = 1;
struct node tt;
tt.u = u.u;
tt.v = v;
tt.st = 0;
tt.d = u.d + 1;
prex[tt.u][tt.v][tt.st] = u.u;
prey[tt.u][tt.v][tt.st] = u.v;
prez[tt.u][tt.v][tt.st] = u.st;
q.push(tt);
}
}
}
}
}
int main() {
ans = (1e9 + 7);
memset(prex, -1, sizeof(prex));
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &m);
for (int i = 1, a, b; i <= m; i++) {
scanf("%d%d", &a, &b);
eg[a].push_back(b);
eg[b].push_back(a);
}
bfs();
if (ans == (1e9 + 7)) printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 501;
int n, m;
vector<vector<int> > g;
pair<pair<int, int>, int> p[N][N][2];
vector<int> path1, path2;
int bfs(int a, int b) {
queue<pair<pair<int, int>, int> > q;
int d[N][N][2];
memset(d, -1, sizeof(d));
d[a][b][0] = 0;
q.push(make_pair(make_pair(a, b), 0));
while (!q.empty()) {
a = q.front().first.first;
b = q.front().first.second;
int t = q.front().second;
q.pop();
if (a == n - 1 && b == 0 && t == 0) return d[a][b][t];
if (t) {
for (int v : g[b]) {
if (d[a][v][0] == -1 && v != a) {
d[a][v][0] = d[a][b][1] + 1;
p[a][v][0] = make_pair(make_pair(a, b), 1);
q.push(make_pair(make_pair(a, v), 0));
}
}
} else {
for (int v : g[a]) {
if (d[v][b][1] == -1) {
d[v][b][1] = d[a][b][0];
p[v][b][1] = make_pair(make_pair(a, b), 0);
q.push(make_pair(make_pair(v, b), 1));
}
}
}
}
return -1;
}
void findPath(int a, int b, int t) {
if (a == 0 && b == n - 1) {
path1.push_back(1);
path2.push_back(n);
return;
}
pair<pair<int, int>, int> nx = p[a][b][t];
findPath(nx.first.first, nx.first.second, nx.second);
if (t == 0) {
path1.push_back(a + 1);
path2.push_back(b + 1);
}
}
int main() {
scanf("%d%d", &n, &m);
g.resize(n);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
int res = bfs(0, n - 1);
if (res == -1) {
printf("-1\n");
return 0;
}
printf("%d\n", res);
findPath(n - 1, 0, 0);
for (int i = 0; i < path1.size(); i++) printf("%d ", path1[i]);
puts("");
for (int i = 0; i < path2.size(); i++) printf("%d ", path2[i]);
puts("");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, par[2][501][501], ans;
vector<int> adj[501], Ans[2];
queue<int> q;
bool k, vis[2][501][501];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
q.push(0);
q.push(1);
q.push(n);
q.push(0);
vis[0][1][n] = 1;
while (!q.empty()) {
bool t = q.front();
q.pop();
int x = q.front();
q.pop();
int y = q.front();
q.pop();
int C = q.front();
q.pop();
if (x == n && y == 1 && t == 0) {
k = 1;
ans = C / 2;
break;
}
if (t == 0) {
for (int i = 0; i < adj[x].size(); i++) {
int z = adj[x][i];
if (!vis[1][z][y]) {
vis[1][z][y] = 1;
par[1][z][y] = x;
q.push(1);
q.push(z);
q.push(y);
q.push(C + 1);
}
}
} else {
for (int i = 0; i < adj[y].size(); i++) {
int z = adj[y][i];
if (!vis[0][x][z] && z != x) {
vis[0][x][z] = 1;
par[0][x][z] = y;
q.push(0);
q.push(x);
q.push(z);
q.push(C + 1);
}
}
}
}
if (!k) {
printf("-1");
return 0;
}
printf("%d\n", ans);
int a = n, b = 1;
Ans[0].push_back(n);
Ans[1].push_back(1);
for (int i = 0; i < ans; i++) {
b = par[0][a][b];
Ans[1].push_back(b);
a = par[1][a][b];
Ans[0].push_back(a);
}
for (int i = Ans[0].size() - 1; i >= 0; i--) printf("%d ", Ans[0][i]);
printf("\n");
for (int i = Ans[1].size() - 1; i >= 0; i--) printf("%d ", Ans[1][i]);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 505, INF = 1e9 + 7;
int dis[N][N];
int prvx[N][N], prvy[N][N];
vector<int> adj[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) dis[i][j] = INF;
dis[1][n] = 0;
queue<int> q;
q.push(1);
q.push(n);
while (q.size()) {
int u = q.front();
q.pop();
int v = q.front();
q.pop();
for (int x : adj[u])
for (int y : adj[v])
if (dis[x][y] == INF && x != y) {
dis[x][y] = 1 + dis[u][v];
q.push(x);
q.push(y);
prvx[x][y] = u;
prvy[x][y] = v;
}
}
if (dis[n][1] == INF)
cout << -1 << endl;
else {
cout << dis[n][1] << endl;
int ca = n, cb = 1;
vector<int> ra, rb;
while (ca != 1 || cb != n) {
ra.push_back(ca);
rb.push_back(cb);
int na = prvx[ca][cb];
int nb = prvy[ca][cb];
ca = na;
cb = nb;
assert(ca && cb);
}
ra.push_back(1);
rb.push_back(n);
reverse(ra.begin(), ra.end());
reverse(rb.begin(), rb.end());
for (int x : ra) cout << x << " ";
cout << endl;
for (int x : rb) cout << x << " ";
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 600;
struct Node {
int x;
int y;
int h;
int who;
} st, en;
int n, m;
int mp[M][M];
int re[M][M];
int re_cnt[M];
bool vst[M][M][2];
int pre[M][M][2];
int rr[M];
int BFS() {
queue<Node> q;
Node cur, next;
memset(vst, 0, sizeof(vst));
vst[st.x][st.y][st.who] = true;
q.push(st);
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.x == n && cur.y == 1 && cur.who == 0) return cur.h;
if (cur.who == 0) {
for (int i = 0; i < re_cnt[cur.x]; i++) {
int nx = re[cur.x][i];
next.x = nx;
next.y = cur.y;
next.h = cur.h + 1;
next.who = 1;
if (!vst[next.x][next.y][next.who]) {
q.push(next);
vst[next.x][next.y][next.who] = true;
pre[next.x][next.y][next.who] = cur.x;
}
}
} else {
for (int i = 0; i < re_cnt[cur.y]; i++) {
int ny = re[cur.y][i];
next.x = cur.x;
next.y = ny;
next.h = cur.h;
next.who = 0;
if (next.x == next.y) continue;
if (!vst[next.x][next.y][next.who]) {
q.push(next);
vst[next.x][next.y][next.who] = true;
pre[next.x][next.y][next.who] = cur.y;
}
}
}
}
return -1;
}
int main() {
int a, b;
while (scanf("%d%d", &n, &m) == 2) {
memset(re_cnt, 0, sizeof(re_cnt));
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
re[a][re_cnt[a]++] = b;
re[b][re_cnt[b]++] = a;
}
st.x = 1;
st.y = n;
st.h = 0;
st.who = 0;
int re = BFS();
stack<int> q1, q2;
if (re == -1)
printf("%d\n", -1);
else {
int nx = n;
int ny = 1;
int who = 0;
q1.push(nx);
q2.push(ny);
while (!(nx == 1 && ny == n)) {
ny = pre[nx][ny][0];
nx = pre[nx][ny][1];
q1.push(nx);
q2.push(ny);
}
printf("%d\n", re);
int cnt = 0;
while (!q1.empty()) {
int tmp = q1.top();
q1.pop();
rr[cnt++] = tmp;
}
for (int i = 0; i < cnt; i++) {
if (i == 0)
printf("%d", rr[i]);
else
printf(" %d", rr[i]);
}
printf("\n");
cnt = 0;
while (!q2.empty()) {
int tmp = q2.top();
q2.pop();
rr[cnt++] = tmp;
}
for (int i = 0; i < cnt; i++) {
if (i == 0)
printf("%d", rr[i]);
else
printf(" %d", rr[i]);
}
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 100 + 13, MAXT = 2 + 13, MAXD = 20 * 1000 + 13;
bool mark[MAXN][MAXN][MAXT];
int dist[MAXN][MAXN][MAXT];
pair<pair<int, int>, int> par[MAXN][MAXN][MAXT];
queue<pair<pair<int, int>, int> > bfsq;
vector<int> adj[MAXN], bob, alex;
int main() {
int n, m;
cin >> n >> m;
int a, b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dist[i][j][1] = dist[i][j][2] = MAXD;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dist[1][n][1] = 0;
mark[1][n][1] = true;
bfsq.push(make_pair(make_pair(1, n), 1));
int t;
while (!bfsq.empty()) {
b = bfsq.front().first.first, a = bfsq.front().first.second,
t = bfsq.front().second;
if (t == 1) {
for (auto i : adj[b]) {
if (!mark[i][a][2]) {
par[i][a][2] = bfsq.front();
dist[i][a][2] = dist[b][a][1] + 1;
mark[i][a][2] = true;
bfsq.push(make_pair(make_pair(i, a), 2));
}
}
} else {
for (auto i : adj[a]) {
if (!mark[b][i][1] and b != i) {
par[b][i][1] = bfsq.front();
dist[b][i][1] = dist[b][a][2] + 1;
mark[b][i][1] = true;
bfsq.push(make_pair(make_pair(b, i), 1));
}
}
}
bfsq.pop();
}
if (dist[n][1][1] > 20 * 1000) {
cout << -1 << endl;
return 0;
}
cout << dist[n][1][1] / 2 << endl;
pair<pair<int, int>, int> tmp = {{n, 1}, 1}, begin = {{1, n}, 1};
while (tmp != begin) {
if (tmp.second == 1) {
bob.push_back(tmp.first.first);
alex.push_back(tmp.first.second);
}
tmp = par[tmp.first.first][tmp.first.second][tmp.second];
}
bob.push_back(1);
alex.push_back(n);
reverse(bob.begin(), bob.end());
reverse(alex.begin(), alex.end());
for (auto i : bob) cout << i << " ";
cout << endl;
for (auto i : alex) cout << i << " ";
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
const int maxm = 1e4 + 10;
bool vis[maxn][maxn][2];
int pre[maxn][maxn][2], head[maxn], ansx[maxn], ansy[maxn];
struct node {
int to, next;
} e[maxm * 2];
struct OO {
int x, y, z;
} p, pp;
int cnt, n, m;
void add(int x, int y) {
e[cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt++;
}
int main() {
int x, y;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
int f = 0;
p.x = 1;
p.y = n;
p.z = 0;
vis[1][n][0] = 1;
queue<OO> q;
q.push(p);
while (!q.empty()) {
p = q.front();
q.pop();
if (p.x == n && p.y == 1 && p.z == 0) {
f = 1;
break;
}
if (p.z == 0) {
for (int i = head[p.x]; i != -1; i = e[i].next) {
int v = e[i].to;
if (!vis[v][p.y][1]) {
pp.x = v;
pp.y = p.y;
pp.z = 1;
q.push(pp);
vis[v][p.y][1] = 1;
pre[pp.x][pp.y][1] = p.x;
}
}
} else {
for (int i = head[p.y]; i != -1; i = e[i].next) {
int v = e[i].to;
if (!vis[p.x][v][0] && v != p.x) {
pp.x = p.x;
pp.y = v;
pp.z = 0;
q.push(pp);
vis[pp.x][v][0] = 1;
pre[pp.x][pp.y][0] = p.y;
}
}
}
}
if (!f)
puts("-1");
else {
int tot = 0;
int nowx = n, nowy = 1;
while (nowx != 1 || nowy != n) {
ansx[++tot] = nowx;
ansy[tot] = nowy;
int ty = pre[nowx][nowy][0];
int tx = pre[nowx][ty][1];
nowx = tx, nowy = ty;
}
printf("%d\n", tot);
printf("1");
for (int i = tot; i > 0; i--) printf(" %d", ansx[i]);
printf("\n%d", n);
for (int i = tot; i > 0; i--) printf(" %d", ansy[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 510;
int n, m, ans[MAXN][MAXN][2], parf[MAXN][MAXN][2], pars[MAXN][MAXN][2],
to[MAXN][MAXN][2];
bool visit[MAXN][MAXN][2];
vector<int> adj[MAXN];
void bfs() {
queue<pair<pair<int, int>, int>> q;
visit[0][n - 1][0] = true;
ans[0][n - 1][0] = 0;
q.push({{0, n - 1}, 0});
pars[0][n - 1][0] = -1;
parf[0][n - 1][0] = -1;
while (!q.empty()) {
int f = q.front().first.first, sec = q.front().first.second,
t = q.front().second;
q.pop();
if (t == 0) {
for (int i = 0; i < (int)adj[f].size(); i++) {
int u = adj[f][i];
if (!visit[u][sec][1]) {
visit[u][sec][1] = true;
q.push({{u, sec}, 1});
ans[u][sec][1] = ans[f][sec][0];
parf[u][sec][1] = f;
pars[u][sec][1] = sec;
to[u][sec][1] = u;
}
}
} else {
for (int i = 0; i < (int)adj[sec].size(); i++) {
int u = adj[sec][i];
if (!visit[f][u][0] && f != u) {
visit[f][u][0] = true;
q.push({{f, u}, 0});
ans[f][u][0] = ans[f][sec][1] + 1;
parf[f][u][0] = f;
pars[f][u][0] = sec;
to[f][u][0] = u;
}
}
}
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
ans[i][j][0] = -1;
ans[i][j][1] = -1;
}
bfs();
cout << ans[n - 1][0][0] << endl;
if (ans[n - 1][0][0] == -1) return 0;
int vv = n - 1, uu = 0, t = 0;
vector<int> p1, p2;
while (parf[vv][uu][t] != -1) {
if (t == 0)
p2.push_back(to[vv][uu][t]);
else
p1.push_back(to[vv][uu][t]);
int v2 = parf[vv][uu][t], u2 = pars[vv][uu][t];
vv = v2, uu = u2;
t = 1 - t;
}
p1.push_back(0);
p2.push_back(n - 1);
while (!p1.empty()) {
cout << p1.back() + 1 << " ";
p1.pop_back();
}
cout << endl;
while (!p2.empty()) {
cout << p2.back() + 1 << " ";
p2.pop_back();
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
vector<int> adj[N];
int n, m, dist[N][N][2], p, ans;
queue<pair<pair<int, int>, bool> > Q;
pair<pair<int, int>, bool> par[N][N][2];
int main() {
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
memset(dist, -1, sizeof(dist));
dist[1][n][0] = 0;
Q.push(make_pair(make_pair(1, n), false));
while (!Q.empty()) {
pair<pair<int, int>, bool> fr = Q.front();
Q.pop();
int a = fr.first.first, b = fr.first.second;
bool t = fr.second;
if (!t) {
for (int u : adj[a])
if (dist[u][b][1] == -1) {
dist[u][b][1] = dist[a][b][t];
par[u][b][1] = fr;
Q.push(make_pair(make_pair(u, b), true));
}
} else {
for (int u : adj[b]) {
if (u == a || dist[a][u][0] != -1) continue;
dist[a][u][0] = dist[a][b][t] + 1;
par[a][u][0] = fr;
Q.push(make_pair(make_pair(a, u), false));
}
}
}
ans = dist[n][1][0];
cout << ans << endl;
if (ans != -1) {
vector<int> v1, v2;
int a = n, b = 1;
do {
v1.push_back(a);
v2.push_back(b);
for (int i = 0; i < 2; i++) {
pair<pair<int, int>, bool> tmp;
tmp = par[a][b][i];
a = tmp.first.first;
b = tmp.first.second;
}
} while (ans--);
for (int x : v2) cout << x << " ";
cout << "\n";
for (int x : v1) cout << x << " ";
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 789;
long long n, m, s, e, dp[N][N][2], par[N][N][2];
bool mark[N][N][2];
vector<long long> adj[N], p1, p2;
queue<pair<pair<long long, long long>, bool> > q;
void bfs() {
while (q.size()) {
long long u = q.front().first.first;
long long v = q.front().first.second;
bool t = q.front().second;
q.pop();
if (t == 0) {
for (auto x : adj[u]) {
if (!mark[x][v][1]) {
mark[x][v][1] = 1;
dp[x][v][1] = dp[u][v][0] + 1;
par[x][v][1] = u;
q.push({{x, v}, 1});
}
}
} else {
for (auto x : adj[v]) {
if (x != u and !mark[u][x][0]) {
mark[u][x][0] = 1;
dp[u][x][0] = dp[u][v][1] + 1;
par[u][x][0] = v;
q.push({{u, x}, 0});
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i][j][0] = dp[i][j][1] = -1;
}
}
cin >> n >> m;
s = 1;
e = n;
long long second = s;
for (int i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(adj[i].begin(), adj[i].end());
}
dp[s][e][0] = 0;
q.push({{s, e}, 0});
mark[s][e][0] = 1;
bfs();
if (dp[e][s][0] == -1) return cout << -1, 0;
cout << (dp[e][s][0] / 2);
bool b = 0;
while (s and e) {
if (b) {
p1.push_back(e);
e = par[e][s][b];
} else {
p2.push_back(s);
s = par[e][s][b];
}
b ^= 1;
}
p1.push_back(second);
cout << '\n';
for (int i = p1.size() - 1; i >= 0; i--) {
cout << p1[i] << " ";
}
cout << '\n';
for (int i = p2.size() - 1; i >= 0; i--) {
cout << p2[i] << " ";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 555;
int n, m, dist[2][N][N];
pair<int, int> pred[2][N][N];
vector<int> gr[N];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
gr[a].push_back(b);
gr[b].push_back(a);
}
queue<vector<int> > q;
dist[0][1][n] = 1;
pred[0][1][n] = {-1, -1};
q.push({0, 1, n});
while (!q.empty()) {
auto vc = q.front();
q.pop();
int type = vc[0], v = vc[1], u = vc[2];
if (type == 0) {
for (auto to : gr[v]) {
if (!dist[1][to][u]) {
dist[1][to][u] = dist[0][v][u];
pred[1][to][u] = {v, u};
q.push({1, to, u});
}
}
} else {
for (auto to : gr[u]) {
if (v != to && !dist[0][v][to]) {
dist[0][v][to] = dist[1][v][u] + 1;
pred[0][v][to] = {v, u};
q.push({0, v, to});
}
}
}
}
if (!dist[0][n][1]) {
cout << -1;
} else {
vector<int> way1, way2;
int v = n, u = 1, t = 0;
while (pred[t][v][u] != make_pair(-1, -1)) {
if (!t) {
way1.push_back(v);
way2.push_back(u);
}
auto pp = pred[t][v][u];
v = pp.first;
u = pp.second;
t ^= 1;
}
way1.push_back(v);
way2.push_back(u);
reverse(way1.begin(), way1.end());
reverse(way2.begin(), way2.end());
cout << dist[0][n][1] - 1 << endl;
for (auto c : way1) cout << c << ' ';
cout << endl;
for (auto c : way2) cout << c << ' ';
}
}
|
#include <bits/stdc++.h>
const int N = 507;
struct State {
short Depth[2] = {0, 0};
short Position[2] = {0, 0};
short LastPosition[2] = {0, 0};
bool Turn = true;
State() {}
State(short pos1, short pos2) {
Position[0] = pos1;
Position[1] = pos2;
}
State(short pos1, short pos2, bool t) {
Position[0] = pos1;
Position[1] = pos2;
Turn = t;
}
State(State parent, short now) {
Depth[0] = (parent.Depth[0]) + ((bool)parent.Turn);
Depth[1] = (parent.Depth[1]) + (!parent.Turn);
LastPosition[0] = Position[0] = parent.Position[0];
LastPosition[1] = Position[1] = parent.Position[1];
Turn = !parent.Turn;
Position[Turn] = now;
}
};
bool V[N][N][2];
std::vector<short> G[N];
State Parent[N][N][2];
void Bfs(int t) {
int x = 1, y = t;
std::queue<State> Q;
if (x != y) {
State s(x, y);
s.Turn = true;
s.LastPosition[0] = s.LastPosition[1] = -2137;
Q.push(s);
}
while (!Q.empty()) {
State S = Q.front();
Q.pop();
if (V[S.Position[0]][S.Position[1]][S.Turn]) continue;
V[S.Position[0]][S.Position[1]][S.Turn] = true;
Parent[S.Position[0]][S.Position[1]][S.Turn] =
State(S.LastPosition[0], S.LastPosition[1], !S.Turn);
if (S.Position[1] == 1 && S.Position[0] == t && S.Depth[0] == S.Depth[1]) {
printf("%d\n", S.Depth[0]);
std::stack<short> R;
short p1 = S.Position[0], p2 = S.Position[1];
bool pt = S.Turn;
R.push(S.Position[0]);
while (p1 > 0) {
State& par = Parent[p1][p2][pt];
if (par.Position[0] <= 0) break;
if (par.Turn) R.push(par.Position[0]);
p1 = par.Position[0];
p2 = par.Position[1];
pt = par.Turn;
}
while (!R.empty()) {
printf("%d ", (int)R.top());
R.pop();
}
printf("\n");
p1 = S.Position[0];
p2 = S.Position[1];
pt = S.Turn;
R.push(1);
while (p1 > 0) {
State& par = Parent[p1][p2][pt];
if (par.Position[1] <= 0) break;
if (!par.Turn) R.push(par.Position[1]);
p1 = par.Position[0];
p2 = par.Position[1];
pt = par.Turn;
}
while (!R.empty()) {
printf("%d ", (int)R.top());
R.pop();
}
return;
}
if (S.Turn) {
for (auto v : G[S.Position[0]]) {
if (!V[v][S.Position[1]][!S.Turn] &&
(v != S.Position[1] || S.Depth[0] + 1 != S.Depth[1])) {
Q.emplace(S, v);
}
}
} else {
for (auto v : G[S.Position[1]]) {
if (!V[S.Position[0]][v][!S.Turn] &&
(v != S.Position[0] || S.Depth[0] != S.Depth[1] + 1)) {
Q.emplace(S, v);
}
}
}
}
printf("-1\n");
}
void Solve(int n, int m) {
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
Bfs(n);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
Solve(n, m);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
queue<pair<int, pair<int, int> > > qu;
pair<int, int> e[10005];
pair<int, pair<int, int> > g[505][505][3];
int head[505], adj[20005], f[505][505][3], res1[25005], res2[25005];
int n, m;
void ghikq() {
printf("%d\n", f[n][1][0] / 2);
int u = n, v = 1, x = 0;
res1[0] = 1;
res1[1] = 1;
res2[0] = 1;
res2[1] = n;
pair<int, pair<int, int> > pt;
while (u != 1 || v != n || x != 0)
if (!x) {
pt = g[u][v][x];
res1[++res1[0]] = pt.second.first;
u = pt.first;
v = pt.second.first;
x = 1;
} else {
pt = g[u][v][x];
res2[++res2[0]] = pt.first;
u = pt.first;
v = pt.second.first;
x = 0;
}
for (int i = 1; i <= f[n][1][0] / 2; i++) printf("%d ", res1[i]);
printf("%d\n", n);
for (int i = 1; i <= f[n][1][0] / 2; i++) printf("%d ", res2[i]);
printf("1\n");
}
int main() {
if (0) {
freopen("a.inp", "r", stdin);
};
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &e[i].first, &e[i].second);
head[e[i].first]++;
head[e[i].second]++;
}
for (int i = 2; i <= n + 1; i++) head[i] += head[i - 1];
for (int i = m; i > 0; i--) {
adj[head[e[i].first]--] = e[i].second;
adj[head[e[i].second]--] = e[i].first;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j][0] = f[i][j][1] = oo;
f[1][n][0] = 0;
qu.push(pair<int, pair<int, int> >(1, pair<int, int>(n, 0)));
pair<int, pair<int, int> > pt;
int u, v, x, vv;
while (!qu.empty()) {
pt = qu.front();
qu.pop();
u = pt.first;
v = pt.second.first;
x = pt.second.second;
if (u == n && v == 1 && !x) {
ghikq();
return 0;
}
if (!x) {
for (int i = head[u] + 1; i <= head[u + 1]; i++) {
vv = adj[i];
if (f[vv][v][1] != oo) continue;
f[vv][v][1] = f[u][v][0] + 1;
g[vv][v][1].first = u;
g[vv][v][1].second.first = v;
g[vv][v][1].second.second = 0;
qu.push(pair<int, pair<int, int> >(vv, pair<int, int>(v, 1)));
}
} else {
for (int i = head[v] + 1; i <= head[v + 1]; i++) {
vv = adj[i];
if (f[u][vv][0] != oo || u == vv) continue;
f[u][vv][0] = f[u][v][1] + 1;
g[u][vv][0].first = u;
g[u][vv][0].second.first = v;
g[u][vv][0].second.second = 1;
qu.push(pair<int, pair<int, int> >(u, pair<int, int>(vv, 0)));
}
}
}
printf("-1\n");
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
int n, m;
vector<int> edge[MAXN];
bool vis[MAXN][MAXN][2];
int pre[MAXN][MAXN][2];
int num[MAXN][MAXN][2];
struct Node {
int x, y, z;
Node(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
};
queue<Node> q;
int ans[MAXN][2];
int main() {
int i, j;
scanf("%d%d", &n, &m);
int x, y, z;
for (i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
edge[x].push_back(y);
edge[y].push_back(x);
}
memset(num, -1, sizeof(num));
q.push(Node(1, n, 0));
vis[1][n][0] = 1;
num[1][n][0] = 0;
pre[1][n][0] = -1;
while (!q.empty()) {
Node u = q.front();
q.pop();
x = u.x;
y = u.y;
if (x == y && u.z == 0) continue;
if (x == n && y == 1 && u.z == 0) break;
if (!u.z) {
z = 1;
for (i = 0; i < edge[x].size(); i++) {
int v = edge[x][i];
if (vis[v][y][z]) continue;
vis[v][y][z] = 1;
num[v][y][z] = num[x][y][u.z] + 1;
pre[v][y][z] = x;
q.push(Node(v, y, z));
}
} else {
z = 0;
for (i = 0; i < edge[y].size(); i++) {
int v = edge[y][i];
if (vis[x][v][z]) continue;
vis[x][v][z] = 1;
num[x][v][z] = num[x][y][u.z];
pre[x][v][z] = y;
q.push(Node(x, v, z));
}
}
}
printf("%d\n", num[n][1][0]);
if (num[n][1][0] == -1) return 0;
int tot1 = 0, tot2 = 0;
x = n, y = 1, z = 0;
while (pre[x][y][z] != -1) {
if (z) {
ans[tot1++][0] = x;
x = pre[x][y][z];
} else {
ans[tot2++][1] = y;
y = pre[x][y][z];
}
z = 1 - z;
}
ans[tot1++][0] = 1;
ans[tot2++][1] = n;
for (i = tot1 - 1; i >= 0; i--) printf("%d ", ans[i][0]);
printf("\n");
for (i = tot2 - 1; i >= 0; i--) printf("%d ", ans[i][1]);
printf("\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 7;
int n, m;
vector<int> adj[MAXN], a, b;
queue<pair<pair<int, int>, bool> > q;
bool mark[MAXN][MAXN][2];
int high[MAXN][MAXN][2];
pair<pair<int, int>, bool> par[MAXN][MAXN][2], nu = {{-1, -1}, 0};
void bfs(int u, int v, bool t) {
par[u][v][t] = nu;
high[u][v][t] = 0;
mark[u][v][t] = 1;
pair<pair<int, int>, bool> root = {{u, v}, t};
q.push(root);
while (!q.empty()) {
auto p = q.front();
int u = p.first.first, v = p.first.second;
bool t = p.second;
q.pop();
if (u == n - 1 && v == 0 && t == 0) return;
if (t == 0)
for (auto x : adj[u]) {
if (!mark[x][v][1]) {
high[x][v][1] = high[u][v][t] + 1;
par[x][v][1] = p;
pair<pair<int, int>, bool> l = {{x, v}, 1};
q.push(l);
mark[x][v][1] = 1;
}
}
else
for (auto x : adj[v])
if (x != u) {
if (!mark[u][x][0]) {
high[u][x][0] = high[u][v][t];
par[u][x][0] = par[u][v][t];
pair<pair<int, int>, bool> l = {{u, x}, 0};
q.push(l);
mark[u][x][0] = 1;
}
}
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
bfs(0, n - 1, 0);
if (!mark[n - 1][0][0]) return cout << -1 << endl, 0;
;
cout << high[n - 1][0][0] << endl;
pair<pair<int, int>, bool> p = {{n - 1, 0}, 0};
auto cur = p;
while (cur != nu) {
int u = cur.first.first, v = cur.first.second, t = cur.second;
a.push_back(u), b.push_back(v);
cur = par[u][v][t];
}
for (int i = (int)a.size() - 1; i > -1; i--) cout << a[i] + 1 << ' ';
cout << endl;
for (int i = (int)b.size() - 1; i > -1; i--) cout << b[i] + 1 << ' ';
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long n, m, a, b, temp, v1, v2, v3, v4;
queue<long> qu;
vector<long> g[1 << 20];
long par[1 << 20];
long used[1 << 20];
long v;
long did[1000][1000];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
--a;
--b;
g[a].push_back(b);
g[b].push_back(a);
}
qu.push(n - 1);
used[n - 1] = 1;
while (qu.size()) {
temp = qu.front();
qu.pop();
v1 = temp / n;
v2 = temp % n;
for (int i = 0; i < g[v1].size(); i++) {
v3 = g[v1][i];
if (did[v3][v2]) continue;
for (int j = 0; j < g[v2].size(); j++) {
v4 = g[v2][j];
if (v3 == v4) continue;
v = v3 * n + v4;
if (used[v]) continue;
par[v] = temp;
used[v] = used[temp] + 1;
qu.push(v);
}
did[v3][v2] = 1;
}
}
if (used[n * n - n] == 0) {
cout << -1 << endl;
return 0;
}
long vert;
vector<long> ans;
vert = n * n - n;
while (vert != n - 1) {
ans.push_back(vert);
vert = par[vert];
}
reverse(ans.begin(), ans.end());
cout << ans.size() << endl;
cout << 1;
for (int i = 0; i < ans.size(); i++) {
cout << " " << ans[i] / n + 1;
}
cout << endl;
cout << n;
for (int i = 0; i < ans.size(); i++) {
cout << " " << ans[i] % n + 1;
}
cout << endl;
cin.get();
cin.get();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 550;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long n, m, dis[maxn][maxn][2], v, u, f, x;
pair<long long, long long> par[maxn][maxn][2];
queue<pair<pair<long long, long long>, long long> > q;
vector<long long> v1, v2;
vector<long long> g[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (long long i = 0; i < m; i++) {
cin >> v >> u;
g[v].push_back(u);
g[u].push_back(v);
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
dis[i][j][0] = inf;
dis[i][j][1] = inf;
}
}
dis[1][n][0] = 0;
q.push({{1, n}, 0});
while (!q.empty()) {
v = q.front().first.first;
u = q.front().first.second;
f = q.front().second;
q.pop();
if (f) {
for (long long i = 0; i < g[u].size(); i++) {
x = g[u][i];
if (dis[v][x][0] > dis[v][u][1] + 1) {
dis[v][x][0] = dis[v][u][1] + 1;
par[v][x][0] = {v, u};
q.push({{v, x}, 0});
}
}
} else {
if (v == u) continue;
for (long long i = 0; i < g[v].size(); i++) {
x = g[v][i];
if (dis[x][u][1] > dis[v][u][0] + 1) {
dis[x][u][1] = dis[v][u][0] + 1;
par[x][u][1] = {v, u};
q.push({{x, u}, 1});
}
}
}
}
if (dis[n][1][0] >= inf) {
cout << -1;
return 0;
}
cout << dis[n][1][0] / 2 << '\n';
v = n;
u = 1;
f = 0;
while (v) {
if (f == 0) {
v2.push_back(v);
v1.push_back(u);
}
x = v;
v = par[v][u][f].first;
u = par[x][u][f].second;
f = 1 - f;
}
for (long long i = 0; i < v1.size(); i++) {
cout << v1[i] << " ";
}
cout << '\n';
for (long long i = 0; i < v2.size(); i++) {
cout << v2[i] << " ";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e2 + 10, MAXM = 1e4 + 10;
int dis[MAXN][MAXN][2], vis[MAXN][MAXN][2];
int ans[2][MAXN], sz, n;
pair<pair<int, int>, int> par[MAXN][MAXN][2];
vector<int> G[MAXN];
inline pair<pair<int, int>, int> consS(int a, int b, int c) {
return pair<pair<int, int>, int>(pair<int, int>(a, b), c);
}
void print(pair<pair<int, int>, int> cur, pair<pair<int, int>, int> e) {
if (cur == e) {
return;
}
int x = cur.first.first, y = cur.first.second, z = cur.second;
print(par[x][y][z], e);
if (z == 0)
ans[0][sz] = x;
else
ans[1][sz++] = y;
}
int main() {
ios::sync_with_stdio(0);
int m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
}
dis[0][n - 1][0] = 0;
queue<pair<pair<int, int>, int> > q;
q.push(consS(0, n - 1, 0));
while (q.size()) {
int u = q.front().first.first, v = q.front().first.second,
z = q.front().second;
if (z == 0) {
for (int i = 0; i < G[u].size(); i++) {
int w = G[u][i];
if (vis[w][v][1]) continue;
vis[w][v][1] = true;
dis[w][v][1] = dis[u][v][0] + 1;
par[w][v][1] = q.front();
q.push(consS(w, v, 1));
}
} else {
for (int i = 0; i < G[v].size(); i++) {
int w = G[v][i];
if (w == u || vis[u][w][0]) continue;
vis[u][w][0] = true;
dis[u][w][0] = dis[u][v][1] + 1;
par[u][w][0] = q.front();
q.push(consS(u, w, 0));
}
}
q.pop();
}
if (vis[n - 1][0][0]) {
cout << dis[n - 1][0][0] / 2 << endl;
print(consS(n - 1, 0, 0), consS(0, n - 1, 0));
for (int j = 0; j < 2; j++, cout << endl) {
for (int i = 0; i < sz; i++) cout << ans[j][i] + 1 << " ";
cout << (j ? 1 : n) << " ";
}
} else
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;
struct Edge {
int src, dst;
int weight;
Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight
: e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
int maximumFlow(const vector<vector<Edge> > &g, int s, int t,
int limit = 1 << 30) {
int n = g.size();
vector<vector<int> > flow(n, vector<int>(n)), capacity(n, vector<int>(n));
for (int u = 0; u < n; u++)
for (__typeof((g[u]).begin()) e = (g[u]).begin(); e != (g[u]).end(); e++)
capacity[e->src][e->dst] += e->weight;
int total = 0;
while (total < limit) {
queue<int> Q;
Q.push(s);
vector<int> prev(n, -1);
prev[s] = s;
while (!Q.empty() && prev[t] < 0) {
int u = Q.front();
Q.pop();
for (__typeof((g[u]).begin()) e = (g[u]).begin(); e != (g[u]).end(); e++)
if (prev[e->dst] < 0 && (capacity[u][e->dst] - flow[u][e->dst]) > 0) {
prev[e->dst] = u;
Q.push(e->dst);
}
}
if (prev[t] < 0) return total;
int inc = limit - total;
for (int j = t; prev[j] != j; j = prev[j])
inc = min(inc, (capacity[prev[j]][j] - flow[prev[j]][j]));
for (int j = t; prev[j] != j; j = prev[j])
flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
total += inc;
}
}
int n, m, d[500][500];
vector<vector<Edge> > e;
bool visit[500], use[500][500];
int pa[500][500], push_back[500][500], cost[500][500];
int rec(int c) {
visit[c] = 1;
int ret = 1;
for (__typeof((e[c]).begin()) i = (e[c]).begin(); i != (e[c]).end(); i++)
if (!visit[i->dst]) ret += rec(i->dst);
return ret;
}
bool can() {
scanf("%d%d", &n, &m);
e.clear();
e.resize(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = i == j ? 0 : 1e9;
for (int i = 0; i < m; i++) {
int s, t;
scanf("%d%d", &s, &t);
s--;
t--;
e[s].push_back(Edge(s, t, 1));
e[t].push_back(Edge(t, s, 1));
d[s][t] = d[t][s] = 1;
}
int flow = maximumFlow(e, 0, n - 1);
if (flow == 0) return 0;
if (flow > 1) return 1;
vector<int> prev(n, -1), path;
queue<int> q;
q.push(0);
while (!q.empty()) {
int c = q.front();
q.pop();
visit[c] = 1;
if (c == n - 1) break;
for (__typeof((e[c]).begin()) i = (e[c]).begin(); i != (e[c]).end(); i++)
if (prev[i->dst] < 0) prev[i->dst] = c, q.push(i->dst);
}
int cur = n - 1;
for (; cur != 0; cur = prev[cur]) path.push_back(cur);
path.push_back(0);
if (path.size() % 2 == 0) return 1;
for (int i = 0; i < n; i++) visit[i] = 0;
for (int i = 0; i < path.size(); i++) visit[i] = 1;
for (__typeof((path).begin()) i = (path).begin(); i != (path).end(); i++)
for (__typeof((e[*i]).begin()) j = (e[*i]).begin(); j != (e[*i]).end(); j++)
if (!visit[j->dst]) {
int ret = rec(j->dst);
if (ret > 1) return 1;
}
return 0;
}
void run() {
if (!can()) {
cout << -1 << endl;
return;
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
pa[i][j] = push_back[i][j] = -1, cost[i][j] = 1e9, use[i][j] = 0;
priority_queue<pair<int, pair<int, int> > > q;
q.push(make_pair(-d[0][n - 1], make_pair(0, n - 1)));
cost[0][n - 1] = 0;
while (!q.empty()) {
int a = q.top().second.first, b = q.top().second.second;
q.pop();
if (use[a][b]) continue;
use[a][b] = 1;
if (a == n - 1 && b == 0) break;
for (__typeof((e[a]).begin()) i = (e[a]).begin(); i != (e[a]).end(); i++)
for (__typeof((e[b]).begin()) j = (e[b]).begin(); j != (e[b]).end(); j++)
if (i->dst != j->dst && cost[i->dst][j->dst] > cost[a][b] + 1)
pa[i->dst][j->dst] = a, push_back[i->dst][j->dst] = b,
cost[i->dst][j->dst] = cost[a][b] + 1,
q.push(make_pair(-max(d[i->dst][n - 1], d[j->dst][0]) - cost[a][b],
make_pair(i->dst, j->dst)));
}
if (cost[n - 1][0] == 1e9) {
cout << -1 << endl;
return;
}
vector<int> p, pp;
int a = n - 1, b = 0;
for (; a != 0 || b != n - 1;) {
p.push_back(a), pp.push_back(b);
int na = pa[a][b], nb = push_back[a][b];
a = na;
b = nb;
}
p.push_back(0);
pp.push_back(n - 1);
reverse((p).begin(), (p).end());
reverse((pp).begin(), (pp).end());
printf("%d\n", (int)p.size() - 1);
for (int i = 0; i < p.size(); i++) printf("%d ", p[i] + 1);
puts("");
for (int i = 0; i < p.size(); i++) printf("%d ", pp[i] + 1);
puts("");
}
int main() { run(); }
|
#include <bits/stdc++.h>
int Map[502][502];
int xline[255000], yline[255000], faline[255000], l, r, rr, next[505][505], now;
int main() {
int n, m, a, b;
scanf("%d%d", &n, &m);
memset(next, 0, sizeof(next));
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
next[a][++next[a][0]] = b;
next[b][++next[b][0]] = a;
}
l = r = rr = 1;
xline[l] = 1;
yline[l] = n;
memset(Map, -1, sizeof(Map));
Map[1][n] = 0;
now = 1;
int fina = 0;
while (l <= r) {
int nowx = xline[l];
int nowy = yline[l];
for (int i = 1; i <= next[nowx][0]; i++)
for (int j = 1; j <= next[nowy][0]; j++) {
if (Map[next[nowx][i]][next[nowy][j]] == -1 &&
next[nowx][i] != next[nowy][j]) {
Map[next[nowx][i]][next[nowy][j]] = now;
r++;
xline[r] = next[nowx][i];
yline[r] = next[nowy][j];
faline[r] = l;
if (xline[r] == n && yline[r] == 1) fina = r;
}
}
if (fina) break;
l++;
if (l > rr) {
now++;
rr = r;
}
}
printf("%d\n", Map[n][1]);
if (Map[n][1] == -1) return 0;
now = fina;
while (now > 1) {
printf("%d ", yline[now]);
now = faline[now];
}
printf("%d\n", yline[now]);
now = fina;
while (now > 1) {
printf("%d ", xline[now]);
now = faline[now];
}
printf("%d\n", xline[now]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 555;
int dis[maxn][maxn];
vector<int> nei[maxn], ans1, ans2;
queue<int> q;
pair<int, int> p[maxn][maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, x, y;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
nei[x].push_back(y);
nei[y].push_back(x);
}
dis[1][n] = 1;
q.push(1);
q.push(n);
int u, v;
p[1][n] = {0, 0};
while (q.size()) {
u = q.front();
q.pop();
v = q.front();
q.pop();
if (u == v) continue;
for (auto e : nei[u]) {
for (auto w : nei[v]) {
if (!dis[e][w]) {
dis[e][w] = dis[u][v] + 1;
q.push(e);
q.push(w);
p[e][w] = {u, v};
}
}
}
}
if (!dis[n][1]) {
cout << -1;
return 0;
}
u = n;
v = 1;
while (u + v > 0) {
ans1.push_back(u);
ans2.push_back(v);
x = p[u][v].first;
v = p[u][v].second;
u = x;
}
cout << ans1.size() - 1 << endl;
for (int i = 0; i < ans1.size(); i++) cout << ans2[i] << ' ';
cout << endl;
for (int i = 0; i < ans1.size(); i++) cout << ans1[i] << ' ';
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> E[505];
int d[501][501][2];
int p[501][501][2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
E[a].push_back(b), E[b].push_back(a);
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) d[i][j][0] = d[i][j][1] = 1e9;
d[1][n][0] = 0;
queue<tuple<int, int, int>> Q;
Q.emplace(1, n, 0);
while (!Q.empty()) {
auto [a, b, f] = Q.front();
Q.pop();
if (f == 0) {
for (auto x : E[a])
if (d[x][b][1] == 1e9) {
d[x][b][1] = d[a][b][0] + 1, Q.emplace(x, b, 1);
p[x][b][1] = a;
}
} else {
for (auto x : E[b])
if (d[a][x][0] == 1e9 and a != x) {
d[a][x][0] = d[a][b][1] + 1, Q.emplace(a, x, 0);
p[a][x][0] = b;
}
}
}
if (d[n][1][0] == 1e9)
cout << -1 << endl;
else {
cout << d[n][1][0] / 2 << endl;
vector<pair<int, int>> sol;
int a = n, b = 1;
for (;;) {
sol.emplace_back(a, b);
if (a == 1 and b == n) break;
int pb = p[a][b][0];
int pa = p[a][pb][1];
tie(a, b) = tie(pa, pb);
}
reverse(begin(sol), end(sol));
for (auto [a, b] : sol) cout << a << ' ';
cout << endl;
for (auto [a, b] : sol) cout << b << ' ';
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, f, u, v;
bool operator<(const node &t) const {
return (v > t.v) || ((v == t.v) && (u > t.u));
}
} temp;
priority_queue<node> pq;
int n, m, x, y, h[505], t[20005][2], w, q[505], rq, wq, d[505][2], f[505][2],
Q[250000][3];
bool b[505][505];
void adde(int x, int y) {
t[w][0] = y;
t[w][1] = h[x];
h[x] = w++;
}
void bfs(int s, int z) {
for (int i = 1; i <= n; i++) {
f[i][z] = -1;
}
d[s][z] = 0;
f[s][z] = 0;
q[0] = s;
rq = 0;
wq = 1;
while (rq != wq) {
x = q[rq];
for (int i = h[x]; i != -1; i = t[i][1]) {
y = t[i][0];
if (f[y][z] == -1) {
d[y][z] = d[x][z] + 1;
f[y][z] = x;
q[wq++] = y;
}
}
rq++;
}
}
void solve() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
b[i][j] = true;
}
b[i][i] = false;
}
temp.x = 1;
temp.y = n;
temp.f = -1;
temp.u = 0;
temp.v = temp.u + max(d[temp.x][1], d[temp.y][0]);
pq.push(temp);
wq = 0;
int X, Y, F, U, V;
while (!pq.empty()) {
temp = pq.top();
pq.pop();
X = temp.x;
Y = temp.y;
F = temp.f;
U = temp.u;
V = temp.v;
Q[wq][0] = X;
Q[wq][1] = Y;
Q[wq][2] = F;
wq++;
if ((X == n) && (Y == 1)) {
printf("%d\n", V);
for (int i = wq - 1, j = Q[i][2], k = Q[j][2];;) {
Q[j][2] = i;
if (k == -1) {
break;
}
i = j;
j = k;
k = Q[j][2];
}
for (int i = 0;; i = Q[i][2]) {
if (i) printf(" ");
printf("%d", Q[i][0]);
if (i == wq - 1) break;
}
printf("\n");
for (int i = 0;; i = Q[i][2]) {
if (i) printf(" ");
printf("%d", Q[i][1]);
if (i == wq - 1) break;
}
printf("\n");
return;
}
for (int i = h[X]; i != -1; i = t[i][1]) {
for (int j = h[Y]; j != -1; j = t[j][1]) {
if (b[t[i][0]][t[j][0]]) {
b[t[i][0]][t[j][0]] = false;
temp.x = t[i][0];
temp.y = t[j][0];
temp.f = wq - 1;
temp.u = U + 1;
temp.v = temp.u + max(d[temp.x][1], d[temp.y][0]);
pq.push(temp);
}
}
}
}
printf("-1\n");
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
h[i] = -1;
}
w = 0;
while (m--) {
scanf("%d%d", &x, &y);
adde(x, y);
adde(y, x);
}
bfs(1, 0);
bfs(n, 1);
if (f[n][0] == -1) {
printf("-1\n");
} else if (d[n][0] & 1) {
wq = 0;
for (int i = n;; i = f[i][0]) {
q[wq++] = i;
if (i == 1) {
break;
}
}
printf("%d\n", d[n][0]);
for (int i = wq - 1; i >= 0; i--) {
if (i != wq - 1) printf(" ");
printf("%d", q[i]);
}
printf("\n");
for (int i = 0; i < wq; i++) {
if (i) printf(" ");
printf("%d", q[i]);
}
printf("\n");
} else {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> a[509];
bool b[501][501][2];
pair<pair<int, int>, int> d[501][501][2];
int n, m;
void bfs() {
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(1, n), 0));
b[1][n][0] = true;
while (q.size() > 0) {
pair<pair<int, int>, int> p = q.front();
int A = p.first.first, B = p.first.second, k = p.second;
if (k == 0) {
for (int i = 0; i < a[A].size(); i++) {
if (not b[a[A][i]][B][1]) {
q.push(make_pair(make_pair(a[A][i], B), 1));
d[a[A][i]][B][1] = make_pair(make_pair(A, B), 0);
b[a[A][i]][B][1] = true;
}
}
} else {
for (int i = 0; i < a[B].size(); i++) {
if (not b[A][a[B][i]][0] and a[B][i] != A) {
q.push(make_pair(make_pair(A, a[B][i]), 0));
d[A][a[B][i]][0] = make_pair(make_pair(A, B), 1);
b[A][a[B][i]][0] = true;
}
}
}
q.pop();
}
return;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
bfs();
if (b[n][1][0]) {
vector<int> ansa, ansb;
pair<pair<int, int>, int> p2 = make_pair(make_pair(n, 1), 0);
while (p2 != make_pair(make_pair(1, n), 0)) {
if (p2.second == 0) {
ansa.push_back(p2.first.first);
ansb.push_back(p2.first.second);
}
p2 = d[p2.first.first][p2.first.second][p2.second];
}
ansa.push_back(1);
ansb.push_back(n);
int l = ansa.size();
cout << l - 1 << endl;
for (int i = l - 1; i >= 0; i--) cout << ansa[i] << ' ';
cout << endl;
for (int i = l - 1; i >= 0; i--) cout << ansb[i] << ' ';
} else {
cout << -1;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 3;
int n, m;
int dis[MAXN][MAXN][2];
vector<int> adj[MAXN];
int par[MAXN][MAXN][2];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dis[i][j][0] = 1e9;
dis[i][j][1] = 1e9;
par[i][j][0] = -1;
par[i][j][1] = -1;
}
}
dis[0][n - 1][0] = 0;
queue<pair<pair<int, int>, int> > s;
s.push({{0, n - 1}, 0});
while (s.size()) {
int v = s.front().first.first;
int u = s.front().first.second;
int bl = s.front().second;
s.pop();
if (v != u || bl) {
if (!bl) {
for (int i = 0; i < adj[v].size(); i++) {
int p = adj[v][i];
if (dis[v][u][bl] + 1 < dis[p][u][!bl]) {
dis[p][u][!bl] = dis[v][u][bl] + 1;
par[p][u][!bl] = v;
s.push({{p, u}, !bl});
}
}
} else {
for (int i = 0; i < adj[u].size(); i++) {
int p = adj[u][i];
if (dis[v][u][bl] + 1 < dis[v][p][!bl]) {
dis[v][p][!bl] = dis[v][u][bl] + 1;
par[v][p][!bl] = u;
s.push({{v, p}, !bl});
}
}
}
}
}
if (dis[n - 1][0][0] >= 1e9) {
cout << -1 << endl;
return 0;
} else {
vector<int> vec1, vec2;
vec1.push_back(n - 1);
vec2.push_back(0);
int a = n - 1;
int b = 0;
while (par[a][b][0] != -1) {
int aa = par[a][b][0];
b = aa;
int bb = par[a][b][1];
a = bb;
vec1.push_back(a), vec2.push_back(b);
}
cout << dis[n - 1][0][0] / 2 << endl;
while (vec1.size()) {
cout << vec1.back() + 1 << " ";
vec1.pop_back();
}
cout << endl;
while (vec2.size()) {
cout << vec2.back() + 1 << " ";
vec2.pop_back();
}
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int nax = 505;
vector<int> g[nax];
int vis[nax][nax];
array<int, 2> p[nax][nax];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<array<int, 2>> q{{1, n}};
vis[1][n] = 1;
for (int i = 0; i < int(q.size()); i++) {
auto [u, v] = q[i];
if (u == n && v == 1) {
break;
}
for (int nu : g[u]) {
for (int nv : g[v])
if (nu != nv) {
if (!vis[nu][nv]) {
vis[nu][nv] = 1;
q.push_back({nu, nv});
p[nu][nv] = {u, v};
}
}
}
}
if (!vis[n][1]) {
cout << "-1\n";
exit(0);
}
vector<vector<int>> path(2);
array<int, 2> guys = {n, 1};
while (guys[0] != 1 || guys[1] != n) {
path[0].push_back(guys[0]);
path[1].push_back(guys[1]);
guys = p[guys[0]][guys[1]];
}
path[0].push_back(1);
path[1].push_back(n);
cout << path[0].size() - 1 << "\n";
for (int i = 0; i < int(2); i++) {
reverse(path[i].begin(), path[i].end());
for (int x : path[i]) {
cout << x << " ";
}
cout << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const int N = 5e2 + 7;
int n, m;
vector<int> ad[N];
int dis[N][N];
bool mrk[N][N];
pair<int, int> par[N][N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int v, u;
scanf("%d%d", &v, &u);
ad[v].push_back(u);
ad[u].push_back(v);
}
queue<pair<int, int> > q;
q.push({1, n});
mrk[1][n] = 1;
while (q.size()) {
int v = q.front().first;
int u = q.front().second;
q.pop();
for (auto vp : ad[v])
for (auto up : ad[u])
if (up != vp && !mrk[vp][up]) {
mrk[vp][up] = 1;
par[vp][up] = {v, u};
dis[vp][up] = dis[v][u] + 1;
q.push({vp, up});
}
}
if (!mrk[n][1]) {
cout << -1;
return 0;
} else
cout << dis[n][1] << '\n';
int v = n, u = 1;
deque<int> res[3];
res[1].push_front(n);
res[2].push_front(1);
int sv, su;
while (v) {
sv = v, su = u;
v = par[sv][su].first;
u = par[sv][su].second;
if (v) {
res[1].push_front(v);
res[2].push_front(u);
}
}
for (auto x : res[1]) printf("%d ", x);
for (auto x : res[2]) printf("%d ", x);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Inf = 1000000000;
const int Maxn = 505;
const int Maxm = 10005;
int n, m;
vector<int> neigh[Maxn];
int dist[Maxn][Maxn][2];
int par[Maxn][Maxn][2];
int a[Maxn * Maxn], b[Maxn * Maxn];
int main() {
scanf("%d %d", &n, &m);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
neigh[a].push_back(b);
neigh[b].push_back(a);
}
fill((int*)dist, (int*)dist + Maxn * Maxn * 2, Inf);
dist[1][n][0] = 0;
queue<pair<pair<int, int>, int> > Q;
Q.push(pair<pair<int, int>, int>(pair<int, int>(1, n), 0));
while (!Q.empty()) {
pair<pair<int, int>, int> v = Q.front();
int d = dist[v.first.first][v.first.second][v.second];
Q.pop();
if (v.second == 0)
for (int i = 0; i < neigh[v.first.first].size(); i++) {
int u = neigh[v.first.first][i];
if (d < dist[u][v.first.second][1]) {
dist[u][v.first.second][1] = d;
par[u][v.first.second][1] = v.first.first;
Q.push(
pair<pair<int, int>, int>(pair<int, int>(u, v.first.second), 1));
}
}
else
for (int i = 0; i < neigh[v.first.second].size(); i++) {
int u = neigh[v.first.second][i];
if (v.first.first != u && d + 1 < dist[v.first.first][u][0]) {
dist[v.first.first][u][0] = d + 1;
par[v.first.first][u][0] = v.first.second;
Q.push(
pair<pair<int, int>, int>(pair<int, int>(v.first.first, u), 0));
}
}
}
if (dist[n][1][0] == Inf)
printf("-1\n");
else {
printf("%d\n", dist[n][1][0]);
pair<pair<int, int>, int> v =
pair<pair<int, int>, int>(pair<int, int>(n, 1), 0);
for (int i = dist[n][1][0]; i >= 0; i--) {
b[i] = v.first.second;
v = pair<pair<int, int>, int>(
pair<int, int>(v.first.first,
par[v.first.first][v.first.second][v.second]),
1);
a[i] = v.first.first;
v = pair<pair<int, int>, int>(
pair<int, int>(par[v.first.first][v.first.second][v.second],
v.first.second),
0);
}
for (int i = 0; i <= dist[n][1][0]; i++)
printf("%d%c", a[i], i + 1 <= dist[n][1][0] ? ' ' : '\n');
for (int i = 0; i <= dist[n][1][0]; i++)
printf("%d%c", b[i], i + 1 <= dist[n][1][0] ? ' ' : '\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7, maxn = 5e2 + 500;
bool visited[maxn][maxn][2];
int dp[maxn][maxn][2], n;
pair<pair<int, int>, bool> par[maxn][maxn][2];
vector<int> v[maxn];
queue<pair<pair<int, int>, bool> > qu;
void bfs(int a, int b) {
visited[a][b][0] = 1;
qu.push(make_pair(make_pair(a, b), 0));
par[a][b][0] = make_pair(make_pair(-1, -1), 1);
dp[a][b][0] = 0;
while (!qu.empty()) {
int a = qu.front().first.first;
int b = qu.front().first.second;
int c = qu.front().second;
if (c == 0) {
for (auto w : v[a]) {
if (!visited[w][b][1]) {
dp[w][b][1] = dp[a][b][0];
qu.push(make_pair(make_pair(w, b), 1));
visited[w][b][1] = 1;
par[w][b][1] = make_pair(make_pair(a, b), 0);
}
}
} else {
for (auto w : v[b]) {
if (w == a) continue;
if (!visited[a][w][0]) {
dp[a][w][0] = dp[a][b][1] + 1;
qu.push(make_pair(make_pair(a, w), 0));
visited[a][w][0] = 1;
par[a][w][0] = make_pair(make_pair(a, b), 1);
}
}
}
qu.pop();
}
}
int main() {
ios_base::sync_with_stdio(false);
int m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, w;
cin >> u >> w;
v[u].push_back(w);
v[w].push_back(u);
}
memset(dp, -1, sizeof dp);
bfs(1, n);
if (dp[n][1][0] != -1) {
cout << dp[n][1][0] << endl;
vector<int> ans1, ans2;
ans2.push_back(1);
int a = n, b = 1, c = 0;
while (a != -1 and b != -1) {
if (c == 0)
ans1.push_back(a);
else
ans2.push_back(b);
int tmp = par[a][b][c].first.first;
int tm = par[a][b][c].first.second;
a = tmp;
b = tm;
c ^= 1;
}
for (int i = ans1.size() - 1; i >= 0; i--) cout << ans1[i] << " ";
cout << endl;
for (int i = ans2.size() - 1; i >= 0; i--) cout << ans2[i] << " ";
cout << endl;
} else
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 100 + 13, MAXT = 2 + 13, MAXD = 1 * 1000 + 13;
bool mark[MAXN][MAXN][MAXT];
int dist[MAXN][MAXN][MAXT];
pair<pair<int, int>, int> par[MAXN][MAXN][MAXT];
queue<pair<pair<int, int>, int> > bfsq;
vector<int> adj[MAXN], bob, alex;
int main() {
int n, m;
cin >> n >> m;
int a, b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dist[i][j][1] = dist[i][j][2] = MAXD;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dist[1][n][1] = 0;
mark[1][n][1] = true;
bfsq.push(make_pair(make_pair(1, n), 1));
int t;
while (!bfsq.empty()) {
b = bfsq.front().first.first, a = bfsq.front().first.second,
t = bfsq.front().second;
if (t == 1) {
for (auto i : adj[b]) {
if (!mark[i][a][2]) {
par[i][a][2] = bfsq.front();
dist[i][a][2] = dist[b][a][1] + 1;
mark[i][a][2] = true;
bfsq.push(make_pair(make_pair(i, a), 2));
}
}
} else {
for (auto i : adj[a]) {
if (!mark[b][i][1] and b != i) {
par[b][i][1] = bfsq.front();
dist[b][i][1] = dist[b][a][2] + 1;
mark[b][i][1] = true;
bfsq.push(make_pair(make_pair(b, i), 1));
}
}
}
bfsq.pop();
}
if (dist[n][1][1] > 1 * 1000) {
cout << -1 << endl;
return 0;
}
cout << dist[n][1][1] / 2 << endl;
pair<pair<int, int>, int> tmp = {{n, 1}, 1}, begin = {{1, n}, 1};
while (tmp != begin) {
if (tmp.second == 1) {
bob.push_back(tmp.first.first);
alex.push_back(tmp.first.second);
}
tmp = par[tmp.first.first][tmp.first.second][tmp.second];
}
bob.push_back(1);
alex.push_back(n);
reverse(bob.begin(), bob.end());
reverse(alex.begin(), alex.end());
for (auto i : bob) cout << i << " ";
cout << endl;
for (auto i : alex) cout << i << " ";
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1LL * 1000 * 1000 * 1000 + 7;
const int inf = 1000 * 1000 * 1000;
const int rad = 1000;
const int maxn = 500 + 10;
int dp[maxn][maxn][3];
bool mark[maxn][maxn][3];
vector<int> adj[maxn];
struct state {
int fi, se, mod;
};
state par[maxn][maxn][3];
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int fi, se;
cin >> fi >> se;
adj[fi].push_back(se);
adj[se].push_back(fi);
}
for (int i = 0; i < maxn; ++i)
for (int j = 0; j < maxn; ++j)
for (int k = 0; k < 3; ++k) dp[i][j][k] = inf;
dp[n][1][1] = 0;
mark[n][1][1] = 1;
queue<state> q;
state A;
A.fi = n, A.se = 1, A.mod = 1;
q.push(A);
while (q.size()) {
state now = q.front();
q.pop();
int mod = now.mod, fi = now.fi, se = now.se;
if (mod == 1) {
for (int i = 0; i < adj[se].size(); ++i) {
int u = adj[se][i];
if (fi != se && mark[fi][u][2] == 0) {
mark[fi][u][2] = 1;
par[fi][u][2] = now;
dp[fi][u][2] = dp[fi][se][1] + 1;
A.fi = fi, A.se = u, A.mod = 2;
q.push(A);
}
}
} else {
for (int i = 0; i < adj[fi].size(); ++i) {
int u = adj[fi][i];
if (u != se && mark[u][se][1] == 0) {
mark[u][se][1] = 1;
dp[u][se][1] = dp[fi][se][2];
par[u][se][1] = now;
A.fi = u, A.se = se, A.mod = 1;
q.push(A);
}
}
}
}
if (dp[1][n][1] == inf) {
cout << -1 << endl;
return 0;
}
cout << dp[1][n][1] << endl << 1 << ' ';
A.fi = 1, A.se = n, A.mod = 1;
vector<int> ans[3];
while (true) {
if (A.fi == n && A.se == 1 && A.mod == 1) break;
state P = par[A.fi][A.se][A.mod];
if (A.mod == 1)
ans[1].push_back(P.fi);
else
ans[2].push_back(P.se);
A = P;
}
for (int i = 0; i < ans[1].size(); ++i) cout << ans[1][i] << ' ';
cout << endl << n << ' ';
for (int i = 0; i < ans[2].size(); ++i) cout << ans[2][i] << ' ';
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 11, inf = 1e9 + 10;
int n, m, v, u;
vector<int> g[N];
bool mark[N][N];
pair<int, int> dis[N][N], par[N][N];
queue<pair<int, int> > q;
vector<int> alex, bob;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> v >> u;
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) dis[i][j] = {inf, inf};
dis[0][n - 1].first = dis[0][n - 1].second = 0;
q.push({0, n - 1});
mark[0][n - 1] = true;
while (!q.empty()) {
int a = q.front().first, b = q.front().second;
q.pop();
for (int v : g[a])
for (int u : g[b]) {
if (mark[v][u]) continue;
if (v == u) continue;
if (max(dis[v][u].first, dis[v][u].second) >
1 + max(dis[a][b].first, dis[a][b].second)) {
dis[v][u].first = dis[a][b].first + 1;
dis[v][u].second = dis[a][b].second + 1;
par[v][u] = {a, b};
}
q.push({v, u});
mark[v][u] = true;
}
}
if (max(dis[n - 1][0].first, dis[n - 1][0].second) == inf)
return cout << -1, 0;
int ans = max(dis[n - 1][0].first, dis[n - 1][0].second);
cout << ans << '\n';
int a = n - 1, b = 0;
while (alex.size() < ans) {
alex.push_back(a);
bob.push_back(b);
pair<int, int> o = par[a][b];
a = o.first, b = o.second;
}
alex.push_back(0);
bob.push_back(n - 1);
reverse(alex.begin(), alex.end());
reverse(bob.begin(), bob.end());
for (int i : alex) cout << i + 1 << ' ';
cout << '\n';
for (int i : bob) cout << i + 1 << ' ';
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
struct state {
int x, y, d;
state() {}
state(int a, int b, int c) {
x = a;
y = b;
d = c;
}
};
int n, m, vis[501][501][2];
state P[501][501][2];
vector<vector<int> > v;
vector<int> ans1, ans2;
void solve() {
int x = n - 1, y = 0, d = 0;
ans1.push_back(x);
ans2.push_back(y);
while (x || y != n - 1 || d) {
state &p = P[x][y][d];
x = p.x;
y = p.y;
d = p.d;
if (!d)
ans1.push_back(x);
else
ans2.push_back(y);
}
for (int i = ans1.size() - 1; i >= 0; --i) printf("%d ", ans1[i] + 1);
printf("\n");
for (int i = ans2.size() - 1; i >= 0; --i) printf("%d ", ans2[i] + 1);
printf("\n");
}
void bfs() {
queue<state> q;
q.push(state(0, n - 1, 0));
vis[0][n - 1][0] = 1;
for (int i = 0; i < n; ++i) vis[i][i][0] = 1;
while (!q.empty()) {
state p = q.front();
q.pop();
int x = p.x, y = p.y, d = p.d, cst = vis[x][y][d];
if (x == n - 1 && !y && !d) {
printf("%d\n", cst - 1);
solve();
return;
}
if (!d)
for (int i = 0; i < v[x].size(); ++i) {
int nx = v[x][i];
if (vis[nx][y][1]) continue;
vis[nx][y][1] = cst;
P[nx][y][1] = p;
q.push(state(nx, y, 1));
}
else
for (int j = 0; j < v[y].size(); ++j) {
int ny = v[y][j];
if (vis[x][ny][0]) continue;
vis[x][ny][0] = cst + 1;
P[x][ny][0] = p;
q.push(state(x, ny, 0));
}
}
cout << -1 << endl;
}
int main() {
cin >> n >> m;
v.resize(n);
for (int a, b, i = 0; i < m; ++i) {
scanf("%d%d", &a, &b);
--a;
--b;
v[a].push_back(b);
v[b].push_back(a);
}
bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 560;
int dis[maxn][maxn];
vector<int> g[maxn], ans1, ans2;
queue<int> q;
pair<int, int> p[maxn][maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, x, y;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dis[1][n] = 1;
q.push(1);
q.push(n);
int u, v;
p[1][n] = {0, 0};
while (q.size()) {
u = q.front();
q.pop();
v = q.front();
q.pop();
if (u == v) continue;
for (auto e : g[u]) {
for (auto w : g[v]) {
if (!dis[e][w]) {
dis[e][w] = dis[u][v] + 1;
q.push(e);
q.push(w);
p[e][w] = {u, v};
}
}
}
}
if (!dis[n][1]) {
cout << -1;
return 0;
}
u = n;
v = 1;
while (u + v > 0) {
ans1.push_back(u);
ans2.push_back(v);
x = p[u][v].first;
v = p[u][v].second;
u = x;
}
cout << ans1.size() - 1 << endl;
for (int i = 0; i < ans1.size(); i++) cout << ans2[i] << ' ';
cout << '\n';
for (int i = 0; i < ans1.size(); i++) cout << ans1[i] << ' ';
}
|
#include <bits/stdc++.h>
using namespace std;
class State {
public:
int t, i, j;
State(int ti, int ii, int ji) {
t = ti;
i = ii;
j = ji;
}
};
int main() {
int n, m;
cin >> n >> m;
vector<int> adj[n];
for (int k = 0; k < m; k++) {
int i, j;
cin >> i >> j;
i--;
j--;
adj[i].push_back(j);
adj[j].push_back(i);
}
int p[2][n][n];
memset(p, -1, sizeof p);
bool v[2][n][n];
memset(v, false, sizeof v);
v[0][0][n - 1] = true;
queue<State> q;
q.push(State(0, 0, n - 1));
while (!q.empty()) {
State st = q.front();
q.pop();
if (st.t == 0 && st.i == n - 1 && st.j == 0) {
vector<int> res[2];
int nowi = n - 1, nowj = 0, nowt = 0;
while (p[nowt][nowi][nowj] != -1) {
if (nowt == 0) {
res[1].push_back(nowj);
nowj = p[nowt][nowi][nowj];
nowt = 1 - nowt;
} else {
res[0].push_back(nowi);
nowi = p[nowt][nowi][nowj];
nowt = 1 - nowt;
}
}
res[0].push_back(0);
res[1].push_back(n - 1);
cout << res[0].size() - 1 << endl;
for (int i = res[0].size() - 1; i >= 0; i--)
if (i == res[0].size() - 1)
cout << 1 + res[0][i];
else
cout << " " << 1 + res[0][i];
cout << endl;
for (int i = res[1].size() - 1; i >= 0; i--)
if (i == res[1].size() - 1)
cout << 1 + res[1][i];
else
cout << " " << 1 + res[1][i];
cout << endl;
return 0;
}
if (st.t == 0) {
for (int ii = 0; ii < adj[st.i].size(); ii++) {
int i = adj[st.i][ii];
if (!v[1 - st.t][i][st.j]) {
v[1 - st.t][i][st.j] = true;
p[1 - st.t][i][st.j] = st.i;
q.push(State(1 - st.t, i, st.j));
}
}
} else {
for (int ji = 0; ji < adj[st.j].size(); ji++) {
int j = adj[st.j][ji];
if (j == st.i) continue;
if (!v[1 - st.t][st.i][j]) {
v[1 - st.t][st.i][j] = true;
p[1 - st.t][st.i][j] = st.j;
q.push(State(1 - st.t, st.i, j));
}
}
}
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
int Map[502][502];
int xline[255000], yline[255000], faline[255000], l, r, next[505][505], now;
int main() {
int n, m, a, b;
scanf("%d%d", &n, &m);
memset(next, 0, sizeof(next));
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
next[a][++next[a][0]] = b;
next[b][++next[b][0]] = a;
}
l = r = 1;
xline[l] = 1;
yline[l] = n;
memset(Map, -1, sizeof(Map));
Map[1][n] = 0;
int fina = 0;
while (l <= r) {
int nowx = xline[l];
int nowy = yline[l];
for (int i = 1; i <= next[nowx][0]; i++)
for (int j = 1; j <= next[nowy][0]; j++) {
int nextx = next[nowx][i];
int nexty = next[nowy][j];
if (Map[nextx][nexty] == -1 && nextx != nexty) {
Map[nextx][nexty] = Map[nowx][nowy] + 1;
r++;
xline[r] = nextx;
yline[r] = nexty;
faline[r] = l;
if (nextx == n && nexty == 1) fina = r;
}
}
if (fina) break;
l++;
}
printf("%d\n", Map[n][1]);
if (Map[n][1] == -1) return 0;
now = fina;
while (now > 1) {
printf("%d ", yline[now]);
now = faline[now];
}
printf("%d\n", yline[now]);
now = fina;
while (now > 1) {
printf("%d ", xline[now]);
now = faline[now];
}
printf("%d\n", xline[now]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 500 + 10;
vector<int> adj[maxN];
bool mark[maxN][maxN][2];
pair<int, int> par[maxN][maxN][2];
int n, m, res;
vector<int> res1, res2;
void make_ans(int v, int u, int w, int left) {
if (!left) return;
if (w == 0) {
res1.push_back(v);
make_ans(par[v][u][w].first, par[v][u][w].second, w ^ 1, left);
} else {
res2.push_back(u);
make_ans(par[v][u][w].first, par[v][u][w].second, w ^ 1, left - 1);
}
}
void bfs() {
queue<pair<pair<int, int>, pair<int, int> > > q;
q.push(make_pair(make_pair(1, n), make_pair(0, 0)));
mark[1][n][0] = true;
par[1][n][0] = make_pair(-1, -1);
while (!q.empty()) {
pair<pair<int, int>, pair<int, int> > cur = q.front();
q.pop();
int v = cur.first.first, u = cur.first.second;
int w = cur.second.first, dis = cur.second.second;
if (v == n && u == 1 && w == 0) {
cout << dis << endl;
make_ans(v, u, 0, dis);
res1.push_back(1);
reverse(res1.begin(), res1.end());
for (int i = 0; i < res1.size(); i++) cout << res1[i] << " ";
cout << endl;
reverse(res2.begin(), res2.end());
res2.push_back(1);
for (int i = 0; i < res2.size(); i++) cout << res2[i] << " ";
cout << endl;
exit(0);
}
if (w == 0) {
for (int i = 0; i < adj[v].size(); i++) {
int now = adj[v][i];
if (!mark[now][u][1]) {
mark[now][u][1] = true;
par[now][u][1] = make_pair(v, u);
q.push(make_pair(make_pair(now, u), make_pair(1, dis)));
}
}
}
if (w == 1) {
for (int i = 0; i < adj[u].size(); i++) {
int now = adj[u][i];
if (!mark[v][now][0] && v != now) {
mark[v][now][0] = true;
par[v][now][0] = make_pair(v, u);
q.push(make_pair(make_pair(v, now), make_pair(0, dis + 1)));
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
bfs();
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 10;
int n, m, h[MAXN][MAXN];
pair<int, int> p[MAXN][MAXN];
vector<int> g[MAXN];
void bfs(pair<int, int> src) {
queue<pair<int, int>> q;
memset(h, -1, sizeof h);
h[src.first][src.second] = 0;
q.push(src);
while (!q.empty()) {
int v1 = (q.front()).first, v2 = (q.front()).second;
q.pop();
for (auto u1 : g[v1]) {
for (auto u2 : g[v2]) {
if (u1 != u2 && h[u1][u2] == -1) {
h[u1][u2] = h[v1][v2] + 1;
p[u1][u2] = {v1, v2};
q.push({u1, u2});
}
}
}
}
}
void print(int a, int b, int c) {
if (a == 0 && b == 0) return;
if (c) {
cout << a << ' ';
} else {
cout << b << ' ';
}
return print(p[a][b].first, p[a][b].second, c);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
g[u].push_back(v);
}
bfs({1, n});
cout << h[n][1] << '\n';
if (h[n][1] > 0) {
print(n, 1, 0);
cout << '\n';
print(n, 1, 1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 505, inf = 1e18, mod = 1e9 + 7;
vector<long long> vec[N];
vector<long long> ans1, ans2;
queue<pair<int, pair<int, int> > > q;
long long dis[2][N][N];
pair<int, pair<int, int> > par[2][N][N];
void bfs(long long n) {
q.push({0, {1, n}});
dis[0][1][n] = 0;
while (q.size()) {
int t = q.front().first;
int u = q.front().second.first;
int v = q.front().second.second;
q.pop();
if (t == 0) {
for (auto e : vec[u]) {
if (dis[1][e][v] == -1) {
dis[1][e][v] = dis[0][u][v] + 1;
par[1][e][v] = {0, {u, v}};
q.push({1, {e, v}});
}
}
} else {
for (auto e : vec[v]) {
if (dis[0][u][e] == -1 && u != e) {
dis[0][u][e] = dis[1][u][v] + 1;
par[0][u][e] = {1, {u, v}};
q.push({0, {u, e}});
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dis[0][i][j] = -1;
dis[0][j][i] = -1;
dis[1][i][j] = -1;
dis[1][j][i] = -1;
}
}
bfs(n);
if (dis[0][n][1] == -1) return cout << -1, 0;
cout << dis[0][n][1] / 2 << '\n';
pair<int, pair<int, int> > x = {0, {n, 1}};
while (x.first != 0 || x.second.first != 1 || x.second.second != n) {
int t = x.first;
int u = x.second.first;
int v = x.second.second;
ans1.push_back(u);
ans2.push_back(v);
x.first = par[t][u][v].first;
x.second.first = par[t][u][v].second.first;
x.second.second = par[t][u][v].second.second;
}
ans1.push_back(x.second.first);
ans2.push_back(x.second.second);
for (int i = 0; i < ans2.size(); i += 2) cout << ans2[i] << " ";
cout << '\n';
for (int i = 0; i < ans1.size(); i += 2) cout << ans1[i] << " ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 555;
int n, m, dist[2][N][N];
pair<int, int> pred[2][N][N];
vector<int> gr[N];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
gr[a].push_back(b);
gr[b].push_back(a);
}
queue<vector<int> > q;
dist[0][1][n] = 1;
pred[0][1][n] = {-1, -1};
q.push({0, 1, n});
while (!q.empty()) {
auto vc = q.front();
q.pop();
int type = vc[0], v = vc[1], u = vc[2];
if (type == 0) {
for (auto to : gr[v]) {
if (!dist[1][to][u]) {
dist[1][to][u] = dist[0][v][u];
pred[1][to][u] = {v, u};
q.push({1, to, u});
}
}
} else {
for (auto to : gr[u]) {
if (v != to && !dist[0][v][to]) {
dist[0][v][to] = dist[1][v][u] + 1;
pred[0][v][to] = {v, u};
q.push({0, v, to});
}
}
}
}
if (!dist[0][n][1]) {
cout << -1;
} else {
vector<int> way1, way2;
int v = n, u = 1, t = 0;
while (pred[t][v][u] != make_pair(-1, -1)) {
if (!t) {
way1.push_back(v);
way2.push_back(u);
}
auto pp = pred[t][v][u];
v = pp.first;
u = pp.second;
t ^= 1;
}
way1.push_back(v);
way2.push_back(u);
reverse(way1.begin(), way1.end());
reverse(way2.begin(), way2.end());
cout << dist[0][n][1] - 1 << endl;
for (auto c : way1) cout << c << ' ';
cout << endl;
for (auto c : way2) cout << c << ' ';
}
}
|
#include <bits/stdc++.h>
using namespace std;
struct state {
int a, b, turn, moves;
state() {}
state(int x, int y, int z, int u) : a(x), b(y), turn(z), moves(u) {}
bool operator<(const state& other) const { return this->moves < other.moves; }
};
int d[501][501][2];
state predecessor[501][501][2];
queue<state> frontier;
vector<int> adj[501];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(d, -1, sizeof d);
frontier.push(state(n, 1, 0, 1));
d[n][1][0] = 0;
while (!frontier.empty()) {
state u = frontier.front();
frontier.pop();
if (u.turn == 0) {
for (int j = 0; j < adj[u.a].size(); j++)
if (d[adj[u.a][j]][u.b][1] == -1 ||
d[u.a][u.b][0] + 1 < d[adj[u.a][j]][u.b][1]) {
predecessor[adj[u.a][j]][u.b][1] = u;
d[adj[u.a][j]][u.b][1] = d[u.a][u.b][u.turn] + 1;
frontier.push(state(adj[u.a][j], u.b, 1, d[u.a][u.b][u.turn] + 1));
}
} else if (u.turn == 1) {
for (int j = 0; j < adj[u.b].size(); j++) {
if (adj[u.b][j] == u.a) continue;
if (d[u.a][adj[u.b][j]][0] == -1 ||
d[u.a][u.b][1] + 1 < d[u.a][adj[u.b][j]][0]) {
predecessor[u.a][adj[u.b][j]][0] = u;
d[u.a][adj[u.b][j]][0] = d[u.a][u.b][u.turn] + 1;
frontier.push(state(u.a, adj[u.b][j], 0, d[u.a][u.b][u.turn] + 1));
}
}
}
}
if (d[1][n][0] == -1)
cout << -1 << endl;
else {
cout << d[1][n][0] / 2 << '\n';
stack<int> alex;
stack<int> bob;
state u = state(1, n, 0, -1);
for (int i = 0; i < d[1][n][0]; i++) {
if (u.turn == 0)
bob.push(u.b);
else if (u.turn == 1)
alex.push(u.a);
u = predecessor[u.a][u.b][u.turn];
}
cout << 1;
while (!bob.empty()) {
cout << ' ' << bob.top();
bob.pop();
}
cout << '\n';
cout << n;
while (!alex.empty()) {
cout << ' ' << alex.top();
alex.pop();
}
cout << endl;
}
cout << flush;
return 0;
}
|
#include <bits/stdc++.h>
int Map[502][502];
int xline[255000], yline[255000], faline[255000], l, r, next[505][505], now;
int main() {
int n, m, a, b;
scanf("%d%d", &n, &m);
memset(next, 0, sizeof(next));
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
next[a][++next[a][0]] = b;
next[b][++next[b][0]] = a;
}
l = r = 1;
xline[l] = 1;
yline[l] = n;
memset(Map, -1, sizeof(Map));
Map[1][n] = 0;
int fina = 0;
while (l <= r) {
int nowx = xline[l];
int nowy = yline[l];
for (int i = 1; i <= next[nowx][0]; i++)
for (int j = 1; j <= next[nowy][0]; j++) {
int nextx = next[nowx][i];
int nexty = next[nowy][j];
if (Map[nextx][nexty] == -1 && nextx != nexty) {
Map[nextx][nexty] = Map[nowx][nowy] + 1;
r++;
xline[r] = nextx;
yline[r] = nexty;
faline[r] = l;
if (nextx == n && nexty == 1) fina = r;
}
}
if (fina) break;
l++;
}
printf("%d\n", Map[n][1]);
if (Map[n][1] == -1) return 0;
now = fina;
while (now > 1) {
printf("%d ", yline[now]);
now = faline[now];
}
printf("%d\n", yline[now]);
now = fina;
while (now > 1) {
printf("%d ", xline[now]);
now = faline[now];
}
printf("%d\n", xline[now]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 501;
int n, m;
vector<int> v[N], path[2];
struct GRAPH {
int a, b, who;
};
GRAPH previ[N][N][2];
bool odw[N][N][2], found;
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
queue<GRAPH> q;
q.push({1, n, 0});
while (q.size() && !found) {
GRAPH curr = q.front();
q.pop();
if (curr.who == 0)
for (int i = 0; i < v[curr.a].size(); ++i) {
GRAPH u;
u.a = v[curr.a][i];
u.b = curr.b;
u.who = 1;
if (!odw[u.a][u.b][u.who]) {
odw[u.a][u.b][u.who] = 1;
previ[u.a][u.b][u.who] = curr;
q.push(u);
if (u.a == n && u.b == 1 && u.who == 0) {
found = 1;
break;
}
}
}
else
for (int i = 0; i < v[curr.b].size(); ++i) {
GRAPH u;
u.a = curr.a;
u.b = v[curr.b][i];
u.who = 0;
if (u.a == u.b) continue;
if (!odw[u.a][u.b][u.who]) {
odw[u.a][u.b][u.who] = 1;
previ[u.a][u.b][u.who] = curr;
q.push(u);
if (u.a == n && u.b == 1 && u.who == 0) {
found = 1;
break;
}
}
}
}
if (!found)
cout << "-1";
else {
GRAPH curr;
curr.a = n;
curr.b = 1;
curr.who = 0;
while (curr.a != 1 || curr.b != n || curr.who != 0) {
if (curr.who == 0) {
path[0].push_back(curr.a);
path[1].push_back(curr.b);
}
curr = previ[curr.a][curr.b][curr.who];
}
printf("%d\n1 ", (int)path[0].size());
for (int i = path[0].size() - 1; i >= 0; --i) printf("%d ", path[0][i]);
printf("\n%d ", n);
for (int i = path[1].size() - 1; i >= 0; --i) printf("%d ", path[1][i]);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, p[510][510][2];
vector<int> vr[510], ans, ans2;
queue<pair<pair<int, int>, bool> > qe;
bool bfs() {
qe.push(make_pair(make_pair(1, n), true));
p[1][n][true] = -1;
while (qe.size()) {
int v = qe.front().first.first, u = qe.front().first.second;
bool f = qe.front().second;
qe.pop();
if (f == true) {
for (int i = 0; i < vr[v].size(); i++) {
int v2 = vr[v][i];
if (p[v2][u][!f] == 0) {
p[v2][u][!f] = v;
qe.push(make_pair(make_pair(v2, u), !f));
}
}
} else
for (int i = 0; i < vr[u].size(); i++) {
int u2 = vr[u][i];
if (u2 != v && p[v][u2][!f] == 0) {
p[v][u2][!f] = u;
if (v == n && u2 == 1) return true;
qe.push(make_pair(make_pair(v, u2), !f));
}
}
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
vr[v].push_back(u);
vr[u].push_back(v);
}
if (bfs() == false)
cout << "-1\n";
else {
int v = n, u = 1;
bool f = true;
ans.push_back(v);
ans2.push_back(u);
while (v != 1 || u != n || f != true) {
if (f == true) {
u = p[v][u][f];
ans2.push_back(u);
} else {
v = p[v][u][f];
ans.push_back(v);
}
f = !f;
}
cout << ans.size() - 1 << '\n';
for (int i = ans.size() - 1; i > -1; i--) cout << ans[i] << ' ';
cout << '\n';
for (int i = ans2.size() - 1; i > -1; i--) cout << ans2[i] << ' ';
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 555;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int dh[5] = {1, 0, 0, -1};
int dc[5] = {0, 1, -1, 0};
const int oo = 1111111111;
vector<int> a[N];
int f[N][N], n, m;
pair<int, int> trace[N][N];
void solve() {
queue<pair<int, int> > s;
s.push(pair<int, int>(1, n));
f[1][n] = 0;
while (s.empty() != 1) {
int u = s.front().first;
int v = s.front().second;
s.pop();
if (u == n && v == 1) break;
for (int uu : a[u])
for (int vv : a[v]) {
if (f[uu][vv] == oo && uu != vv) {
f[uu][vv] = f[u][v] + 1;
trace[uu][vv] = pair<int, int>(u, v);
s.push(pair<int, int>(uu, vv));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = oo;
solve();
cout << (f[n][1] == oo ? -1 : f[n][1]);
if (f[n][1] != oo) {
int x = n;
int y = 1;
vector<int> A;
vector<int> B;
int dem = f[n][1];
while (dem >= 1) {
A.push_back(x);
B.push_back(y);
pair<int, int> X = trace[x][y];
x = X.first;
y = X.second;
dem--;
}
A.push_back(1);
B.push_back(n);
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
cout << "\n";
for (int V : A) cout << V << " ";
cout << "\n";
for (int V : B) cout << V << " ";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 510, inf = 1e12;
vector<int> adj[maxn], ans[3];
pair<pair<int, int>, int> p[maxn][maxn][2];
int l[maxn][maxn][2];
bool mark[maxn][maxn][2];
queue<pair<pair<int, int>, int> > q;
void show(pair<pair<int, int>, int> pr) {
int x = pr.first.first, y = pr.first.second, t = pr.second;
if (x == -1 & y == -1) return;
show(p[x][y][t]);
if (!t) {
ans[0].push_back(x);
ans[1].push_back(y);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int(i) = int(0); int(i) < int(m); ++i) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int(i) = int(0); int(i) < int(maxn); ++i)
for (int(j) = int(0); int(j) < int(maxn); ++j)
p[i][j][0] = p[i][j][1] = make_pair(make_pair(-1, -1), 0);
mark[0][n - 1][0] = true;
q.push(make_pair(make_pair(0, n - 1), 0));
while (!q.empty()) {
pair<pair<int, int>, int> pr = q.front();
q.pop();
int x = pr.first.first, y = pr.first.second, t = pr.second;
if (t) {
for (int(i) = int(0); int(i) < int(adj[y].size()); ++i) {
int next = adj[y][i];
if (!mark[x][next][1 - t] && next != x) {
p[x][next][1 - t] = make_pair(make_pair(x, y), t);
l[x][next][1 - t] = l[x][y][1];
mark[x][next][1 - t] = true;
q.push(make_pair(make_pair(x, next), 1 - t));
}
}
} else {
for (int(i) = int(0); int(i) < int(adj[x].size()); ++i) {
int next = adj[x][i];
if (!mark[next][y][1 - t]) {
p[next][y][1 - t] = make_pair(make_pair(x, y), t);
l[next][y][1 - t] = l[x][y][0] + 1;
mark[next][y][1 - t] = true;
q.push(make_pair(make_pair(next, y), 1 - t));
}
}
}
}
show(make_pair(make_pair(n - 1, 0), 0));
if (ans[0].size() == 1)
cout << -1 << endl;
else {
cout << l[n - 1][0][0] << endl;
for (int(i) = int(0); int(i) < int(2); ++i) {
for (int(j) = int(0); int(j) < int(ans[i].size()); ++j)
cout << ans[i][j] + 1 << ' ';
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 Rand(time(0));
const int maxn = 5e2 + 5;
const long long maxc = 1e9 + 7;
int n, m;
vector<int> adj[maxn];
int d[maxn][maxn];
pair<int, int> trace[maxn][maxn];
vector<int> resOne, resN;
void bfs() {
queue<pair<int, int> > q;
q.push(make_pair(1, n));
memset(d, (-1), sizeof(d));
d[1][n] = 0;
while (!q.empty()) {
int s = q.front().first;
int t = q.front().second;
q.pop();
for (auto u : adj[s]) {
for (auto v : adj[t]) {
if (d[u][v] != -1 || u == v) continue;
trace[u][v] = make_pair(s, t);
d[u][v] = d[s][t] + 1;
q.push(make_pair(u, v));
}
}
}
cout << d[n][1] << "\n";
if (d[n][1] == -1) return;
int s = 1, t = n;
while (t != 0) {
resOne.push_back(t);
resN.push_back(s);
pair<int, int> cur = trace[t][s];
t = cur.first, s = cur.second;
}
reverse((resOne).begin(), (resOne).end());
reverse((resN).begin(), (resN).end());
for (auto u : resOne) cout << u << " ";
cout << "\n";
for (auto u : resN) cout << u << " ";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e2 + 11;
int a, b, n, m, dp[maxn][maxn], t;
pair<int, int> par[maxn][maxn];
vector<int> adg[maxn];
bool mark[maxn][maxn];
queue<pair<int, int> > q;
vector<int> v;
vector<int> v10;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
adg[a].push_back(b);
adg[b].push_back(a);
}
q.push(make_pair(1, n));
mark[1][n] = 1;
par[1][n] = make_pair(-1, -1);
pair<int, int> p11;
while (!q.empty()) {
p11 = q.front();
mark[p11.first][p11.second] = 1;
for (int v1, i = 0; i < adg[p11.first].size(); i++) {
v1 = adg[p11.first][i];
for (int u1, j = 0; j < adg[p11.second].size(); j++) {
u1 = adg[p11.second][j];
if (!mark[v1][u1] && v1 != u1) {
q.push(make_pair(v1, u1));
mark[v1][u1] = 1;
dp[v1][u1] = dp[p11.first][p11.second] + 1;
par[v1][u1] = make_pair(p11.first, p11.second);
}
}
}
q.pop();
}
if (dp[n][1] == 0) {
cout << -1;
return 0;
}
printf("%d", dp[n][1]);
printf("\n");
pair<int, int> p10;
p10 = make_pair(n, 1);
v.push_back(n);
v10.push_back(1);
while (par[p10.first][p10.second].first != -1) {
p10 = par[p10.first][p10.second];
v.push_back(p10.first);
v10.push_back(p10.second);
}
for (int i = v.size() - 1; i >= 0; i--) {
printf("%d ", v[i]);
}
printf("\n");
for (int i = v10.size() - 1; i >= 0; i--) {
printf("%d ", v10[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int SIZE = 510;
struct Item {
int first, second;
Item() {}
Item(int a, int b) : first(a), second(b) {}
bool operator!=(const Item &a) const {
if (first == a.first && second == a.second) return false;
return true;
}
};
int V, E;
vector<int> vt[501];
int dis[SIZE][SIZE];
bool used[SIZE][SIZE];
Item father[SIZE][SIZE];
void SPFA();
int main() {
int u, v;
scanf("%d %d", &V, &E);
for (int i = 0; i < E; i++) {
scanf("%d %d", &u, &v);
vt[u].push_back(v);
vt[v].push_back(u);
}
SPFA();
if (dis[V][1] == 0x7f7f7f7f)
printf("-1\n");
else {
Item p, source(1, V);
printf("%d\n", dis[V][1]);
p.first = V;
p.second = 1;
while (p != source) {
printf("%d ", p.second);
p = father[p.first][p.second];
}
printf("%d\n", V);
p.first = V;
p.second = 1;
while (p != source) {
printf("%d ", p.first);
p = father[p.first][p.second];
}
printf("1\n");
}
return 0;
}
void SPFA() {
Item tmp;
queue<Item> que;
que.push(Item(1, V));
memset(dis, 0x7f, sizeof(dis));
dis[1][V] = 0;
while (!que.empty()) {
tmp = que.front();
que.pop();
int x = tmp.first, y = tmp.second;
int len1 = vt[x].size();
int len2 = vt[y].size();
for (int i = 0; i < len1; i++) {
int t1 = vt[x][i];
if (used[y][t1]) continue;
used[y][t1] = true;
for (int j = 0; j < len2; j++) {
int t2 = vt[y][j];
if (t1 != t2 && dis[t1][t2] == 0x7f7f7f7f) {
father[t1][t2] = tmp;
dis[t1][t2] = dis[x][y] + 1;
que.push(Item(t1, t2));
if (t1 == V && t2 == 1) return;
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
FILE *in;
FILE *out;
struct State {
int idx1, idx2;
int who, moves;
};
int vis[512][512][2];
int lst[512][512][2];
queue<State> q;
int n, m;
vector<int> v[512];
void bfs() {
State cur, next;
memset(lst, -1, sizeof(lst));
memset(vis, +0, sizeof(vis));
while (!q.empty()) q.pop();
next.moves = 0;
next.who = 0;
next.idx1 = 1;
next.idx2 = n;
q.push(next);
vis[next.idx1][next.idx2][next.who] = 1;
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.idx1 == n && cur.idx2 == 1 && cur.who == 0) break;
if (cur.who == 0) {
next.idx2 = cur.idx2;
next.moves = cur.moves + 1;
next.who = 1;
for (int i = 0; i < (int)v[cur.idx1].size(); i++) {
next.idx1 = v[cur.idx1][i];
if (!vis[next.idx1][next.idx2][next.who]) {
vis[next.idx1][next.idx2][next.who] = 1;
lst[next.idx1][next.idx2][next.who] = cur.idx1;
q.push(next);
}
}
} else {
next.idx1 = cur.idx1;
next.moves = cur.moves;
next.who = 0;
for (int i = 0; i < (int)v[cur.idx2].size(); i++) {
next.idx2 = v[cur.idx2][i];
if (next.idx1 == next.idx2) continue;
if (!vis[next.idx1][next.idx2][next.who]) {
vis[next.idx1][next.idx2][next.who] = 1;
lst[next.idx1][next.idx2][next.who] = cur.idx2;
q.push(next);
}
}
}
}
if (cur.idx1 != n || cur.idx2 != 1 || cur.who != 0)
fprintf(out, "-1\n");
else {
int pathLen = cur.moves;
fprintf(out, "%d\n", pathLen);
vector<int> p1, p2;
for (int i = 0; i < pathLen * 2 + 2; i++) {
if (cur.who == 0) {
p2.push_back(cur.idx2);
cur.idx2 = lst[cur.idx1][cur.idx2][cur.who];
cur.who = 1;
} else {
p1.push_back(cur.idx1);
cur.idx1 = lst[cur.idx1][cur.idx2][cur.who];
cur.who = 0;
}
}
reverse(p1.begin(), p1.end());
reverse(p2.begin(), p2.end());
for (int i = 0; i < (int)p1.size(); i++)
fprintf(out, "%d%c", p1[i], i + 1 == (int)p1.size() ? '\n' : ' ');
for (int i = 0; i < (int)p2.size(); i++)
fprintf(out, "%d%c", p2[i], i + 1 == (int)p2.size() ? '\n' : ' ');
}
}
int main(void) {
in = stdin;
out = stdout;
fscanf(in, "%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int n1, n2;
fscanf(in, "%d %d", &n1, &n2);
v[n1].push_back(n2);
v[n2].push_back(n1);
}
bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
const int N = 5e2 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 2e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, d[N][N];
vi g[N];
pii pr[N][N];
int main() {
if (fopen("threesum"
".in",
"r"))
freopen(
"threesum"
".in",
"r", stdin),
freopen(
"threesum"
".out",
"w", stdout);
cin >> n >> m;
for (int i = 1; i <= m; i += 1) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
d[1][n] = 1;
queue<pii> q;
q.push({1, n});
while (!q.empty()) {
pii v = q.front();
q.pop();
for (int i : g[v.first]) {
for (int j : g[v.second]) {
if (!d[i][j] and i != j) {
pr[i][j] = v;
d[i][j] = d[v.first][v.second] + 1;
q.push({i, j});
}
}
}
}
if (d[n][1] == 0) return !(cout << -1);
int x = n, y = 1;
vi a, b;
while (x != 1 || y != 1) {
pii p = pr[x][y];
a.emplace_back(x);
b.emplace_back(y);
if (!p.first) break;
x = p.first;
y = p.second;
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
cout << a.size() - 1 << '\n';
for (int it : a) cout << it << ' ';
cout << '\n';
for (int it : b) cout << it << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 21, inf = 1e9 + 21;
int n, m;
int h[MAXN][MAXN];
pair<int, int> par[MAXN][MAXN];
pair<int, int> tmp;
queue<pair<int, int> > q;
vector<int> adj[MAXN];
vector<int> ans, res;
inline void bfs() {
while (!q.empty()) {
int u = q.front().first;
int v = q.front().second;
q.pop();
for (auto a : adj[u])
for (auto b : adj[v]) {
if (a != b && (h[a][b] > h[u][v] + 1)) {
h[a][b] = h[u][v] + 1;
par[a][b] = make_pair(u, v);
q.push(make_pair(a, b));
}
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
q.push(make_pair(1, n));
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) h[i][j] = inf;
h[1][n] = 0;
bfs();
if (h[n][1] == inf) return cout << -1 << '\n', 0;
for (int i = n, j = 1; i > 0 || j > 0;) {
tmp = par[i++][j++];
ans.push_back(j);
res.push_back(i);
i = tmp.first;
j = tmp.second;
}
cout << h[n][1] << '\n';
for (int y : ans) cout << y - 1 << ' ';
cout << '\n';
for (int y : res) cout << y - 1 << ' ';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 505, inf = 1e18, mod = 1e9 + 7;
vector<long long> vec[N];
vector<long long> ans1, ans2;
queue<pair<int, pair<int, int> > > q;
long long dis[2][N][N];
pair<int, pair<int, int> > par[2][N][N];
void bfs(long long n) {
q.push({0, {1, n}});
dis[0][1][n] = 0;
while (q.size()) {
int t = q.front().first;
int u = q.front().second.first;
int v = q.front().second.second;
q.pop();
if (t == 0) {
for (auto e : vec[u]) {
if (dis[1][e][v] == -1) {
dis[1][e][v] = dis[0][u][v] + 1;
par[1][e][v] = {0, {u, v}};
q.push({1, {e, v}});
}
}
} else {
for (auto e : vec[v]) {
if (dis[0][u][e] == -1 && u != e) {
dis[0][u][e] = dis[1][u][v] + 1;
par[0][u][e] = {1, {u, v}};
q.push({0, {u, e}});
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dis[0][i][j] = -1;
dis[0][j][i] = -1;
dis[1][i][j] = -1;
dis[1][j][i] = -1;
}
}
bfs(n);
if (dis[0][n][1] == -1) return cout << -1, 0;
cout << dis[0][n][1] / 2 << '\n';
pair<int, pair<int, int> > x = {0, {n, 1}};
while (x.first != 0 || x.second.first != 1 || x.second.second != n) {
int t = x.first;
int u = x.second.first;
int v = x.second.second;
ans1.push_back(u);
ans2.push_back(v);
x.first = par[t][u][v].first;
x.second.first = par[t][u][v].second.first;
x.second.second = par[t][u][v].second.second;
}
ans1.push_back(x.second.first);
ans2.push_back(x.second.second);
for (int i = 0; i < ans2.size(); i += 2) cout << ans2[i] << " ";
cout << '\n';
for (int i = 0; i < ans1.size(); i += 2) cout << ans1[i] << " ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int vist[502][502][2], n, m;
int pathRet[502][502][2];
vector<int> gra[502];
struct state {
int indA;
int indB;
int cantMov;
int pers;
};
queue<state> cola;
void bfs() {
vector<int> pathA, pathB;
memset(vist, 0, sizeof(vist));
memset(pathRet, 0, sizeof(pathRet));
state act, next;
act.indA = 1;
act.indB = n;
act.cantMov = 0;
act.pers = 0;
cola.push(act);
vist[act.indA][act.indB][act.pers] = 1;
while (!cola.empty()) {
act = cola.front();
cola.pop();
if (act.indA == n && act.indB == 1 && act.pers == 0) break;
if (act.pers == 0) {
next.indB = act.indB;
next.cantMov = act.cantMov + 1;
next.pers = 1;
for (int i = 0; i < gra[act.indA].size(); i++) {
next.indA = gra[act.indA][i];
if (!vist[next.indA][act.indB][next.pers]) {
vist[next.indA][act.indB][next.pers] = 1;
pathRet[next.indA][act.indB][next.pers] = act.indA;
cola.push(next);
}
}
} else {
next.indA = act.indA;
next.cantMov = act.cantMov;
next.pers = 0;
for (int i = 0; i < gra[act.indB].size(); i++) {
next.indB = gra[act.indB][i];
if (next.indA == next.indB) continue;
if (!vist[act.indA][next.indB][next.pers]) {
vist[act.indA][next.indB][next.pers] = 1;
pathRet[act.indA][next.indB][next.pers] = act.indB;
cola.push(next);
}
}
}
}
if (act.indA != n || act.indB != 1 || act.pers != 0)
printf("-1\n");
else {
for (int i = 0; i < act.cantMov * 2 + 2; i++) {
if (act.pers == 0) {
pathB.push_back(act.indB);
act.indB = pathRet[act.indA][act.indB][act.pers];
act.pers = 1;
} else {
pathA.push_back(act.indA);
act.indA = pathRet[act.indA][act.indB][act.pers];
act.pers = 0;
}
}
reverse(pathA.begin(), pathA.end());
reverse(pathB.begin(), pathB.end());
printf("%d\n", act.cantMov);
for (int i = 0; i < pathA.size(); i++)
printf("%d%c", pathA[i], i + 1 != pathA.size() ? ' ' : '\n');
for (int i = 0; i < pathB.size(); i++)
printf("%d%c", pathB[i], i + 1 != pathB.size() ? ' ' : '\n');
}
}
int main() {
int a, b;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
gra[a].push_back(b);
gra[b].push_back(a);
}
bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, m, dp[N][N];
queue<pair<int, int>> q;
vector<int> ans[2], adj[N];
pair<int, int> par[N][N], zero;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
adj[--u].push_back(--v);
adj[v].push_back(u);
}
memset(dp, 127, sizeof dp);
for (q.push({0, n - 1}), dp[0][n - 1] = 0; q.size(); q.pop()) {
auto [u, v] = q.front();
if (u == n - 1 && v == 0) {
cout << dp[u][v] << endl;
for (pair<int, int> x = {u, v}; x != zero; x = par[x.first][x.second]) {
ans[1].push_back(x.first);
ans[0].push_back(x.second);
}
for (int i = 0; i < 2; i++, cout << endl)
for (int x : ans[i]) cout << x + 1 << ' ';
return 0;
}
for (int x : adj[u])
for (int y : adj[v])
if (x ^ y && dp[u][v] + 1 < dp[x][y]) {
dp[x][y] = dp[u][v] + 1;
par[x][y] = {u, v};
q.push({x, y});
}
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[501][501][2], cost[501][501];
pair<int, int> pp[501][501];
vector<vector<int> > v;
vector<int> ans1, ans2;
void ans(int b, int a) {
if (b == 1 && a == n) {
ans1.push_back(b);
ans2.push_back(a);
return;
}
ans(pp[b][a].first, pp[b][a].second);
ans1.push_back(b);
ans2.push_back(a);
}
void BFS(int s, int p) {
queue<pair<pair<int, int>, pair<int, int> > > q;
q.push(make_pair(make_pair(s, p), make_pair(1, 0)));
cost[s][p] = 0;
vis[s][p][0] = 1;
while (!q.empty()) {
int b = q.front().first.first, a = q.front().first.second;
int k = q.front().second.first, c = q.front().second.second;
q.pop();
if (k) {
for (int i = 0; i < v[b].size(); i++) {
if (vis[v[b][i]][a][k]) continue;
vis[v[b][i]][a][k] = 1;
q.push({{v[b][i], a}, {!k, b}});
}
} else {
for (int i = 0; i < v[a].size(); i++) {
if (vis[b][v[a][i]][k] || v[a][i] == b) continue;
vis[b][v[a][i]][k] = 1;
cost[b][v[a][i]] = cost[c][a] + 1;
q.push({{b, v[a][i]}, {!k, b}});
pp[b][v[a][i]] = make_pair(c, a);
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
v.resize(n + 1);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
cost[n][1] = -1;
BFS(1, n);
printf("%d\n", cost[n][1]);
if (cost[n][1] == -1) return 0;
ans(n, 1);
for (int i = 0; i < ans1.size(); i++) printf("%d ", ans1[i]);
puts("");
for (int i = 0; i < ans2.size(); i++) printf("%d ", ans2[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 1000 + 10;
const int maxn5 = 5 * 1000 * 100 + 10;
const int maxn3 = 500 + 10;
const long long mod = 1000 * 1000 * 1000 + 7;
const long long inf = 1LL * 2 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10;
vector<int> adj[maxn3], ans1, ans2;
long long h[maxn3][maxn3][2];
pair<pair<int, int>, int> par[maxn3][maxn3][2];
queue<pair<pair<int, int>, int>> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) h[i][j][0] = -1, h[i][j][1] = -1;
h[1][n][0] = 0;
q.push({{1, n}, 0});
while (!q.empty()) {
int a = q.front().first.first, b = q.front().first.second,
t = q.front().second;
q.pop();
if (t == 0 and a == n and b == 1) {
cout << h[a][b][t] / 2 << endl;
while (a != 0 and b != 0) {
int x = par[a][b][t].first.first, y = par[a][b][t].first.second;
t ^= 1;
if (t == 0)
ans1.push_back(a);
else
ans2.push_back(b);
a = x, b = y;
}
ans1.push_back(1);
reverse(ans1.begin(), ans1.end());
reverse(ans2.begin(), ans2.end());
for (auto u : ans1) cout << u << ' ';
cout << '\n';
for (auto u : ans2) cout << u << ' ';
return cout << endl, 0;
}
if (t == 0) {
for (auto u : adj[a])
if (h[u][b][1] == -1) {
q.push({{u, b}, 1});
h[u][b][1] = h[a][b][0] + 1;
par[u][b][1] = {{a, b}, 0};
}
} else {
for (auto u : adj[b])
if (h[a][u][0] == -1 and u != a) {
q.push({{a, u}, 0});
h[a][u][0] = h[a][b][1] + 1;
par[a][u][0] = {{a, b}, 1};
}
}
}
return cout << -1 << endl, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M;
int E[511][511];
int pre[511][511][2];
int dis[511][511][2];
int qux[1 << 19];
int quy[1 << 19];
int quf[1 << 19];
int p, q;
void enterqu(int x, int y, int f) {
qux[q] = x, quy[q] = y, quf[q] = f;
q++;
}
void outqu(int &x, int &y, int &f) {
x = qux[p], y = quy[p], f = quf[p];
p++;
}
int solve() {
memset(pre, 0xff, sizeof pre);
for (int i = (0); i < (511); i++)
for (int j = (0); j < (511); j++)
for (int k = (0); k < (2); k++) dis[i][j][k] = 1000000001;
dis[0][N - 1][0] = 0;
p = 0, q = 0;
enterqu(0, N - 1, 0);
while (p < q) {
int x, y, f;
outqu(x, y, f);
if (f == 0) {
for (int i = (1); i < (E[x][0] + 1); i++) {
int nx = E[x][i], ny = y, nf = 1 - f;
if (dis[nx][ny][nf] != 1000000001) continue;
dis[nx][ny][nf] = dis[x][y][f];
pre[nx][ny][nf] = x;
enterqu(nx, ny, nf);
}
} else {
for (int j = (1); j < (E[y][0] + 1); j++) {
int nx = x, ny = E[y][j], nf = 1 - f;
if (nx == ny) continue;
if (dis[nx][ny][nf] != 1000000001) continue;
dis[nx][ny][nf] = dis[x][y][f] + 1;
pre[nx][ny][nf] = y;
enterqu(nx, ny, nf);
if (nx == N - 1 && ny == 0) return dis[nx][ny][nf];
}
}
}
return -1;
}
int main() {
scanf("%d%d", &N, &M);
for (int i = (0); i < (511); i++) E[i][0] = 0;
for (int i = (0); i < (M); i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
E[u][++E[u][0]] = v;
E[v][++E[v][0]] = u;
}
int ans = solve();
if (ans == -1) {
printf("-1\n");
return 0;
}
printf("%d\n", ans);
vector<pair<int, int> > vp;
int x = N - 1, y = 0;
do {
vp.push_back(make_pair(x, y));
y = pre[x][y][0];
x = pre[x][y][1];
} while (!(x == 0 && y == N - 1));
vp.push_back(make_pair(x, y));
for (int i = (0); i < (vp.size()); i++) printf("%d ", vp[i].second + 1);
printf("\n");
for (int i = (0); i < (vp.size()); i++) printf("%d ", vp[i].first + 1);
printf("\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 600 + 10;
const long long int mod = 1e9 + 7;
const long long int inf = 1e16;
long long int dp[N][N][2];
int par[N][N][2];
deque<pair<pair<int, int>, int>> q;
vector<int> adj[N];
vector<pair<int, int>> ans;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][0] = inf;
dp[i][j][1] = inf;
par[i][j][0] = -1;
par[i][j][1] = -1;
}
}
dp[0][n - 1][1] = 0;
q.push_back({{0, n - 1}, 1});
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int k = q.front().second;
q.pop_front();
if (k == 1) {
for (int i = 0; i < adj[x].size(); i++) {
int u = adj[x][i];
if (dp[u][y][0] > dp[x][y][1] + 1) {
dp[u][y][0] = dp[x][y][1] + 1;
par[u][y][0] = x;
q.push_back({{u, y}, 0});
}
}
} else {
for (int i = 0; i < adj[y].size(); i++) {
int u = adj[y][i];
if (u != x && dp[x][u][1] > dp[x][y][0] + 1) {
dp[x][u][1] = dp[x][y][0] + 1;
par[x][u][1] = y;
q.push_back({{x, u}, 1});
}
}
}
}
if (dp[n - 1][0][1] == inf) {
cout << -1 << endl;
return 0;
}
cout << dp[n - 1][0][1] / 2 << endl;
int x = n - 1, y = 0, k = 1;
while (par[x][y][k] != -1) {
if (k == 1) {
ans.push_back({y, par[x][y][k]});
y = par[x][y][k];
k = 0;
} else {
ans.push_back({x, par[x][y][k]});
x = par[x][y][k];
k = 1;
}
}
int j = 0;
for (int i = 0; i < ans.size(); i++) {
if (i % 2 == 0) {
cout << ans[i].first + 1 << ' ';
j = i;
}
}
cout << ans[j].second + 1 << endl;
for (int i = 0; i < ans.size(); i++) {
if (i % 2 == 1) {
cout << ans[i].first + 1 << ' ';
j = i;
}
}
cout << ans[j].second + 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 + 10;
int n, m;
vector<int> adj[MAXN];
int dis[MAXN][MAXN][5];
int par[MAXN][MAXN][5];
bool mark[MAXN][MAXN][5];
queue<pair<pair<int, int>, bool> > q;
vector<int> ans[5];
void bfs(pair<pair<int, int>, bool> p) {
memset(dis, 63, sizeof dis);
dis[p.first.first][p.first.second][p.second] = 0;
q.push(p);
while ((int)q.size()) {
auto p = q.front();
int u = p.first.first, v = p.first.second;
bool b = p.second;
q.pop();
if (!b) {
for (int x : adj[u]) {
if (!mark[x][v][!b]) {
dis[x][v][!b] = dis[u][v][b] + 1;
par[x][v][!b] = u;
mark[x][v][!b] = true;
q.push(make_pair(pair<int, int>(x, v), !b));
}
}
} else {
for (int x : adj[v]) {
if (!mark[u][x][!b]) {
dis[u][x][!b] = dis[u][v][b] + 1;
par[u][x][!b] = v;
mark[u][x][!b] = true;
q.push(make_pair(pair<int, int>(u, x), !b));
}
}
}
}
}
void pp(int u, int v, bool b) {
if (u == 0 && v == n - 1 && !b) return;
int x = par[u][v][b];
ans[b].push_back(x);
if (!b)
pp(u, x, !b);
else
pp(x, v, !b);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++) mark[i][i][0] = true;
bfs(make_pair(pair<int, int>(0, n - 1), 0));
if (dis[n - 1][0][0] > 1e9) return (cout << -1 << endl, 0);
cout << dis[n - 1][0][0] / 2 << endl;
pp(n - 1, 0, 0);
reverse(ans[0].begin(), ans[0].end());
reverse(ans[1].begin(), ans[1].end());
ans[0].push_back(0);
ans[1].push_back(n - 1);
for (int i : ans[1]) cout << i + 1 << " ";
cout << endl;
for (int i : ans[0]) cout << i + 1 << " ";
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int max_n = 503;
struct tri {
int x;
int y;
int z;
tri(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
};
vector<int> v[max_n];
int dp[max_n][max_n][2];
bool check[max_n][max_n][2];
queue<tri> bfs;
tri dad[max_n][max_n][2];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
v[x].push_back(y);
v[y].push_back(x);
}
memset(dp, 63, sizeof dp);
int inf = dp[0][0][0];
dp[0][n - 1][1] = 0;
check[0][n - 1][1] = 1;
bfs.push(tri(0, n - 1, 1));
vector<int> masir1, masir2;
while (!bfs.empty()) {
tri a = bfs.front();
bfs.pop();
if (a.z == 1)
for (auto i : v[a.y]) {
if (!check[a.x][i][0]) {
bfs.push(tri(a.x, i, 0));
check[a.x][i][0] = true;
dp[a.x][i][0] = dp[a.x][a.y][1];
dad[a.x][i][0] = tri(a.x, a.y, 1);
}
}
else
for (auto i : v[a.x]) {
if (!check[i][a.y][1] && i != a.y) {
bfs.push(tri(i, a.y, 1));
check[i][a.y][1] = true;
dp[i][a.y][1] = dp[a.x][a.y][0] + 1;
dad[i][a.y][1] = tri(a.x, a.y, 0);
}
}
}
if (dp[n - 1][0][1] == inf) {
cout << -1 << endl;
return 0;
}
tri id = tri(n - 1, 0, 1);
while (id.x != 0 || id.y != n - 1) {
masir1.push_back(id.x);
masir2.push_back(id.y);
id = dad[id.x][id.y][id.z];
id = dad[id.x][id.y][id.z];
}
masir1.push_back(0);
masir2.push_back(n - 1);
reverse(masir1.begin(), masir1.end());
reverse(masir2.begin(), masir2.end());
cout << masir1.size() - 1 << endl;
for (auto i : masir1) cout << i + 1 << ' ';
cout << endl;
for (auto i : masir2) cout << i + 1 << ' ';
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int oo = numeric_limits<int>::max() / 2;
constexpr int N = 501;
int n, m;
vector<int> g[N];
int d[N][N][2];
int fromx[N][N][2];
int fromy[N][N][2];
struct State {
State(int x_, int y_, int t_) : x(x_), y(y_), t(t_) {}
int x, y, t;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j][0] = d[i][j][1] = oo;
}
}
fromx[0][n - 1][0] = -1;
fromy[0][n - 1][0] = -1;
d[0][n - 1][0] = 0;
queue<State> q;
q.emplace(0, n - 1, 0);
while (!q.empty()) {
auto cur = q.front();
q.pop();
if (cur.t == 0) {
for (auto to : g[cur.x]) {
if (d[to][cur.y][1] > d[cur.x][cur.y][0] + 1) {
d[to][cur.y][1] = d[cur.x][cur.y][0] + 1;
q.emplace(to, cur.y, 1);
fromx[to][cur.y][1] = cur.x;
fromy[to][cur.y][1] = cur.y;
}
}
} else {
for (auto to : g[cur.y]) {
if (to == cur.x) {
continue;
}
if (d[cur.x][to][0] > d[cur.x][cur.y][1] + 1) {
d[cur.x][to][0] = d[cur.x][cur.y][1] + 1;
q.emplace(cur.x, to, 0);
fromx[cur.x][to][0] = cur.x;
fromy[cur.x][to][0] = cur.y;
}
}
}
}
if (d[n - 1][0][0] == oo) {
cout << -1 << endl;
} else {
vector<int> path[2];
function<void(int, int, int, bool)> build = [&](int i, int j, int t,
bool flag) {
while (1) {
if (i == -1 || j == -1) {
break;
}
int pi = fromx[i][j][t];
int pj = fromy[i][j][t];
if (flag) {
path[0].push_back(i);
path[1].push_back(j);
} else {
if (t == 1) {
path[1].push_back(j);
} else {
path[0].push_back(i);
}
}
flag = false;
t = 1 - t;
i = pi;
j = pj;
}
};
cout << d[n - 1][0][0] / 2 << endl;
build(n - 1, 0, 0, true);
reverse(path[0].begin(), path[0].end());
for (int i : path[0]) {
cout << (i + 1) << " ";
}
cout << endl;
reverse(path[1].begin(), path[1].end());
for (int i : path[1]) {
cout << (i + 1) << " ";
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 555;
vector<int> adj[N];
int d[N][N][2], p[N][N][2];
int main() {
int n, m;
scanf("%d%d", &n, &m);
while (m--) {
int v, u;
scanf("%d%d", &v, &u);
adj[--v].push_back(--u);
adj[u].push_back(v);
}
memset(d, -1, sizeof(d));
d[0][n - 1][0] = 0;
queue<pair<pair<int, int>, bool> > Q;
Q.push(make_pair(make_pair(0, n - 1), 0));
while (!Q.empty()) {
int v = Q.front().first.first;
int u = Q.front().first.second;
int t = Q.front().second;
Q.pop();
if (t) {
for (int i = 0; i < ((int)(adj[u]).size()); i++) {
int to = adj[u][i];
if (v == to || d[v][to][0] != -1) continue;
d[v][to][0] = d[v][u][1] + 1;
p[v][to][0] = u;
Q.push(make_pair(make_pair(v, to), 0));
}
} else {
for (int i = 0; i < ((int)(adj[v]).size()); i++) {
int to = adj[v][i];
if (d[to][u][1] != -1) continue;
d[to][u][1] = d[v][u][0] + 1;
p[to][u][1] = v;
Q.push(make_pair(make_pair(to, u), 1));
}
}
}
if (d[n - 1][0][0] == -1) {
puts("-1");
return 0;
}
vector<int> ans[2];
int a = n - 1;
int b = 0;
ans[1].push_back(a);
ans[0].push_back(b);
for (int i = 0; i < d[n - 1][0][0]; i += 2) {
b = p[a][b][0];
ans[0].push_back(b);
a = p[a][b][1];
ans[1].push_back(a);
}
for (int i = 0; i < 2; i++) reverse((ans[i]).begin(), (ans[i]).end());
printf("%d\n", d[n - 1][0][0] / 2);
for (int i = 1; i >= 0; i--) {
for (int j = 0; j < ((int)(ans[i]).size()); j++)
printf("%d%c", ans[i][j] + 1, " \n"[j == ((int)(ans[i]).size()) - 1]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 555;
long long dis[N][N][2], n, m, a, b, x, y, u, v, w, k1, k2, W, U;
vector<int> nei[N], oneton, ntoone;
bool mark[N][N][2], p, r, R;
pair<pair<int, int>, bool> par[N][N][2], k;
queue<pair<pair<int, int>, bool> > q;
void bfs() {
q.push({{0, n - 1}, 0});
while (!q.empty()) {
k = q.front();
k1 = k.first.first, k2 = k.first.second;
p = k.second;
if (!p) {
for (int i = 0; i < nei[k1].size(); i++) {
v = nei[k1][i];
if (!mark[v][k2][!p]) {
mark[v][k2][!p] = 1;
dis[v][k2][!p] = dis[k1][k2][p] + 1;
par[v][k2][!p] = {{k1, k2}, p};
q.push({{v, k2}, !p});
}
}
} else {
for (int i = 0; i < nei[k2].size(); i++) {
v = nei[k2][i];
if (!mark[k1][v][!p] && k1 != v) {
mark[k1][v][!p] = 1;
dis[k1][v][!p] = dis[k1][k2][p] + 1;
par[k1][v][!p] = {{k1, k2}, p};
q.push({{k1, v}, !p});
}
}
}
q.pop();
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--, b--;
nei[a].push_back(b);
nei[b].push_back(a);
}
bfs();
if (!mark[n - 1][0][0]) {
cout << -1;
return 0;
}
u = n - 1, w = 0;
oneton.push_back(u);
ntoone.push_back(w);
while (u != 0 || w != n - 1 || r) {
U = u, W = w, R = r;
u = par[U][W][R].first.first;
w = par[U][W][R].first.second;
r = !R;
if (!r)
oneton.push_back(u);
else
ntoone.push_back(w);
}
cout << oneton.size() - 1 << endl;
for (int i = oneton.size() - 1; i >= 0; i--) cout << oneton[i] + 1 << " ";
cout << endl;
for (int i = ntoone.size() - 1; i >= 0; i--) cout << ntoone[i] + 1 << " ";
}
|
#include <bits/stdc++.h>
using namespace std;
bool vis[555][555][2];
int n, e;
int dis[555][555][2];
int p[555][555][2];
vector<int> adj[555];
void bfs() {
queue<pair<pair<int, int>, bool> > qu;
qu.push(make_pair(make_pair(0, n - 1), false));
vis[0][n - 1][0] = true;
while (!qu.empty()) {
if (vis[n - 1][0][0]) break;
int a = qu.front().first.first;
int b = qu.front().first.second;
bool mv = qu.front().second;
if (mv) {
for (int i = 0; i < adj[b].size(); i++) {
int bb = adj[b][i];
if (a != bb && !vis[a][bb][!mv]) {
vis[a][bb][!mv] = true;
dis[a][bb][!mv] = dis[a][b][mv] + 1;
p[a][bb][!mv] = b;
qu.push(make_pair(make_pair(a, bb), !mv));
}
}
}
if (!mv) {
for (int i = 0; i < adj[a].size(); i++) {
int aa = adj[a][i];
if (!vis[aa][b][!mv]) {
vis[aa][b][!mv] = true;
dis[aa][b][!mv] = dis[a][b][mv] + 1;
p[aa][b][!mv] = a;
qu.push(make_pair(make_pair(aa, b), !mv));
}
}
}
qu.pop();
}
}
int main() {
scanf("%d%d", &n, &e);
for (int i = 1; i <= e; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
bfs();
if (!vis[n - 1][0][0]) {
puts("-1");
return 0;
}
int a = n - 1, b = 0, mv = 0;
vector<int> ans1, ans2;
ans1.push_back(a);
ans2.push_back(b);
while (a != 0 || b != (n - 1) || mv != 0) {
if (!mv) {
ans2.push_back(p[a][b][mv]);
b = p[a][b][mv];
} else {
ans1.push_back(p[a][b][mv]);
a = p[a][b][mv];
}
mv = !mv;
}
printf("%u\n", ans1.size() - 1u);
for (int i = ans1.size() - 1; i >= 0; i--) printf("%d ", ans1[i] + 1);
printf("\n");
for (int i = ans2.size() - 1; i >= 0; i--) printf("%d ", ans2[i] + 1);
printf("\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 600;
long long n, m;
pair<long long, long long> par[N][N];
long long d[N][N];
vector<long long> G[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 0, v, u; i < m; i++) {
cin >> v >> u;
G[v].push_back(u);
G[u].push_back(v);
}
memset(d, 63, sizeof d);
d[1][n] = 0;
queue<pair<long long, long long> > q;
q.push({1, n});
while (q.size()) {
auto X = q.front();
q.pop();
long long x = X.first, y = X.second;
for (auto xx : G[x]) {
for (auto yy : G[y]) {
if (xx ^ yy && d[xx][yy] > d[x][y] + 1) {
d[xx][yy] = d[x][y] + 1;
q.push({xx, yy});
par[xx][yy] = {x, y};
}
}
}
}
if (d[n][1] > 1e10) return cout << -1, 0;
cout << d[n][1] << '\n';
vector<long long> A, B;
long long X = n, Y = 1;
long long cnt = 0;
while (!(X == 1 && Y == n)) {
A.push_back(X);
B.push_back(Y);
auto P = par[X][Y];
X = P.first;
Y = P.second;
}
A.push_back(1);
B.push_back(n);
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
for (auto u : A) cout << u << ' ';
cout << '\n';
for (auto u : B) cout << u << ' ';
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int y, next;
};
const int maxn = 550, maxm = 10010;
int n, m, num, head[maxn], d[maxn][maxn][2], ansa[maxn], ansb[maxn];
edge e[maxm << 1];
pair<int, int> pre[maxn][maxn][2];
void add(int a, int b) {
num++;
e[num].next = head[a];
head[a] = num;
e[num].y = b;
}
int main() {
scanf("%d%d", &n, &m);
memset(head, -1, sizeof head);
num = -1;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
memset(d, -1, sizeof d);
queue<pair<pair<int, int>, int> > Q;
d[1][n][0] = 0;
Q.push(make_pair(make_pair(1, n), 0));
while (!Q.empty()) {
pair<pair<int, int>, int> now = Q.front();
Q.pop();
int a = now.first.first, b = now.first.second, s = now.second;
if (a == n && b == 1 && s == 0) break;
if (s == 0) {
for (int temp = head[a]; temp >= 0; temp = e[temp].next) {
int na = e[temp].y;
if (d[na][b][1] < 0) {
d[na][b][1] = d[a][b][0] + 1;
pre[na][b][1] = make_pair(a, b);
Q.push(make_pair(make_pair(na, b), 1));
}
}
} else {
for (int temp = head[b]; temp >= 0; temp = e[temp].next) {
int nb = e[temp].y;
if (a != nb && d[a][nb][0] < 0) {
d[a][nb][0] = d[a][b][1];
pre[a][nb][0] = make_pair(a, b);
Q.push(make_pair(make_pair(a, nb), 0));
}
}
}
}
printf("%d\n", d[n][1][0]);
if (d[n][1][0] >= 0) {
pair<int, int> p = make_pair(n, 1);
int topa = 0, topb = 0;
while (d[p.first][p.second][0] > 0) {
ansa[topa++] = p.first;
ansb[topb++] = p.second;
p = pre[p.first][p.second][0];
p = pre[p.first][p.second][1];
}
printf("1");
for (int i = topa - 1; i >= 0; i--) printf(" %d", ansa[i]);
printf("\n%d", n);
for (int i = topb - 1; i >= 0; i--) printf(" %d", ansb[i]);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 505;
const long long MOD = 1e9 + 7;
vector<int> g[MAX];
queue<pair<pair<int, int>, int>> q;
int dis[MAX][MAX][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
v--, u--;
g[v].push_back(u);
g[u].push_back(v);
}
memset(dis, -1, sizeof(dis));
dis[0][n - 1][0] = MAX;
q.push({{0, n - 1}, 0});
while (q.size()) {
int v = q.front().first.first;
int u = q.front().first.second;
int t = q.front().second;
q.pop();
if (v == u && t == 0) continue;
if (t == 0) {
for (auto x : g[v])
if (dis[x][u][1] == -1) {
dis[x][u][1] = v;
q.push({{x, u}, 1});
}
} else
for (auto x : g[u])
if (dis[v][x][0] == -1) {
dis[v][x][0] = u;
q.push({{v, x}, 0});
}
}
if (dis[n - 1][0][0] == -1) return cout << "-1\n", 0;
vector<int> res[2];
if (dis[n - 1][0][0] != -1) {
int a, b, t;
a = n - 1;
b = 0;
t = 0;
while (dis[a][b][t] != MAX) {
if (t == 0)
res[1].push_back(b);
else
res[0].push_back(a);
if (t == 0)
b = dis[a][b][t];
else
a = dis[a][b][t];
t = 1 - t;
}
}
res[0].push_back(0);
res[1].push_back(n - 1);
reverse(res[0].begin(), res[0].end());
reverse(res[1].begin(), res[1].end());
cout << res[0].size() - 1 << "\n";
for (auto x : res[0]) cout << x + 1 << " ";
cout << "\n";
for (auto x : res[1]) cout << x + 1 << " ";
cout << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, y, z, fi, fr, p[555][555][2], v[555][555][2], qx[660000],
qy[660000], qz[660000];
vector<int> g[555], r[2];
int main() {
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
p[1][n][0] = 1;
qy[0] = n;
qx[0] = fr = 1;
qz[0] = fi = 0;
while (fi < fr) {
x = qx[fi];
y = qy[fi];
z = qz[fi++];
if (z == 0)
for (j = 0; j < g[x].size(); j++) {
k = g[x][j];
if (p[k][y][1] == 0) {
p[k][y][1] = p[x][y][z] + 1;
v[k][y][1] = x;
qx[fr] = k;
qy[fr] = y;
qz[fr++] = 1;
}
}
else
for (j = 0; j < g[y].size(); j++) {
k = g[y][j];
if (p[x][k][0] == 0 && x != k) {
p[x][k][0] = p[x][y][z] + 1;
v[x][k][0] = y;
qx[fr] = x;
qy[fr] = k;
qz[fr++] = 0;
}
}
}
if (p[n][1][0]) {
printf("%d\n", (p[n][1][0] - 1) / 2);
for (x = n, y = 1, z = 0; p[x][y][z] > 0; z = 1 - z)
if (z == 0) {
r[1].push_back(y);
y = v[x][y][z];
} else {
r[0].push_back(x);
x = v[x][y][z];
}
r[0].push_back(1);
for (j = 0; j < 2; j++)
for (i = r[j].size() - 1; i >= 0; i--) {
printf("%d", r[j][i]);
if (i)
putchar(' ');
else
putchar('\n');
}
} else
puts("-1");
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.