이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
const int MAX_N = 1e5 + 5;
const int MAX_L = 5000;
int Gcd(int a, int b) {
return (b ? Gcd(b, a % b) : a);
}
inline int Lcm(int a, int b) {
return a * b / Gcd(a, b);
}
set<int> bad_node[MAX_L];
set<pii> bad_edge[MAX_L];
queue<pii> q[MAX_L];
bitset<MAX_N> visited[MAX_L];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vvi graph(n + 1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int k;
cin >> k;
vi len(k);
vvi path(k);
int l = 1;
for (int j = 0; j < k; j++) {
cin >> len[j];
path[j].resize(len[j]);
l = Lcm(len[j], l);
for (int i = 0; i < len[j]; i++) {
cin >> path[j][i];
}
}
for (int j = 0; j < k; j++) {
// path[j][l] = path[j][0];
for (int i = 1; i <= l; i++) {
bad_node[i].insert(path[j][i % len[j]]);
bad_edge[i].insert({min(path[j][i % len[j]], path[j][(i - 1) % len[j]]), max(path[j][i % len[j]], path[j][(i - 1) % len[j]])});
}
}
swap(bad_node[0], bad_node[l]);
swap(bad_edge[0], bad_edge[l]);
q[0].push({1, 0});
for (int i = 0; ; i++) {
if (i == l) i = 0;
if (q[i].empty()) break;
int t = (i + 1) % l;
while (!q[i].empty()) {
auto [node, d] = q[i].front();
if (node == n) {
cout << d << '\n';
return 0;
}
q[i].pop();
if (!visited[t][node] && !bad_node[t].count(node)) {
visited[t][node] = true;
q[t].push({node, d + 1});
}
for (int neighbor : graph[node]) {
if (!visited[t][neighbor] && !bad_node[t].count(neighbor)) {
if (!bad_edge[t].count({min(node, neighbor), max(node, neighbor)})) {
visited[t][neighbor] = true;
q[t].push({neighbor, d + 1});
}
}
}
}
}
cout << "impossible\n";
return 0;
}
//6 6
//1 2
//2 3
//3 4
//4 5
//5 2
//5 6
//1
//4 3 2 5 4
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |