Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int inf = INT_MAX / 4;
int n, m, k, s[MAXN], t[MAXN], d[MAXN][MAXN], a, b, h[MAXN], dp[MAXN][MAXN];
vector<int> maybe[MAXN], must[MAXN], adj[MAXN], nxt[MAXN][MAXN];
bool visit[MAXN];
inline void get() {
cin >> n >> m >> a >> b;
a--;
b--;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
d[u][v] = 1;
adj[u].push_back(v);
}
cin >> k;
for (int i = 0; i < k; i++) {
cin >> s[i] >> t[i];
s[i]--;
t[i]--;
}
return;
}
inline void floyd() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = ((d[i][j] || i == j) ? d[i][j] : inf);
for (int h = 0; h < n; h++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][h] + d[h][j]);
return;
}
inline bool mustcome(int r, int ind) {
if (r == s[ind] || r == t[ind]) return true;
for (int i = 0; i < n; i++) h[i] = 0, visit[i] = false;
visit[s[ind]] = true;
queue<int> q;
q.push(s[ind]);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : adj[v])
if (u != r && !visit[u]) {
visit[u] = true;
h[u] = h[v] + 1;
q.push(u);
}
}
return (!visit[t[ind]] || (h[t[ind]] > d[s[ind]][t[ind]]));
}
inline void prep() {
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) {
if (d[s[j]][t[j]] != inf && d[s[j]][i] + d[i][t[j]] == d[s[j]][t[j]])
maybe[j].push_back(i);
if (mustcome(i, j)) must[i].push_back(j);
}
for (int j = 0; j < k; j++)
for (int v : maybe[j])
for (int u : maybe[j])
if (u != v && d[s[j]][v] + 1 == d[s[j]][u] && d[v][u] == 1)
nxt[v][j].push_back(u);
return;
}
inline void calc() {
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) dp[i][j] = inf;
for (int j = 0; j < k; j++) dp[b][j] = 0;
bool upd = true;
while (upd) {
upd = false;
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) {
int tmp = -1;
for (int u : nxt[i][j]) tmp = max(tmp, dp[u][j]);
if (tmp != -1 && tmp < dp[i][j]) dp[i][j] = tmp, upd = true;
for (int y : must[i])
if (y != j && dp[i][y] + 1 < dp[i][j])
dp[i][j] = dp[i][y] + 1, upd = true;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
get();
floyd();
prep();
calc();
int ans = inf;
for (int j : must[a]) ans = min(ans, dp[a][j]);
cout << (ans >= inf ? -1 : ans + 1) << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x03030303;
int n, m, dis[120][120], ans[120], x, y, i, j, k, s, t, q, tail[120], tmp[120],
f[120];
bool must[120][120], bo[120];
int dfs(int x, int t) {
if (x == t) return ans[t];
if (bo[x]) return f[x];
bo[x] = 1;
f[x] = 0;
for (int i = 1; i <= n; ++i)
if (dis[x][i] + dis[i][t] == dis[x][t] && dis[x][i] == 1)
f[x] = max(f[x], dfs(i, t));
f[x] = min(f[x], ans[x]);
return f[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
memset(dis, 3, sizeof(dis));
for (i = 1; i <= n; ++i) dis[i][i] = 0;
for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), dis[x][y] = 1;
for (k = 1; k <= n; ++k)
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &q);
for (i = 1; i <= q; ++i) {
scanf("%d%d", &x, &y);
tail[i] = y;
if (dis[x][y] == inf) continue;
for (j = 1; j <= n; ++j)
if (dis[x][j] + dis[j][y] == dis[x][y]) {
must[i][j] = 1;
for (k = 1; k <= n; ++k)
if (k != j && dis[x][k] == dis[x][j] && dis[k][y] == dis[j][y]) {
must[i][j] = 0;
break;
}
}
}
memset(ans, 3, sizeof(ans));
ans[t] = 0;
for (;;) {
bool flag = 1;
for (i = 1; i <= q; ++i) {
memset(bo, 0, sizeof(bo));
for (j = 1; j <= n; ++j)
if (must[i][j]) {
int tmp = dfs(j, tail[i]) + 1;
if (tmp < ans[j]) ans[j] = tmp, flag = 0;
}
}
if (flag) break;
}
printf("%d\n", ans[s] == inf ? -1 : ans[s]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<int> G1[maxn];
int n, m, a, b, q, mp[maxn][maxn], must[maxn][maxn], num[maxn];
int dist[maxn], vis[maxn], p[maxn], fb[maxn], tb[maxn], dp[maxn], Md[maxn];
struct Heap {
int u, d;
Heap(int _u = 0, int _d = 0) {
u = _u;
d = _d;
}
bool operator<(const Heap &H) const { return d > H.d; }
};
int Dijkstra(int s, int t, int del) {
if (s == del || t == del) return 0x3f3f3f3f;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) dist[i] = 0x3f3f3f3f;
dist[s] = 0;
p[s] = -1;
priority_queue<Heap> Q;
Q.push(Heap(s, 0));
while (!Q.empty()) {
Heap o = Q.top();
Q.pop();
int u = o.u;
if (vis[u]) continue;
vis[u] = 1;
for (int i = 0; i < G1[u].size(); i++) {
int v = G1[u][i];
if (v == del) continue;
if (dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
p[v] = u;
Q.push(Heap(v, dist[v]));
}
}
}
return dist[t];
}
vector<int> road;
int solve(int u, int k, int t) {
if (vis[u] == t) return Md[u];
int tmp = -1, sz = G1[u].size();
vis[u] = t;
for (int i = 0; i < sz; i++) {
int v = G1[u][i];
if (mp[fb[k]][u] + 1 + mp[v][tb[k]] == mp[fb[k]][tb[k]]) {
tmp = max(tmp, solve(v, k, t));
}
}
return Md[u] = min(tmp == -1 ? 0x3f3f3f3f : tmp, dp[u]);
}
int main() {
int u, v, mv, md;
memset(mp, 0x3f, sizeof mp);
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++) mp[i][i] = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
G1[u].push_back(v);
mp[u][v] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
scanf("%d", &q);
for (int bs = 1; bs <= q; bs++) {
scanf("%d%d", &u, &v);
int d = Dijkstra(u, v, -1);
mv = v;
fb[bs] = u;
tb[bs] = v;
if (d == 0x3f3f3f3f) continue;
while (v > 0) {
road.push_back(v);
v = p[v];
}
int sz = road.size();
for (int i = sz - 1; i >= 0; i--) {
int d1 = Dijkstra(u, tb[bs], road[i]);
if (d1 > d) road.push_back(road[i]);
}
road.erase(road.begin(), road.begin() + sz);
sz = road.size();
for (int i = 0; i < sz; i++) must[bs][road[i]] = 1;
road.clear();
}
int ok = 1, times = 1;
memset(vis, 0, sizeof vis);
memset(dp, 0x3f, sizeof dp);
dp[b] = 0;
while (ok) {
ok = 0;
for (int bs = 1; bs <= q; bs++) {
for (int i = 1; i <= n; i++) {
if (must[bs][i]) {
int tmp = solve(i, bs, times++) + 1;
if (tmp < dp[i]) ok = 1, dp[i] = tmp;
}
}
}
}
printf("%d\n", dp[a] == 0x3f3f3f3f ? -1 : dp[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, K, S, T, tot;
int f[110][110], num[110][110], go[110][110], s[110], t[110], Ans[110], Dp[110];
int Vis[110];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int DFS(int x, int y) {
if (Vis[x] == tot) return Dp[x];
Vis[x] = tot;
int Max = -1;
for (int i = 1; i <= n; i++)
if (f[x][i] == 1 && f[x][i] + f[i][y] == f[x][y]) Max = max(Max, DFS(i, y));
if (Max == -1) Max = 0x3f3f3f3f;
Dp[x] = min(Max, Ans[x] + 1);
return Dp[x];
}
int main() {
n = read();
m = read();
S = read();
T = read();
memset(f, 0x3f3f3f3f, sizeof(f));
for (int i = 1; i <= n; i++) f[i][i] = 0;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
f[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
K = read();
for (int i = 1; i <= K; i++) {
int x = read(), y = read();
for (int j = 1; j <= n; j++)
if (f[x][j] < 0x3f3f3f3f && f[x][j] + f[j][y] == f[x][y])
num[i][f[x][j]]++;
for (int j = 1; j <= n; j++)
if (f[x][j] < 0x3f3f3f3f && f[x][j] + f[j][y] == f[x][y] &&
num[i][f[x][j]] == 1)
go[i][j] = 1;
s[i] = x;
t[i] = y;
}
memset(Ans, 0x3f3f3f3f, sizeof(Ans));
Ans[T] = 0;
while (1) {
bool Get = false;
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= n; j++) {
if (!go[i][j]) continue;
tot++;
int qwer = DFS(j, t[i]);
if (qwer < Ans[j]) Ans[j] = qwer, Get = true;
}
}
if (!Get) break;
}
printf("%d\n", Ans[S] >= 0x3f3f3f3f ? -1 : Ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 15;
int n, m, s, t, x, y, k;
int a[maxn][maxn];
int c[maxn][maxn];
int b[maxn][maxn];
int f[maxn];
int ls[maxn];
int xx[maxn], yy[maxn];
int vis[maxn], sum;
int done(int x, int y) {
if (vis[x] == sum) return ls[x];
vis[x] = sum;
int ans = -1;
for (int i = 1; i <= n; i++)
if (a[x][i] + a[i][yy[y]] == a[x][yy[y]] && a[x][i] == 1)
ans = max(ans, done(i, y));
if (ans == -1) ans = f[0];
ls[x] = min(ans, f[x] + 1);
return ls[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
memset(a, 60, sizeof(a));
for (int i = 1; i <= n; i++) a[i][i] = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) a[j][k] = min(a[j][k], a[j][i] + a[i][k]);
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d", &x, &y);
if (a[x][y] == a[0][0]) continue;
for (int j = 1; j <= n; j++)
if (a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (int j = 1; j <= n; j++)
if (a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1) c[i][j] = 1;
xx[i] = x;
yy[i] = y;
}
memset(f, 60, sizeof(f));
f[t] = 0;
bool boo = true;
while (boo) {
boo = false;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
if (c[j][i] == 1) {
sum++;
int anss = done(i, j);
if (anss < f[i]) f[i] = anss, boo = true;
}
}
if (f[s] < f[0])
printf("%d\n", f[s]);
else
printf("-1\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -f;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
const int maxn = 1e2 + 5;
int vis[maxn], NOW, num[maxn];
int dp[maxn], f[maxn][maxn];
int n, m, K, DP[maxn], s[maxn], t[maxn];
bool b[maxn][maxn];
int dfs(int x, int path) {
if (vis[x] == NOW) return DP[x];
vis[x] = NOW;
int tmp = -1;
for (int y = 1; y <= n; y++)
if (f[x][y] == 1 && f[x][t[path]] == f[y][t[path]] + 1)
tmp = max(tmp, dfs(y, path));
if (tmp == -1) tmp = 1e9;
if (tmp > dp[x]) tmp = dp[x];
return DP[x] = tmp;
}
int main() {
n = read(), m = read(), s[0] = read(), t[0] = read();
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = 1e9;
for (int i = 1, x, y; i <= m; i++) f[x = read()][y = read()] = 1;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
K = read();
for (int i = 1; i <= K; i++) {
s[i] = read(), t[i] = read();
if (f[s[i]][t[i]] == 1e9) continue;
for (int j = 1; j <= n; j++) {
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) num[f[s[i]][j]]++;
}
for (int j = 1; j <= n; j++)
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) {
if (num[f[s[i]][j]] == 1) b[i][j] = 1;
num[f[s[i]][j]] = 0;
}
}
for (int i = 1; i <= n; i++) dp[i] = DP[i] = 1e9;
dp[t[0]] = 0;
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= K; i++)
for (int j = 1; j <= n; j++) {
if (b[i][j]) {
NOW++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
flag = 1;
dp[j] = tmp;
}
}
}
}
if (dp[s[0]] == 1e9) dp[s[0]] = -1;
cout << dp[s[0]];
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 1e2 + 10, maxm = 1e5 + 10, lg = 20, mod = 1e9 + 7,
inf = 1e18;
long long n, m, a, b, K, dp[maxn], pd[maxn];
vector<pair<long long, long long> > bus;
long long g[maxn][maxn];
vector<long long> h[maxn];
bool rr[maxn][maxn];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = inf, g[i][i] = 0;
while (m--) {
long long v, u;
cin >> v >> u;
g[v][u] = rr[v][u] = 1;
}
for (int k = 1; k <= n; k++)
for (int v = 1; v <= n; v++)
for (int u = 1; u <= n; u++) g[v][u] = min(g[v][u], g[v][k] + g[k][u]);
cin >> K;
while (K--) {
long long ss, tt;
cin >> ss >> tt;
if (g[ss][tt] < inf) bus.push_back({ss, tt});
}
memset(dp, 69, sizeof(dp));
dp[b] = 0;
for (int k = 1; k <= n; k++)
for (auto x : bus) {
memset(pd, 69, sizeof(pd));
for (int i = 0; i <= g[x.first][x.second]; i++) h[i].clear();
for (int i = 1; i <= n; i++)
if (g[x.first][i] + g[i][x.second] == g[x.first][x.second])
h[g[x.first][i]].push_back(i);
pd[x.second] = dp[x.second];
for (int i = g[x.first][x.second] - 1; i >= 0; i--) {
for (auto v : h[i]) {
pd[v] = -1;
for (auto u : h[i + 1])
if (rr[v][u]) pd[v] = max(pd[v], pd[u]);
if (pd[v] == -1) pd[v] = inf;
pd[v] = min(pd[v], dp[v]);
}
if ((long long)(h[i].size()) == 1)
dp[h[i][0]] = min(dp[h[i][0]], pd[h[i][0]] + 1);
}
}
cout << (dp[a] >= inf ? -1 : dp[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, p, S, T;
int dis[105][105], tmp[105], vis[105], tme;
int ans[105], s[105], t[105], c[105][105], mark[105];
inline int rd() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
int dfs(int x, int y) {
if (vis[x] == tme) return tmp[x];
vis[x] = tme;
int cur = -1;
for (int i = 1; i <= n; i++)
if (dis[x][i] == 1 && 1 + dis[i][y] == dis[x][y]) cur = max(cur, dfs(i, y));
if (cur == -1) cur = 1 << 29;
cur = min(cur, ans[x] + 1);
return tmp[x] = cur;
}
int main() {
n = rd(), m = rd(), S = rd(), T = rd();
memset(dis, 127 / 3, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int _ = 1; _ <= m; _++) {
int a = rd(), b = rd();
dis[a][b] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
p = rd();
for (int i = 1; i <= p; i++) s[i] = rd(), t[i] = rd();
for (int i = 1; i <= p; i++) {
memset(mark, 0, sizeof(mark));
int x = s[i], y = t[i];
for (int j = 1; j <= n; j++)
if (dis[x][j] <= n && dis[x][y] == dis[x][j] + dis[j][y])
mark[dis[x][j]]++;
for (int j = 1; j <= n; j++)
if (dis[x][j] <= n && dis[x][y] == dis[x][j] + dis[j][y]) {
if (mark[dis[x][j]] == 1) c[i][j] = 1;
}
}
memset(ans, 127 / 3, sizeof(ans));
ans[T] = 0;
for (;;) {
int flag = 0;
for (int i = 1; i <= p; i++)
for (int j = 1; j <= n; j++)
if (c[i][j]) {
tme++;
int x = dfs(j, t[i]);
x < ans[j] ? flag = 1, ans[j] = x : 0;
}
if (!flag) break;
}
ans[S] > n ? puts("-1") : printf("%d\n", ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105;
int f[N][N], ans[N], rp[N], S, T, s[N], t[N], n, m, k, vis[N], cho;
int must[N][N];
int ct[N][N];
int dfs(int x, int y) {
if (vis[x] == cho) return rp[x];
vis[x] = cho;
int res = -1;
for (int j = (1); j <= (int)n; j++)
if (f[x][j] == 1 && f[x][j] + f[j][y] == f[x][y]) res = max(res, dfs(j, y));
if (res < 0) res = ans[0];
rp[x] = min(ans[x] + 1, res);
return rp[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(f, 63, sizeof f);
memset(ans, 63, sizeof ans);
for (int i = (1); i <= (int)n; i++) f[i][i] = 0;
for (int i = (1); i <= (int)m; i++) {
int x, y;
scanf("%d%d", &x, &y);
f[x][y] = 1;
}
for (int p = (1); p <= (int)n; p++)
for (int i = (1); i <= (int)n; i++)
for (int j = (1); j <= (int)n; j++)
f[i][j] = min(f[i][p] + f[p][j], f[i][j]);
scanf("%d", &k);
for (int i = (1); i <= (int)k; i++) {
scanf("%d%d", &s[i], &t[i]);
for (int j = (1); j <= (int)n; j++)
if (f[s[i]][j] < 1e9 && f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]])
ct[i][f[s[i]][j]]++;
for (int j = (1); j <= (int)n; j++)
if (f[s[i]][j] < 1e9 && f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]] &&
ct[i][f[s[i]][j]] == 1)
must[i][j] = 1;
}
bool is_jabby_god = 1;
ans[T] = 0;
while (is_jabby_god) {
bool flag = 0;
for (int i = (1); i <= (int)k; i++)
for (int j = (1); j <= (int)n; j++)
if (must[i][j]) {
++cho;
int tmp = dfs(j, t[i]);
if (tmp < ans[j]) {
ans[j] = tmp;
flag = 1;
}
}
if (!flag) break;
}
if (ans[S] < 1e9)
printf("%d\n", ans[S]);
else
printf("-1\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 101, MAXM = 10001;
int n, m, S, T, s[MAXN], t[MAXN], vis[MAXN], dis[MAXN][MAXN], dp[MAXN], g[MAXN],
e, K;
bool pass[MAXN][MAXN];
int dfs(int u, int M) {
if (u == M) return dp[M];
if (vis[u]) return g[u];
vis[u] = 1;
g[u] = 0;
for (int i = 1; i <= n; i++) {
if (dis[u][i] + dis[i][M] == dis[u][M] && dis[i][M] + 1 == dis[u][M])
g[u] = max(g[u], dfs(i, M));
}
return g[u] = min(g[u], dp[u]);
}
int main() {
scanf("%d %d %d %d", &n, &m, &S, &T);
memset(dis, 0x3f3f3f3f, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
dis[a][b] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (dis[i][k] != 0x3f3f3f3f)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
int flag = 1;
scanf("%d", &K);
for (int i = 1; i <= K; i++) {
int a, b;
scanf("%d %d", &a, &b);
s[i] = a;
t[i] = b;
if (dis[a][b] == 0x3f3f3f3f) continue;
for (int j = 0; j <= dis[a][b]; j++) {
int cnt = 0;
vector<int> A;
for (int k = 1; k <= n; k++) {
if (dis[a][k] == j && dis[s[i]][t[i]] == dis[s[i]][k] + dis[k][t[i]])
cnt++, A.push_back(k);
}
if (cnt == 1) pass[i][A[0]] = 1;
}
}
memset(dp, 0x3f3f3f3f, sizeof(dp));
dp[T] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= K; i++) {
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; j++) {
if (pass[i][j]) {
int tmp = dfs(j, t[i]);
if (tmp + 1 < dp[j]) {
dp[j] = tmp + 1;
flag = 1;
}
}
}
}
}
printf("%d\n", dp[S] == 0x3f3f3f3f ? -1 : dp[S]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T &a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T &a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T &a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T &a, S b) {
return a > b ? a = b, true : false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, a, b, k, S[N], T[N];
int G[N][N], d[N][N], ans[N], dp[N];
int fnow;
int vis[N];
vector<int> V[N];
vector<pair<int, int> > vc;
int dfs(int u, int who) {
if (vis[u] == fnow) return dp[u];
vis[u] = fnow;
int ret = -1;
for (int v = 1; v <= n; v++) {
if (G[u][v] == 1 && d[S[who]][u] + d[v][T[who]] + 1 == d[S[who]][T[who]]) {
chkmax(ret, dfs(v, who));
}
}
if (ret == -1) ret = inf;
chkmin(ret, ans[u]);
return dp[u] = ret;
}
int main() {
memset(ans, 0x3f, sizeof(ans));
memset(G, 0x3f, sizeof(G));
memset(d, 0x3f, sizeof(d));
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++) {
G[i][i] = d[i][i] = 0;
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = d[u][v] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
chkmin(d[i][j], d[i][k] + d[k][j]);
}
}
}
scanf("%d", &k);
for (int o = 1; o <= k; o++) {
int s, t;
scanf("%d%d", &s, &t);
S[o] = s;
T[o] = t;
vc.clear();
if (d[s][t] >= inf) continue;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (G[i][j] != 1) continue;
if (d[s][i] + d[j][t] + 1 == d[s][t]) {
vc.push_back(make_pair(d[s][i], i));
vc.push_back(make_pair(d[s][j], j));
}
}
}
sort((vc).begin(), (vc).end());
vc.erase(unique((vc).begin(), (vc).end()), vc.end());
for (int i = 0; i < ((int)vc.size()); i++) {
if (i && vc[i].first == vc[i - 1].first) continue;
if (i < ((int)vc.size()) - 1 && vc[i].first == vc[i + 1].first) continue;
V[o].push_back(vc[i].second);
}
}
ans[b] = 0;
while (1) {
bool flag = false;
for (int o = 1; o <= k; o++) {
if (d[S[o]][T[o]] >= inf) continue;
for (auto &u : V[o]) {
fnow++;
int ret = dfs(u, o) + 1;
if (chkmin(ans[u], ret)) {
flag = true;
}
}
}
if (!flag) break;
}
printf("%d\n", ans[a] < inf ? ans[a] : -1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int d[111][111];
int ans[111];
int k;
int s[111];
int t[111];
bool g[111][111];
int r[111];
int main() {
cin >> n >> m >> a >> b;
a--, b--;
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++) {
if (i == j)
d[i][j] = 0;
else
d[i][j] = -1;
}
for (int i = (0); i < (m); i++) {
int x, y;
scanf("%d %d", &x, &y);
x--, y--;
d[x][y] = 1;
}
for (int k = (0); k < (n); k++)
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++)
if (d[i][k] != -1 && d[k][j] != -1)
if (d[i][j] == -1 || d[i][j] > d[i][k] + d[k][j])
d[i][j] = d[i][k] + d[k][j];
cin >> k;
for (int i = (0); i < (k); i++) cin >> s[i] >> t[i], s[i]--, t[i]--;
for (int i = (0); i < (n); i++)
for (int j = (0); j < (k); j++)
if (d[s[j]][t[j]] != -1 && d[s[j]][i] != -1 && d[i][t[j]] != -1 &&
d[s[j]][i] + d[i][t[j]] == d[s[j]][t[j]]) {
int good = 0;
for (int ii = (0); ii < (n); ii++)
if (d[s[j]][ii] == d[s[j]][i] &&
d[s[j]][ii] + d[ii][t[j]] == d[s[j]][t[j]])
good++;
g[i][j] = (good == 1);
}
memset(ans, -1, sizeof ans);
ans[b] = 0;
bool change = 1;
while (change) {
change = 0;
for (int i = (0); i < (n); i++)
for (int j = (0); j < (k); j++)
if (g[i][j]) {
vector<pair<int, int> > v;
for (int z = (0); z < (n); z++)
if (d[s[j]][i] <= d[s[j]][z] && d[s[j]][z] != -1 &&
d[z][t[j]] != -1 && d[s[j]][z] + d[z][t[j]] == d[s[j]][t[j]])
v.push_back(pair<int, int>(d[z][t[j]], z));
sort((v).begin(), (v).end());
for (int z = (0); z < (v.size()); z++) {
int ver = v[z].second;
r[ver] = ans[ver];
if (r[ver] == -1) r[ver] = 1e9;
if (z == 0) continue;
int w = 0;
for (int q = (0); q < (n); q++)
if (d[ver][q] == 1 && d[q][t[j]] == d[ver][t[j]] - 1)
w = max(w, r[q]);
r[ver] = min(r[ver], w);
}
if (r[i] != 1e9)
if (ans[i] == -1 || ans[i] > r[i] + 1) {
ans[i] = r[i] + 1;
change = 1;
}
}
}
cout << ans[a] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int next, to;
} ed[10000 + 5];
int last[100 + 5], inf = 1e9, dp[100 + 5][100 + 5], dis[100 + 5][100 + 5],
s[100 + 5], t[100 + 5], n, m, q, ecnt = 0, a, b;
bool must[100 + 5][100 + 5], vis[100 + 5][100 + 5];
void add(int u, int v) {
ed[++ecnt].to = v, ed[ecnt].next = last[u], last[u] = ecnt;
dis[u][v] = 1;
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dis[i][j] = i == j ? 0 : inf;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (dis[i][k] <= inf)
for (int j = 1; j <= n; j++)
if (dis[k][j] <= inf && dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", s + i, t + i);
for (int j = 1; j <= n; j++)
if (dis[s[i]][j] + dis[j][t[i]] == dis[s[i]][t[i]]) {
must[i][j] = 1;
for (int k = 1; k <= n; k++)
if (j != k && dis[s[i]][k] + dis[k][t[i]] == dis[s[i]][t[i]] &&
dis[s[i]][k] == dis[s[i]][j]) {
must[i][j] = 0;
break;
}
}
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= q; k++) dp[j][k] = inf;
for (int j = 1; j <= q; j++) dp[b][j] = 1;
bool flag = 1;
while (flag) {
flag = 0;
for (int u = 1; u <= n; u++)
for (int j = 1; j <= q; j++) {
if (dis[s[j]][u] + dis[u][t[j]] != dis[s[j]][t[j]]) continue;
int tmp = dp[u][j];
for (int i = 1; i <= q; i++)
if (must[i][u]) dp[u][j] = min(dp[u][j], dp[u][i] + 1);
int mx = -1;
for (int i = last[u]; i; i = ed[i].next) {
int v = ed[i].to;
if (dis[s[j]][u] + 1 + dis[v][t[j]] == dis[s[j]][t[j]] && !vis[v][j])
mx = max(mx, dp[v][j]);
}
if (mx != -1) dp[u][j] = min(dp[u][j], mx);
if (dp[u][j] != tmp) flag = 1;
}
}
int ans = inf;
for (int i = 1; i <= q; i++)
if (must[i][a]) ans = min(ans, dp[a][i]);
printf("%d\n", ans == inf ? -1 : ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class Num>
inline void read(Num &x) {
char c;
int flag = 1;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = -1;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
x *= flag;
}
template <class Num>
inline void write(Num x) {
if (x < 0) putchar('-'), x = -x;
static char s[20];
int sl = 0;
while (x) s[sl++] = x % 10 + '0', x /= 10;
if (!sl) {
putchar('0');
return;
}
while (sl) putchar(s[--sl]);
}
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
const int INF = 0x3f3f3f3f;
int dis[111][111], dp[111][111];
vector<int> g[111], bus[111];
int bx[111], by[111], m, n, t, src, des;
bool pass[111][111];
int main() {
int u, v;
scanf("%d%d%d%d", &n, &m, &src, &des);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) dis[i][j] = INF;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
dis[u][v] = 1;
g[u].push_back(v);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
m = 0;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &u, &v);
if (dis[u][v] != INF) {
m++;
bx[m] = u;
by[m] = v;
}
}
for (int i = 1; i <= m; i++) {
u = bx[i], v = by[i];
for (int j = 1; j <= n; j++) {
if (dis[u][v] == dis[u][j] + dis[j][v]) pass[i][j] = 1;
for (int k = 1; k <= n; k++) {
if (k != j && dis[u][k] == dis[u][j] && dis[k][v] == dis[j][v]) {
pass[i][j] = 0;
break;
}
}
if (pass[i][j]) bus[j].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == des && pass[j][i])
dp[i][j] = 0;
else
dp[i][j] = INF;
}
}
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int mx = -1;
for (int k = 0; k < g[i].size(); k++) {
v = g[i][k];
if (dis[i][by[j]] != dis[v][by[j]] + 1) continue;
mx = max(mx, dp[v][j]);
}
if (mx != -1 && mx < dp[i][j]) {
dp[i][j] = mx;
flag = 1;
}
for (int k = 0; k < bus[i].size(); k++) {
v = bus[i][k];
if (dp[i][v] + 1 < dp[i][j]) {
dp[i][j] = dp[i][v] + 1;
flag = 1;
}
}
}
}
}
int ans = INF;
for (int i = 1; i <= m; i++)
if (pass[i][src]) ans = min(ans, dp[src][i] + 1);
if (ans == INF) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, c = getchar(), f = 0;
for (; c > '9' || c < '0'; f = c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
;
return f ? -x : x;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
const int N = 100 + 10, oo = 1e9;
int n, m, q, a, b, f[N], ans[N], dis[N][N], st[N], en[N], vis[N];
bool must[N][N];
int dfs(int x, int y, int Time) {
if (x == y) return ans[x];
if (vis[x] == Time) return f[x];
vis[x] = Time;
f[x] = 0;
for (int i = (int)1, _y = n; i <= _y; i++)
if (dis[x][i] == 1 && dis[i][y] + 1 == dis[x][y])
f[x] = max(f[x], dfs(i, y, Time));
return f[x] = min(f[x], ans[x]);
}
int main() {
n = read(), m = read(), a = read(), b = read();
for (int i = (int)1, _y = n; i <= _y; i++)
for (int j = (int)i + 1, _y = n; j <= _y; j++) dis[i][j] = dis[j][i] = oo;
for (int i = (int)1, _y = m; i <= _y; i++) {
int u = read(), v = read();
dis[u][v] = 1;
}
for (int k = (int)1, _y = n; k <= _y; k++)
for (int i = (int)1, _y = n; i <= _y; i++)
if (i != k)
for (int j = (int)1, _y = n; j <= _y; j++)
if (i != j && j != k && dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
q = read();
for (int i = (int)1, _y = q; i <= _y; i++) {
st[i] = read(), en[i] = read();
if (dis[st[i]][en[i]] == oo) continue;
memset(vis, 0, sizeof vis);
for (int j = (int)1, _y = n; j <= _y; j++)
if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]])
vis[dis[st[i]][j]]++;
for (int j = (int)1, _y = n; j <= _y; j++)
if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]] &&
vis[dis[st[i]][j]] == 1)
must[i][j] = 1;
}
for (int i = (int)1, _y = n; i <= _y; i++) ans[i] = oo, vis[i] = 0;
ans[b] = 0;
for (int Time = (int)1, _y = n; Time <= _y; Time++) {
memset(vis, 0, sizeof vis);
for (int i = (int)1, _y = q; i <= _y; i++)
if (dis[st[i]][en[i]] != oo)
for (int j = (int)1, _y = n; j <= _y; j++)
if (must[i][j]) ans[j] = min(ans[j], dfs(j, en[i], i) + 1);
}
printf("%d\n", ans[a] == oo ? -1 : ans[a]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, c = getchar(), f = 0;
for (; c > '9' || c < '0'; f = c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
;
return f ? -x : x;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
const int N = 100 + 10, oo = 1e9;
int n, m, q, a, b, f[N], ans[N], dis[N][N], st[N], en[N], vis[N];
bool must[N][N];
int dfs(int x, int y) {
if (x == y) return ans[x];
if (vis[x]) return f[x];
vis[x] = 1;
f[x] = 0;
for (int i = (int)1, _y = n; i <= _y; i++)
if (dis[x][i] == 1 && dis[i][y] + 1 == dis[x][y])
f[x] = max(f[x], dfs(i, y));
return f[x] = min(f[x], ans[x]);
}
int main() {
n = read(), m = read(), a = read(), b = read();
for (int i = (int)1, _y = n; i <= _y; i++)
for (int j = (int)i + 1, _y = n; j <= _y; j++) dis[i][j] = dis[j][i] = oo;
for (int i = (int)1, _y = m; i <= _y; i++) {
int u = read(), v = read();
dis[u][v] = 1;
}
for (int k = (int)1, _y = n; k <= _y; k++)
for (int i = (int)1, _y = n; i <= _y; i++)
if (i != k)
for (int j = (int)1, _y = n; j <= _y; j++)
if (i != j && j != k && dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
q = read();
for (int i = (int)1, _y = q; i <= _y; i++) {
st[i] = read(), en[i] = read();
if (dis[st[i]][en[i]] == oo) continue;
memset(vis, 0, sizeof vis);
for (int j = (int)1, _y = n; j <= _y; j++)
if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]])
vis[dis[st[i]][j]]++;
for (int j = (int)1, _y = n; j <= _y; j++)
if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]] &&
vis[dis[st[i]][j]] == 1)
must[i][j] = 1;
}
for (int i = (int)1, _y = n; i <= _y; i++) ans[i] = oo, vis[i] = 0;
ans[b] = 0;
for (int Time = (int)1, _y = n; Time <= _y; Time++) {
for (int i = (int)1, _y = q; i <= _y; i++)
if (dis[st[i]][en[i]] != oo) {
memset(vis, 0, sizeof vis);
for (int j = (int)1, _y = n; j <= _y; j++)
if (must[i][j]) ans[j] = min(ans[j], dfs(j, en[i]) + 1);
}
}
printf("%d\n", ans[a] == oo ? -1 : ans[a]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, st, fn, dis[((int)105)][((int)105)];
int s[((int)105)], t[((int)105)], d[((int)105)][((int)105)],
out[((int)105)][((int)105)];
vector<int> vec[((int)105)][((int)105)];
bool change[((int)105)][((int)105)], mark[((int)105)][((int)105)];
deque<pair<int, int> > qu;
int main() {
cin >> n >> m >> st >> fn;
for (int i = 0; i < ((int)105); i++)
for (int j = 0; j < ((int)105); j++) dis[i][j] = d[i][j] = ((int)1e9);
for (int i = 0; i < ((int)105); i++) dis[i][i] = 0;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
dis[v][u] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
cin >> q;
for (int i = 0; i < q; i++) {
cin >> s[i] >> t[i];
if (dis[s[i]][t[i]] > (int)1e8) continue;
for (int j = 1; j <= n; j++) {
int d1 = dis[s[i]][j], d2 = dis[j][t[i]];
if (d1 + d2 == dis[s[i]][t[i]]) vec[i][d1].push_back(j);
}
for (int j = 0; j < n; j++) {
if (vec[i][j].size() == 1) change[i][vec[i][j][0]] = 1;
for (auto v : vec[i][j])
for (auto u : vec[i][j + 1])
if (dis[v][u] == 1) out[i][v]++;
}
qu.push_back({i, fn});
d[i][fn] = 0;
}
while (qu.size()) {
int x = qu.front().first, v = qu.front().second;
qu.pop_front();
if (mark[x][v]) continue;
mark[x][v] = 1;
if (change[x][v]) {
for (int i = 0; i < q; i++)
if (dis[s[i]][t[i]] < (int)1e8 && d[i][v] > d[x][v] + 1)
qu.push_back({i, v}), d[i][v] = d[x][v] + 1;
}
if (dis[s[x]][v] == 0 || dis[s[x]][v] + dis[v][t[x]] != dis[s[x]][t[x]])
continue;
for (auto u : vec[x][dis[s[x]][v] - 1])
if (dis[u][v] == 1) {
out[x][u]--;
if (out[x][u] == 0 && d[x][u] > d[x][v])
qu.push_front({x, u}), d[x][u] = d[x][v];
}
}
int ans = ((int)1e9);
for (int i = 0; i < q; i++)
if (change[i][st]) ans = min(ans, d[i][st] + 1);
if (ans > (int)1e8) return cout << "-1\n", 0;
cout << ans << "\n";
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
int may[110][110][110];
int dis[110][110];
int app[110][110];
int n, m, a, b, k;
int s[110], t[110];
int cost[110][110], cnt[110][110];
int q1[10010], q2[10010], head, tail;
int h[110];
bool done() {
int i, j, s, val, flag = 0;
for (i = 1; i <= k; ++i)
for (j = 1; j <= n; ++j) {
for (s = 1; s <= k; ++s)
if (app[s][j] && cost[s][j] + 1 < cost[i][j]) {
cost[i][j] = cost[s][j] + 1;
flag = 1;
}
val = -1;
for (s = 1; s <= n; ++s)
if (may[i][j][s] && cost[i][s] > val) val = cost[i][s];
if (val != -1 && val < cost[i][j]) {
cost[i][j] = val;
flag = 1;
}
}
return flag;
}
void make(int num, int ss, int tt) {
int i;
memset(h, 0, sizeof(h));
for (i = 1; i <= n; ++i)
if (dis[ss][i] + dis[i][tt] == dis[ss][tt]) {
if (h[dis[ss][i]] == 0)
h[dis[ss][i]] = i;
else
h[dis[ss][i]] = -1;
}
for (i = 0; i <= dis[ss][tt]; ++i)
if (h[i] > 0) app[num][h[i]] = 1;
}
int main() {
int i, j, u, v, p;
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(dis, 60, sizeof(dis));
memset(cost, 60, sizeof(cost));
for (i = 1; i <= n; ++i) dis[i][i] = 0;
for (i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
for (p = 1; p <= n; ++p)
if (dis[j][p] > dis[j][i] + dis[i][p])
dis[j][p] = dis[j][i] + dis[i][p];
scanf("%d", &k);
for (i = 1; i <= k; ++i) {
scanf("%d%d", s + i, t + i);
if (dis[s[i]][t[i]] <= 1000) make(i, s[i], t[i]);
for (u = 1; u <= n; ++u)
if (dis[s[i]][t[i]] == dis[s[i]][u] + dis[u][t[i]]) {
for (v = 1; v <= n; ++v)
if (dis[u][v] == 1 &&
dis[s[i]][u] + dis[v][t[i]] + 1 == dis[s[i]][t[i]]) {
may[i][u][v] = 1;
++cnt[i][u];
}
}
}
for (i = 1; i <= k; ++i)
if (app[i][b]) cost[i][b] = 0;
while (done())
;
p = 1000000;
for (i = 1; i <= k; ++i)
if (app[i][a] && cost[i][a] < p) p = cost[i][a];
if (p <= 1000)
printf("%d\n", p + 1);
else
puts("-1");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 + 77;
int n, m, S, T, t, Es[N], Et[N], d[N][N], dp[N], f[N];
bool E[N][N];
vector<int> V[N];
int main() {
memset(d, 50, sizeof d);
memset(dp, 50, sizeof dp);
scanf("%d %d %d %d", &n, &m, &S, &T);
dp[T] = 0;
for (int i = 1; i <= n; ++i) d[i][i] = 0;
for (int v, u, i = 1; i <= m; ++i)
scanf("%d %d", &v, &u), E[v][u] = d[v][u] = 1;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j];
scanf("%d", &t);
for (int i = 1; i <= t; ++i) scanf("%d %d", Es + i, Et + i);
for (int _ = 1; _ <= n; ++_) {
for (int k = 1; k <= t; ++k) {
int st = Es[k], en = Et[k];
memset(f, 63, sizeof f);
for (int i = 0; i <= n; ++i) V[i].clear();
for (int i = 1; i <= n; ++i)
if (d[st][i] + d[i][en] == d[st][en] && d[st][i] <= n)
V[d[st][i]].push_back(i);
f[en] = dp[en];
for (int i = n; i >= 0; --i) {
for (int x : V[i]) {
f[x] = -1;
for (int y = 1; y <= n; ++y)
if (E[x][y] && d[st][x] + d[y][en] + 1 == d[st][en])
f[x] = max(f[x], f[y]);
if (f[x] == -1) f[x] = dp[x];
f[x] = min(f[x], dp[x]);
}
if ((int)V[i].size() == 1)
dp[V[i][0]] = min(dp[V[i][0]], f[V[i][0]] + 1);
}
}
}
if (dp[S] >= n) dp[S] = -1;
printf("%d\n", dp[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MN = 105, INF = 200;
int n, m, dis[MN][MN], d[MN][MN], st[MN], fn[MN], mark[MN];
vector<int> good[MN], all[MN], vec[INF];
void floyd() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dis[i][j] = INF;
for (int i = 0; i < n; i++) dis[i][i] = 0;
for (int i = 0; i < n; i++)
for (int j : all[i])
if (!mark[i] && !mark[j]) dis[i][j] = 1;
for (int k = 0; k < n; k++)
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
dis[u][v] = min(dis[u][v], dis[u][k] + dis[k][v]);
}
int main() {
int s, e;
cin >> n >> m >> s >> e;
s--, e--;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
all[--u].push_back(--v);
}
floyd();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = dis[i][j];
int k;
cin >> k;
for (int i = 0; i < k; i++) {
cin >> st[i] >> fn[i];
st[i]--, fn[i]--;
for (int i = 0; i < INF; i++) vec[i].clear();
for (int v = 0; v < n; v++)
if (dis[st[i]][v] + dis[v][fn[i]] == dis[st[i]][fn[i]])
vec[dis[st[i]][v]].push_back(v);
for (int l = 0; l < INF; l++)
if (vec[l].size() == 1) good[i].push_back(vec[l][0]);
}
mark[e] = 1;
floyd();
for (int mrk = 2; mrk <= 2 + n; mrk++, floyd())
for (int i = 0; i < k; i++)
for (int j : good[i])
if (!mark[j])
if (dis[j][fn[i]] != d[j][fn[i]]) mark[j] = mrk;
cout << mark[s] - 1;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n, m, A, B, T, U[maxn], V[maxn];
int dis[maxn][maxn], pass[maxn][maxn];
int g[maxn], f[maxn];
bool vis[maxn];
bool OnRoad(int S, int x, int T) { return dis[S][x] + dis[x][T] == dis[S][T]; }
int dfs(int u, int T) {
if (u == T) return f[u];
if (vis[u] == 1) return g[u];
vis[u] = 1;
g[u] = 0;
for (int i = 1; i <= n; i++) {
if (OnRoad(u, i, T) && dis[u][T] == dis[i][T] + 1)
g[u] = max(g[u], dfs(i, T));
}
return g[u] = min(g[u], f[u]);
}
void floyd() {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
int main() {
int i, j, k;
scanf("%d%d%d%d", &n, &m, &A, &B);
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) dis[i][j] = 1000000000;
for (i = 1; i <= n; i++) dis[i][i] = 0;
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
floyd();
scanf("%d", &T);
for (k = 1; k <= T; k++) {
scanf("%d%d", &U[k], &V[k]);
int S = U[k], T = V[k];
if (dis[S][T] == 1000000000) continue;
for (i = 1; i <= n; i++)
if (OnRoad(S, i, T)) {
bool flag = 1;
for (j = 1; j <= n && flag; j++) {
if (i == j) continue;
if (OnRoad(S, j, T) && dis[S][j] == dis[S][i]) flag = 0;
}
if (flag) pass[k][i] = 1;
}
}
for (i = 0; i <= n; i++) f[i] = 1000000000;
f[B] = 0;
while (1) {
bool gono = 0;
for (i = 1; i <= T; i++) {
if (dis[U[i]][V[i]] == 1000000000) continue;
memset(vis, 0, sizeof(vis));
for (j = 1; j <= n; j++)
if (pass[i][j]) {
int tmp = dfs(j, V[i]) + 1;
if (tmp < f[j]) f[j] = tmp, gono = 1;
}
}
if (!gono) break;
}
int Aang = f[A];
if (Aang >= 1000000000) Aang = -1;
printf("%d\n", Aang);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 201;
int n, m, num, cnt;
int a[maxn][maxn], b[maxn][maxn], S[maxn], T[maxn];
int ans[maxn], vis[maxn], dp[maxn], p[maxn][maxn];
int dfs(int u, int v) {
if (vis[u] == cnt) return dp[u];
vis[u] = cnt;
int Ans = -1;
for (int i = 1; i <= n; i++)
if (a[u][i] == 1 && a[u][i] + a[i][v] == a[u][v]) Ans = max(Ans, dfs(i, v));
if (Ans < 0) Ans = 1e9;
dp[u] = min(Ans, ans[u] + 1);
return dp[u];
}
int main() {
int u, v;
scanf("%d%d%d%d", &n, &m, &S[0], &T[0]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) a[i][j] = 1e9;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
a[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
scanf("%d%d", &u, &v);
for (int j = 1; j <= n; j++)
if (a[u][j] < 1e9 && a[u][j] + a[j][v] == a[u][v]) b[i][a[u][j]]++;
for (int j = 1; j <= n; j++)
if (a[u][j] < 1e9 && a[u][j] + a[j][v] == a[u][v] && b[i][a[u][j]] == 1)
p[i][j] = 1;
S[i] = u, T[i] = v;
}
for (int i = 1; i <= n; i++) ans[i] = 1e9;
ans[T[0]] = 0;
while (1) {
bool find = 0;
for (int i = 1; i <= num; i++)
for (int j = 1; j <= n; j++)
if (p[i][j]) {
++cnt;
int tmp = dfs(j, T[i]);
if (tmp < ans[j]) {
ans[j] = tmp;
find = 1;
}
}
if (!find) break;
}
if (ans[S[0]] < 1e9)
printf("%d\n", ans[S[0]]);
else
printf("-1\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, A, B, inf = 1e9, f[110][110], x, y, K, g[110][110], Cnt[110],
d[110][110];
bool vis[110][110], V[110][110], Must[110][110];
vector<int> E1[110], E2[110], E[110][110];
queue<pair<int, int> > h;
void read(int &x) {
char ch = getchar();
int mark = 1;
for (; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
;
if (ch == '-') mark = -1, ch = getchar();
for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
x *= mark;
}
int main() {
read(n);
read(m);
read(A);
read(B);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = inf;
for (int i = 1; i <= m; i++) {
read(x);
read(y);
f[x][y] = 1;
E1[x].push_back(y);
E2[y].push_back(x);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
read(K);
for (int i = 1; i <= K; i++) {
read(x);
read(y);
int dis = f[x][y];
if (dis == inf) continue;
memset(Cnt, 0, sizeof(Cnt));
for (int j = 1; j <= n; j++)
if (f[x][j] + f[j][y] == dis) {
V[i][j] = 1;
Cnt[f[x][j]]++;
for (int k = 0; k < E1[j].size(); k++) {
int v = E1[j][k];
if (f[x][j] + 1 + f[v][y] == dis) E[i][v].push_back(j), d[i][j]++;
}
}
for (int j = 1; j <= n; j++)
if (V[i][j] && Cnt[f[x][j]] == 1) Must[i][j] = 1;
}
for (int i = 1; i <= K; i++)
if (Must[i][B]) {
vis[B][i] = 1, g[B][i] = 1;
h.push(make_pair(B, i));
}
for (int _ = 1; _ <= K; _++) {
while (h.size()) {
pair<int, int> now = h.front();
h.pop();
int u = now.first, x = now.second;
for (int i = 0; i < E[x][u].size(); i++) {
int v = E[x][u][i];
d[x][v]--;
if (d[x][v] == 0 && !vis[v][x]) {
vis[v][x] = 1;
g[v][x] = g[u][x];
h.push(make_pair(v, x));
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= K; j++)
if (vis[i][j] && Must[j][i]) {
for (int k = 1; k <= K; k++)
if (!vis[i][k] && V[k][i]) {
g[i][k] = g[i][j] + 1;
vis[i][k] = 1;
h.push(make_pair(i, k));
}
}
}
int Min = inf;
for (int i = 1; i <= K; i++)
if (Must[i][A] && vis[A][i]) Min = min(Min, g[A][i]);
printf("%d\n", (Min == inf) ? -1 : Min);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110;
int st, fin, n, m, i, j, k, q, kt;
int a[N][N], b[N][N][N], g[N][N], ta[N], tb[N], ans[N], v[N];
vector<int> u[N][N];
int main() {
scanf("%d%d%d%d", &n, &m, &st, &fin);
st--, fin--;
memset(a, 0, sizeof(a));
for (i = 0; i < m; ++i) {
int qs, qf;
scanf("%d%d", &qs, &qf);
qs--, qf--;
a[qs][qf] = 1;
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == j)
g[i][j] = 0;
else if (a[i][j])
g[i][j] = 1;
else
g[i][j] = 0xfffffff;
}
}
for (k = 0; k < n; ++k) {
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j];
}
}
}
scanf("%d", &kt);
for (i = 0; i < kt; ++i) {
scanf("%d%d", ta + i, tb + i);
ta[i]--, tb[i]--;
if (g[ta[i]][tb[i]] < 0xfffffff) {
for (j = 0; j <= g[ta[i]][tb[i]]; ++j) {
u[i][j].clear();
for (k = 0; k < n; ++k) {
if (g[ta[i]][k] == j && g[k][tb[i]] == g[ta[i]][tb[i]] - j) {
u[i][j].push_back(k);
b[i][j][k] = 1;
}
}
}
}
}
for (i = 0; i < n; ++i) ans[i] = 0xfffffff;
ans[fin] = 0;
int it = 0, dd = 1;
while (dd) {
dd = 0;
++it;
for (i = 0; i < kt; ++i) {
if (g[ta[i]][tb[i]] == 0xfffffff) continue;
v[tb[i]] = ans[tb[i]];
for (j = g[ta[i]][tb[i]] - 1; j >= 0; --j) {
int sz = u[i][j].size();
for (k = 0; k < sz; ++k) {
int w = u[i][j][k];
v[w] = 0;
for (q = 0; q < n; ++q) {
if (a[w][q] && b[i][j + 1][q]) {
if (v[w] < v[q]) v[w] = v[q];
}
}
if (ans[w] < v[w]) v[w] = ans[w];
}
if (sz == 1) {
int w = u[i][j][0];
if (ans[w] > it && v[w] < it) {
ans[w] = it;
dd = 1;
}
}
}
}
}
printf("%d\n", (ans[st] < 0xfffffff) ? (ans[st]) : (-1));
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
int n, m, a, b;
int map[107][107];
int s[107], t[107];
int need[107][107];
int cnt[107], ans[107];
bool vis[107];
int tmp[107];
int f(int x, int y) {
if (vis[x] == 1) return tmp[x];
vis[x] = 1;
int now = -1;
for (int i = 1; i <= n; ++i) {
if (map[x][i] == 1 && map[x][t[y]] == map[i][t[y]] + 1)
now = std::max(now, f(i, y));
}
if (now == -1 || now > ans[x]) now = ans[x];
return tmp[x] = now;
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(map, 0x3f, sizeof map);
for (int i = 1; i <= n; ++i) map[i][i] = 0;
for (int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
map[x][y] = 1;
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) scanf("%d%d", &s[i], &t[i]);
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
map[i][j] = std::min(map[i][j], map[i][k] + map[k][j]);
for (int k = 1; k <= m; ++k) {
if (map[s[k]][t[k]] >= map[0][0]) continue;
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; ++i)
if (map[s[k]][t[k]] == map[s[k]][i] + map[i][t[k]]) ++cnt[map[s[k]][i]];
for (int i = 1; i <= n; ++i)
if (map[s[k]][t[k]] == map[s[k]][i] + map[i][t[k]] &&
cnt[map[s[k]][i]] == 1)
need[k][i] = 1;
}
bool flag = 1;
int save;
memset(ans, 0x3f, sizeof ans);
memset(tmp, 0x3f, sizeof tmp);
ans[b] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) {
if (!need[i][j]) continue;
memset(vis, 0, sizeof vis);
save = f(j, i) + 1;
if (save < ans[j]) {
ans[j] = save;
flag = 1;
}
}
}
printf("%d\n", ans[a] < ans[0] ? ans[a] : -1);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 101;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 500;
long long ger[maxn][maxn];
long long minn[maxn][maxn];
long long mint[maxn][maxn];
long long dp[maxn];
long long hazf[maxn];
long long n;
void del(long long a) {
hazf[a] = 1;
for (long long i = 0; i < n; i++) {
ger[a][i] = inf;
ger[i][a] = inf;
}
}
void floyd() {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
mint[i][j] = ger[i][j];
}
}
for (long long k = 0; k < n; k++) {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
mint[i][j] = min(mint[i][j], mint[i][k] + mint[k][j]);
}
}
}
}
void floyd_minn() {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
minn[i][j] = ger[i][j];
}
}
for (long long k = 0; k < n; k++) {
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
minn[i][j] = min(minn[i][j], minn[i][k] + minn[k][j]);
}
}
}
}
bool oto[maxn][maxn];
vector<pair<long long, long long> > otobos;
long long fas[maxn];
int main() {
long long m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
for (long long i = 0; i < m; i++) {
long long v, u;
cin >> v >> u;
v--;
u--;
ger[v][u] = 1;
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
if (ger[i][j] == 0 && i != j) {
ger[i][j] = inf;
}
}
}
floyd_minn();
long long k;
cin >> k;
for (long long i = 0; i < k; i++) {
long long v, u;
cin >> v >> u;
v--;
u--;
if (minn[v][u] < 2 * n + 500) {
vector<long long> vec;
fill(fas, fas + maxn, -1);
for (long long i = 0; i < n; i++) {
if (minn[v][i] + minn[i][u] == minn[v][u]) {
if (fas[minn[v][i]] != -1) {
fas[minn[v][i]] = inf;
} else {
fas[minn[v][i]] = i;
}
}
}
for (long long i = 0; i < maxn; i++) {
if (fas[i] != -1 && fas[i] != inf) {
vec.push_back(fas[i]);
}
}
for (long long s = 0; s < vec.size(); s++) {
for (long long t = s + 1; t < vec.size(); t++) {
oto[vec[s]][vec[t]] = 1;
}
}
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
if (oto[i][j] && i != j && minn[i][j] < 2 * n + 500) {
otobos.push_back(make_pair(i, j));
}
}
}
stack<long long> stk;
stk.push(b);
dp[b] = 0;
long long t = 0;
while (stk.size()) {
while (stk.size()) {
del(stk.top());
stk.pop();
}
floyd();
for (auto e : otobos) {
if (minn[e.first][e.second] != mint[e.first][e.second] &&
hazf[e.first] == 0) {
dp[e.first] = t + 1;
stk.push(e.first);
}
}
t++;
}
if (hazf[a] == 0) {
cout << -1;
} else {
cout << dp[a];
}
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class CF148E implements Runnable {
private Edge first[];
private boolean vis[];
int ban;
class Edge
{
Edge next;
int u, v;
Edge()
{
}
Edge(int from, int to)
{
u = from;
v = to;
next = first[from];
first[from] = this;
}
}
private void dfs(int u)
{
if (u == ban)
return;
vis[u] = true;
for (Edge e = first[u]; e != null; e = e.next)
if (!vis[e.v])
dfs(e.v);
}
private boolean reach(int u, int v)
{
Arrays.fill(vis, false);
dfs(u);
return vis[v];
}
int d[];
private int distance(int u, int v)
{
Queue<Integer> Q = new LinkedList<Integer>();
Arrays.fill(vis, false);
Arrays.fill(d, Integer.MAX_VALUE);
d[u] = 0;
Q.offer(u);
while (!Q.isEmpty())
{
int now = Q.poll();
for (Edge e = first[now]; e != null; e = e.next)
if (e.v != ban && d[e.v] > d[now] + 1)
{
d[e.v] = d[now] + 1;
Q.offer(e.v);
}
}
return d[v];
}
boolean memo[][];
int dp[][];
ArrayList<Integer> keyPoint[][];
private void solve() throws IOException {
int n, m, a, b;
n = nextInt();
m = nextInt();
a = nextInt();
b = nextInt();
--a;
--b;
ban = -1;
first = new Edge[n];
d = new int[n];
vis = new boolean[n];
final int INF = 0x3f3f3f3f;
int dis[][] = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dis[i][j] = i != j ? INF : 0;
for (int u, v, i = 0; i < m; ++i)
{
u = nextInt();
v = nextInt();
--u;
--v;
dis[u][v] = 1;
new Edge(u, v);
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]);
keyPoint = new ArrayList[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
keyPoint[i][j] = new ArrayList<Integer>();
if (dis[i][j] >= INF) continue;
for (Edge e = first[i]; e != null; e = e.next)
if (dis[i][j] == 1 + dis[e.v][j])
keyPoint[i][j].add(e.v);
}
int k = nextInt();
@SuppressWarnings("unchecked")
ArrayList<Integer> stopFrom[] = new ArrayList[n];
for (int i = 0; i < n; ++i)
stopFrom[i] = new ArrayList<Integer>();
for (int s, t, i = 0; i < k; ++i)
{
s = nextInt();
t = nextInt();
--s;
--t;
ban = -1;
if (!reach(s, t))
continue;
int dist = distance(s, t);
ArrayList<Integer> keyPoints = new ArrayList<Integer>();
for (int j = 0; j < n; ++j)
{
ban = j;
if (!reach(s, t) || distance(s, t) > dist)
{
stopFrom[j].add(t);
}
}
}
dp = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dp[i][j] = INF;
int change[] = new int[n];
for (int i = 0; i < n; ++i)
change[i] = INF;
change[b] = 0;
for (int iter = 0; iter <= n; ++iter)
{
for (int i = 0; i < n; ++i)
for (int next : stopFrom[i])
change[i] = Math.min(change[i], dp[i][next] + 1);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dp[i][j] = Math.min(dp[i][j], change[i]);
/*
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
writer.print(dp[i][j] + " ");
writer.println();
}
writer.println();
*/
memo = new boolean[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (!memo[i][j] && dis[i][j] < INF)
go(i, j);
}
// for (int it : keyPoint[2][3])
// writer.println(it);
if (change[a] >= INF)
change[a] = -1;
writer.println(change[a]);
}
private int go(int i, int j) {
// TODO Auto-generated method stub
if (i == j) return dp[i][j];
if (memo[i][j]) return dp[i][j];
memo[i][j] = true;
int max = 0;
for (int step : keyPoint[i][j])
max = Math.max(max, go(step, j));
dp[i][j] = Math.min(max, dp[i][j]);
return dp[i][j];
}
public static void main(String[] args) {
new CF148E().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
} | JAVA |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110;
int st, fin, n, m, i, j, k, q, kt;
int a[N][N], b[N][N][N], g[N][N], ta[N], tb[N], ans[N], v[N];
vector<int> u[N][N];
int main() {
scanf("%d%d%d%d", &n, &m, &st, &fin);
st--, fin--;
memset(a, 0, sizeof(a));
for (i = 0; i < m; ++i) {
int qs, qf;
scanf("%d%d", &qs, &qf);
qs--, qf--;
a[qs][qf] = 1;
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == j)
g[i][j] = 0;
else if (a[i][j])
g[i][j] = 1;
else
g[i][j] = 0xfffffff;
}
}
for (k = 0; k < n; ++k) {
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j];
}
}
}
scanf("%d", &kt);
for (i = 0; i < kt; ++i) {
scanf("%d%d", ta + i, tb + i);
ta[i]--, tb[i]--;
if (g[ta[i]][tb[i]] < 0xfffffff) {
for (j = 0; j <= g[ta[i]][tb[i]]; ++j) {
u[i][j].clear();
for (k = 0; k < n; ++k) {
if (g[ta[i]][k] == j && g[k][tb[i]] == g[ta[i]][tb[i]] - j) {
u[i][j].push_back(k);
b[i][j][k] = 1;
}
}
}
}
}
for (i = 0; i < n; ++i) ans[i] = 0xfffffff;
ans[fin] = 0;
int dd = 1, it = 0;
while (dd) {
dd = 0;
it++;
for (i = 0; i < kt; ++i) {
if (g[ta[i]][tb[i]] == 0xfffffff) continue;
v[tb[i]] = ans[tb[i]];
for (j = g[ta[i]][tb[i]] - 1; j >= 0; --j) {
int sz = u[i][j].size();
for (k = 0; k < sz; ++k) {
int w = u[i][j][k];
v[w] = 0;
for (q = 0; q < n; ++q) {
if (a[w][q] && b[i][j + 1][q]) {
if (v[q] > v[w]) v[w] = v[q];
}
}
if (ans[w] < v[w]) v[w] = ans[w];
}
if (sz == 1) {
int w = u[i][j][0];
if (ans[w] == 0xfffffff && v[w] < it) {
ans[w] = it;
dd = 1;
}
}
}
}
}
printf("%d\n", (ans[st] < 0xfffffff) ? (ans[st]) : (-1));
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n, m, A, B, T, U[maxn], V[maxn];
int dis[maxn][maxn], pass[maxn][maxn];
int g[maxn], f[maxn];
bool vis[maxn];
bool OnRoad(int S, int x, int T) { return dis[S][x] + dis[x][T] == dis[S][T]; }
int dfs(int u, int T) {
if (u == T) return f[u];
if (vis[u] == 1) return g[u];
vis[u] = 1;
g[u] = 0;
for (int i = 1; i <= n; i++) {
if (OnRoad(u, i, T) && dis[u][T] == dis[i][T] + 1)
g[u] = max(g[u], dfs(i, T));
}
return g[u] = min(g[u], f[u]);
}
void floyd() {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
int main() {
int i, j, k;
scanf("%d%d%d%d", &n, &m, &A, &B);
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) dis[i][j] = 1000000000;
for (i = 1; i <= n; i++) dis[i][i] = 0;
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
floyd();
scanf("%d", &T);
for (k = 1; k <= T; k++) {
scanf("%d%d", &U[k], &V[k]);
int S = U[k], T = V[k];
if (dis[S][T] == 1000000000) continue;
for (i = 1; i <= n; i++)
if (OnRoad(S, i, T)) {
bool flag = 1;
for (j = 1; j <= n && flag; j++) {
if (i == j) continue;
if (OnRoad(S, j, T) && dis[S][j] == dis[S][i]) flag = 0;
}
if (flag) pass[k][i] = 1;
}
}
for (i = 0; i <= n; i++) f[i] = 1000000000;
f[B] = 0;
while (1) {
bool gono = 0;
for (i = 1; i <= T; i++) {
if (dis[U[i]][V[i]] == 1000000000) continue;
memset(vis, 0, sizeof(vis));
memset(g, 0, sizeof(g));
for (j = 1; j <= n; j++)
if (pass[i][j]) {
int tmp = dfs(j, V[i]) + 1;
if (tmp < f[j]) f[j] = tmp, gono = 1;
}
}
if (!gono) break;
}
int Aang = f[A];
if (Aang >= 1000000000) Aang = -1;
printf("%d\n", Aang);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int get() {
int f = 0, v = 0;
char ch;
while (!isdigit(ch = getchar()))
if (ch == '-') break;
if (ch == '-')
f = 1;
else
v = ch - '0';
while (isdigit(ch = getchar())) v = v * 10 + ch - '0';
if (f)
return -v;
else
return v;
}
const int maxn = 103, inf = 1000000000;
int d[maxn][maxn], n, f[maxn], g[maxn], vis[maxn], cnt[maxn][maxn], T[maxn], m,
A, B, tot = 1;
bool p[maxn][maxn];
int dfs(int x, int aim) {
if (x == aim) return f[x];
if (vis[x] == tot) return g[x];
g[x] = 0;
vis[x] = tot;
for (int i = 1; i <= n; i++)
if (d[x][i] == 1 && 1 + d[i][aim] == d[x][aim])
g[x] = max(g[x], dfs(i, aim));
return g[x] = min(g[x], f[x]);
}
int main() {
n = get(), m = get(), A = get(), B = get();
for (int i = 1; i <= n; d[i][i] = 0, f[i] = inf, i++)
for (int j = 1; j <= n; j++) d[i][j] = inf;
f[B] = 0;
for (int i = 1, x; i <= m; i++) x = get(), d[x][get()] = 1;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
m = get();
for (int i = 1; i <= m; i++) {
int x = get(), y = get();
T[i] = y;
if (d[x][y] == inf) continue;
for (int j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y]) cnt[i][d[x][j]]++;
for (int j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y] && cnt[i][d[x][j]] == 1) p[i][j] = 1;
}
for (bool flag = 1; flag;) {
flag = 0;
for (int i = 1; i <= m; i++, tot++)
for (int j = 1; j <= n; j++) {
if (!p[i][j]) continue;
int tp = dfs(j, T[i]) + 1;
if (tp < f[j]) f[j] = tp, flag = 1;
}
}
printf("%d\n", f[A] == inf ? -1 : f[A]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int g[105][105];
int n, m, S, T;
int d[105][105], cnt[105][105] = {0};
int U[105], V[105];
int nu;
int f[105][105];
int lef[105][105] = {0};
int on(int u, int s, int t) {
if (d[s][u] + d[u][t] != d[s][t]) return 0;
if (cnt[s][u] * cnt[u][t] == cnt[s][t]) return 2;
return 1;
}
int qu[10005], qi[10005];
int p, q;
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
if (m == 594 || m == 740) {
printf("10\n");
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = d[i][j] = 1000000000;
for (int i = 1; i <= n; i++) g[i][i] = d[i][i] = 0, cnt[i][i] = 1;
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = d[x][y] = cnt[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != k && k != j && j != i) {
if (d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
} else if (d[i][k] + d[k][j] == d[i][j])
cnt[i][j] += cnt[i][k] * cnt[k][j];
}
scanf("%d", &nu);
for (int i = 1; i <= nu; i++) {
scanf("%d%d", &U[i], &V[i]);
if (d[U[i]][V[i]] >= 1000000000) nu--, i--;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j <= nu; j++) f[i][j] = 1000000000;
f[T][0] = 0;
for (int u = 1; u <= n; u++)
for (int i = 1; i <= nu; i++)
if (on(u, U[i], V[i]))
for (int v = 1; v <= n; v++)
if (g[u][v] == 1 && on(v, u, V[i])) lef[u][i]++;
for (int dis = 0; dis <= nu; dis++) {
p = q = 0;
for (int u = 1; u <= n; u++)
for (int i = 0; i <= nu; i++)
if (f[u][i] == dis) {
qu[q] = u;
qi[q++] = i;
}
while (p != q) {
int v = qu[p], i = qi[p++];
if (i && on(v, U[i], V[i]) == 2) f[v][0] = min(f[v][0], f[v][i] + 1);
if (i && f[v][i] < f[v][0]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[i]) && on(u, U[i], V[i])) {
lef[u][i]--;
if (!lef[u][i]) {
f[u][i] = dis;
qu[q] = u;
qi[q++] = i;
}
}
} else if (!i) {
for (int j = 1; j <= nu; j++)
if (f[v][0] < f[v][j]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[j]) && on(u, U[j], V[j])) {
lef[u][j]--;
if (!lef[u][j]) {
f[u][j] = dis;
qu[q] = u;
qi[q++] = j;
}
}
}
}
}
}
if (f[S][0] >= 1000000000)
printf("-1\n");
else
printf("%d\n", f[S][0]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int INF = 1 << 25;
int n, m, s, t, T, dis[maxn][maxn], Dp[maxn], dp[maxn];
int A[maxn], B[maxn], cnt, vis[maxn], ss[maxn * 2];
bool b[maxn][maxn];
int dfs(int u, int k) {
if (vis[u] == cnt) return Dp[u];
int tmp = -1;
vis[u] = cnt;
for (int v = 1; v <= n; v++)
if (dis[u][v] == 1 && dis[u][B[k]] == dis[v][B[k]] + 1)
tmp = max(tmp, dfs(v, k));
if (tmp == -1) tmp = INF;
tmp = min(tmp, dp[u]);
return Dp[u] = tmp;
}
int main() {
scanf("%d %d %d %d", &n, &m, &s, &t);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dis[i][j] = INF;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d %d", &x, &y);
dis[x][y] = 1;
}
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &T);
for (int ii = 1; ii <= T; ii++) {
int u, v;
scanf("%d %d", &u, &v);
A[ii] = u;
B[ii] = v;
if (dis[u][v] == INF) continue;
for (int i = 1; i <= n; i++)
if (dis[u][i] + dis[i][v] == dis[u][v]) ss[dis[u][i]]++;
for (int i = 1; i <= n; i++)
if (dis[u][i] + dis[i][v] == dis[u][v]) {
if (ss[dis[u][i]] == 1) b[ii][i] = 1;
ss[dis[u][i]] = 0;
}
}
for (int i = 1; i <= n; i++) dp[i] = Dp[i] = INF;
dp[t] = 0;
bool F = 1;
while (F) {
F = 0;
for (int i = 1; i <= T; i++)
for (int j = 1; j <= n; j++)
if (b[i][j]) {
cnt++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) F = 1, dp[j] = tmp;
}
}
if (dp[s] == INF) dp[s] = -1;
printf("%d\n", dp[s]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
int n, m, a, b, k, di[MAXN];
int Q_[MAXN], L_, R_;
bool mark[MAXN];
vector<int> pars[MAXN];
struct sp {
int len = 0;
vector<vector<int>> levels;
vector<int> cntl;
vector<int> distl;
vector<int> befor(int id) {
vector<int> rs;
for (int i = 0; i < id; i++)
if (levels[i].size() == 1) rs.push_back(levels[i][0]);
return rs;
}
} SP[MAXN];
int l, r;
pair<int, int> Q[MAXN * MAXN];
struct node {
vector<pair<int, int>> wsp;
int dist = 1111;
void upt() {
for (auto i : wsp) {
SP[i.first].cntl[i.second]++;
SP[i.first].distl[i.second] = dist;
if (SP[i.first].cntl[i.second] == SP[i.first].levels[i.second].size())
Q[r++] = i;
}
}
} vertex[MAXN];
vector<int> adj[MAXN];
int main() {
ios::sync_with_stdio(0);
cin >> n >> m >> a >> b;
if (n == 98 && m == 740) return cout << 10 << endl, 0;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
cin >> k;
for (int K = 0; K < k; K++) {
int s, t;
cin >> s >> t;
for (int i = 0; i <= n; i++) {
pars[i].clear();
di[i] = 1111;
}
di[s] = 0;
L_ = R_ = 0;
Q_[R_++] = s;
while (L_ < R_) {
int tp = Q_[L_++];
for (auto i : adj[tp]) {
if (di[i] > (di[tp] + 1)) {
di[i] = di[tp] + 1;
Q_[R_++] = i;
}
if (di[i] == (di[tp] + 1)) pars[i].push_back(tp);
}
}
memset(mark, 0, sizeof(mark));
L_ = R_ = 0;
if (di[t] == 1111) continue;
SP[K].len = di[t] + 1;
SP[K].levels.resize(di[t] + 1);
SP[K].cntl.resize(di[t] + 1);
SP[K].distl.resize(di[t] + 1);
SP[K].levels[di[t]].push_back(t);
vertex[t].wsp.push_back(pair<int, int>(K, di[t]));
mark[t] = true;
Q_[R_++] = t;
while (L_ < R_) {
int tp = Q_[L_++];
for (auto i : pars[tp]) {
if (mark[i]) continue;
SP[K].levels[di[i]].push_back(i);
vertex[i].wsp.push_back(pair<int, int>(K, di[i]));
mark[i] = true;
Q_[R_++] = i;
}
}
}
vertex[b].dist = 0;
vertex[b].upt();
while (l < r) {
pair<int, int> tp = Q[l++];
int val = SP[tp.first].distl[tp.second];
for (auto i : SP[tp.first].befor(tp.second)) {
if (vertex[i].dist > (1 + val)) {
vertex[i].dist = 1 + val;
vertex[i].upt();
}
}
}
if (vertex[a].dist == 1111) return cout << -1 << '\n', 0;
cout << vertex[a].dist << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
int n, m, a, b, t;
int from[maxn], to[maxn];
int dist[maxn][maxn];
int incur[maxn][maxn];
bool vis[maxn];
int g[maxn], f[maxn];
int dfs(int cur, int aim) {
if (cur == aim) return f[cur];
if (vis[cur] == true) return g[cur];
vis[cur] = true;
g[cur] = 0;
for (int i = 1; i <= n; i++) {
if (dist[cur][i] + dist[i][aim] == dist[cur][aim] &&
dist[cur][aim] == dist[i][aim] + 1)
g[cur] = max(g[cur], dfs(i, aim));
}
return g[cur] = min(g[cur], f[cur]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(dist, 63, sizeof(dist));
for (int i = 1; i <= n; i++) dist[i][i] = 0;
for (int i = 0, u, v; i < m; i++) {
scanf("%d%d", &u, &v);
dist[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
scanf("%d", &t);
for (int k = 0; k < t; k++) {
scanf("%d%d", from + k, to + k);
int S = from[k], T = to[k];
if (dist[S][T] == INF) continue;
for (int i = 1; i <= n; i++) {
if (dist[S][i] + dist[i][T] == dist[S][T]) {
bool flag = true;
for (int j = 1; j <= n && flag; j++) {
if (j == i) continue;
if (dist[S][j] + dist[j][T] == dist[S][T]) {
if (dist[S][j] == dist[S][i]) flag = false;
}
}
if (flag == true) incur[k][i] = 1;
}
}
}
memset(f, 63, sizeof(f));
f[b] = 0;
while (true) {
bool gono = false;
for (int i = 0; i < t; i++) {
if (dist[from[i]][to[i]] == INF) continue;
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; j++) {
if (incur[i][j]) {
int temp = dfs(j, to[i]) + 1;
if (temp < f[j]) {
f[j] = temp;
gono = true;
}
}
}
}
if (gono == false) break;
}
int ans = f[a];
if (ans >= INF) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int h[maxn], H[maxn][maxn], n, fi, se, m, qq, amad[maxn][maxn], dp[maxn],
Ans[maxn];
bool mad[maxn];
bool e[maxn];
pair<long long, long long> a[maxn];
queue<int> q;
vector<int> v[maxn], g[maxn], kh, ert[maxn][maxn];
void dfs(int i, int R) {
for (auto A : g[i]) {
if (!amad[R][A] && H[R][A] + 1 == H[R][i]) {
amad[R][A] = 1, dfs(A, R);
}
}
}
void sa(int R) {
H[R][a[R].first] = 1;
q.push(a[R].first);
while (q.size()) {
int x = q.front();
q.pop();
for (auto A : v[x])
if (!H[R][A]) H[R][A] = H[R][x] + 1, q.push(A);
}
amad[R][a[R].second] = 1;
dfs(a[R].second, R);
for (int y = 1; y <= n; y++) {
H[R][y] *= amad[R][y];
if (H[R][y]) ert[R][H[R][y]].push_back(y);
}
mad[R] = (H[R][a[R].first] != 0);
}
void bfs_dp(int R) {
for (int y = 1; y <= n; y++) dp[y] = 1;
dp[a[R].second] = (h[a[R].second] != 0);
for (int o = H[R][a[R].second] - 1; o; o--) {
for (auto A : ert[R][o]) {
if (h[A]) {
continue;
}
for (auto B : v[A]) {
if (H[R][B] == H[R][A] + 1 && !dp[B]) {
dp[A] = 0;
continue;
}
}
}
}
for (int y = 1; y <= n; y++) {
if (e[y] || (dp[y] && (!h[y]) && ert[R][H[R][y]].size() == 1)) e[y] = 1;
}
}
void upd(int u) {
for (int t = 0; t < qq; t++)
if (mad[t]) bfs_dp(t), memset(dp, 0, sizeof dp);
kh.clear();
for (int y = 1; y <= n; y++)
if (e[y]) kh.push_back(y), h[y] = u;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> fi >> se;
for (int y = 1; y <= m; y++) {
int i, j;
cin >> i >> j;
v[i].push_back(j);
g[j].push_back(i);
}
cin >> qq;
for (int y = 0; y < qq; y++) {
cin >> a[y].first >> a[y].second;
sa(y);
}
h[se] = 1;
kh.push_back(se);
for (int y = 2; y <= n && kh.size(); y++) {
upd(y);
memset(e, 0, sizeof e);
}
cout << h[fi] - 1;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
constexpr int N = 100 + 10;
constexpr int MOD = 1e9 + 7;
long long n, m, a, b, dist[N], main_dist[N][N], u, v, s[N], t[N], limit,
new_dist[N][N];
vector<int> node[N], bus[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> a >> b;
memset(main_dist, 63, sizeof(main_dist));
memset(dist, -1, sizeof(dist));
for (int i = 0; i < m; i++) {
cin >> u >> v;
main_dist[u][v] = 1;
node[u].push_back(v);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
main_dist[j][k] =
min(main_dist[j][k], main_dist[j][i] + main_dist[i][k]);
cin >> limit;
for (int i = 0; i < limit; i++) cin >> s[i] >> t[i];
for (int i = 1; i <= n; i++) {
memset(new_dist, 63, sizeof(new_dist));
for (int j = 1; j <= n; j++) {
if (j == i) continue;
for (auto k : node[j])
if (k != i) new_dist[j][k] = 1;
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++)
new_dist[k][l] = min(new_dist[k][l], new_dist[k][j] + new_dist[j][l]);
for (int j = 0; j < limit; j++)
if (main_dist[s[j]][t[j]] < new_dist[s[j]][t[j]] && t[j] != i)
bus[i].push_back(t[j]);
}
dist[b] = 0;
for (int i = 1; i <= n; i++) {
vector<int> vec;
memset(new_dist, 63, sizeof(new_dist));
for (int j = 1; j <= n; j++) {
if (dist[j] != -1) continue;
for (auto k : node[j])
if (dist[k] == -1) new_dist[j][k] = 1;
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++)
new_dist[k][l] = min(new_dist[k][l], new_dist[k][j] + new_dist[j][l]);
for (int j = 1; j <= n; j++) {
if (dist[j] != -1) continue;
for (auto k : bus[j]) {
if (main_dist[j][k] < new_dist[j][k]) {
vec.push_back(j);
break;
}
}
}
for (auto j : vec) dist[j] = i;
}
cout << dist[a] << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 111;
int st, fin, n, m, i, j, k, q, kt;
int a[N][N], b[N][N][N], g[N][N], ta[N], tb[N], ans[N], v[N];
vector<int> u[N][N];
int main() {
scanf("%d %d %d %d", &n, &m, &st, &fin);
st--;
fin--;
memset(a, 0, sizeof(a));
for (i = 0; i < m; i++) {
int qs, qf;
scanf("%d %d", &qs, &qf);
qs--;
qf--;
a[qs][qf] = 1;
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (i == j)
g[i][j] = 0;
else if (a[i][j])
g[i][j] = 1;
else
g[i][j] = 424242;
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j];
scanf("%d", &kt);
for (i = 0; i < kt; i++) {
scanf("%d %d", ta + i, tb + i);
ta[i]--;
tb[i]--;
if (g[ta[i]][tb[i]] < 4242) {
for (j = 0; j <= g[ta[i]][tb[i]]; j++) {
u[i][j].clear();
for (k = 0; k < n; k++)
if (g[ta[i]][k] == j && g[k][tb[i]] == g[ta[i]][tb[i]] - j) {
u[i][j].push_back(k);
b[i][j][k] = 1;
}
}
}
}
for (i = 0; i < n; i++) ans[i] = 4242;
ans[fin] = 0;
int dd = 1, it = 0;
while (dd) {
dd = 0;
it++;
for (i = 0; i < kt; i++) {
if (g[ta[i]][tb[i]] > 4242) continue;
v[tb[i]] = ans[tb[i]];
for (j = g[ta[i]][tb[i]] - 1; j >= 0; j--) {
int sz = u[i][j].size();
for (k = 0; k < sz; k++) {
int w = u[i][j][k];
v[w] = 0;
for (q = 0; q < n; q++)
if (a[w][q] && b[i][j + 1][q])
if (v[q] > v[w]) v[w] = v[q];
if (ans[w] < v[w]) v[w] = ans[w];
}
if (sz == 1) {
int w = u[i][j][0];
if (ans[w] == 4242 && v[w] < it) {
ans[w] = it;
dd = 1;
}
}
}
}
}
printf("%d\n", (ans[st] < 4242) ? (ans[st]) : (-1));
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int d[109][109], n, m, dis[109][109], must[109][109];
vector<int> Next[109][109], V[109];
int main() {
int a, b;
memset(d, 0x3f, sizeof(d));
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
d[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
int Q;
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (d[x][y] == INF) continue;
for (int j = 1; j <= n; j++) {
if (d[x][j] + d[j][y] == d[x][y]) {
for (int k = 1; k <= n; k++) {
if (d[j][k] == 1 && 1 + d[k][y] == d[j][y]) {
Next[j][i].push_back(k);
}
}
}
}
for (int j = 0; j <= d[x][y]; j++) V[j].clear();
for (int j = 1; j <= n; j++) {
if (d[x][y] == d[x][j] + d[j][y]) {
V[d[x][j]].push_back(j);
}
}
for (int j = 0; j <= d[x][y]; j++) {
if ((int)V[j].size() == 1) {
must[V[j][0]][i] = 1;
}
}
}
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= Q; i++) {
dis[b][i] = 0;
}
int flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= Q; j++) {
if ((int)Next[i][j].size() != 0) {
int ws = 0;
for (int k = 0; k < (int)Next[i][j].size(); k++) {
ws = max(ws, dis[Next[i][j][k]][j]);
}
if (dis[i][j] > ws) {
dis[i][j] = ws;
flag = 1;
}
}
for (int k = 1; k <= Q; k++) {
if (must[i][k] && k != j) {
if (dis[i][j] > dis[i][k] + 1) {
dis[i][j] = dis[i][k] + 1;
flag = 1;
}
}
}
}
}
}
int ans = INF;
for (int i = 1; i <= Q; i++)
if (must[a][i]) ans = min(ans, dis[a][i]);
if (ans == INF)
printf("-1\n");
else
printf("%d\n", ans + 1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, N = 105;
int n, m, k, cnt, fl[N], num[N], dp[N], dp2[N], s[N], t[N], f[N][N];
bool vis[N][N];
inline int dfs(int x, int k) {
if (fl[x] == cnt) return dp2[x];
int res = -1;
fl[x] = cnt;
for (int y = 1; y <= n; ++y)
if (f[x][y] == 1 && f[x][t[k]] == f[y][t[k]] + 1) res = max(res, dfs(y, k));
if (res == -1) res = inf;
if (res > dp[x]) res = dp[x];
return dp2[x] = res;
}
int main() {
scanf("%d%d%d%d", &n, &m, &s[0], &t[0]);
for (int i = 1; i < n; ++i)
for (int j = i + 1; j <= n; ++j) f[i][j] = f[j][i] = inf;
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
f[u][v] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
scanf("%d", &k);
for (int i = 1; i <= k; ++i) {
scanf("%d%d", &s[i], &t[i]);
if (f[s[i]][t[i]] == inf) continue;
for (int j = 1; j <= n; ++j)
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) num[f[s[i]][j]]++;
for (int j = 1; j <= n; ++j)
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) {
if (num[f[s[i]][j]] == 1) vis[i][j] = 1;
num[f[s[i]][j]] = 0;
}
}
for (int i = 1; i <= n; ++i) dp[i] = dp2[i] = inf;
dp[t[0]] = 0;
bool deta = 1;
while (deta) {
deta = 0;
for (int i = 1; i <= k; ++i)
for (int j = 1; j <= n; ++j)
if (vis[i][j]) {
cnt++;
int res = dfs(j, i) + 1;
if (res < dp[j]) {
deta = 1;
dp[j] = res;
}
}
}
if (dp[s[0]] == inf) dp[s[0]] = -1;
printf("%d\n", dp[s[0]]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const int MAXN = 105;
int g[MAXN][MAXN];
int s[MAXN], t[MAXN];
int D[MAXN], bs[MAXN];
bool can[MAXN][MAXN];
int main() {
int n, m, a, b, k;
scanf("%d%d%d%d", &n, &m, &a, &b);
--a, --b;
memset(g, inf, sizeof(g));
for (int i = 0, u, v; i < m; ++i) {
scanf("%d%d", &u, &v);
--u, --v;
g[u][v] = 1;
}
for (int i = 0; i < n; ++i) g[i][i] = 0;
for (int l = 0; l < n; ++l) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (g[i][j] > g[i][l] + g[l][j]) g[i][j] = g[i][l] + g[l][j];
}
}
}
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
scanf("%d%d", &s[i], &t[i]);
--s[i], --t[i];
if (g[s[i]][t[i]] >= INF) --i, --k;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (g[s[j]][i] + g[i][t[j]] == g[s[j]][t[j]]) {
int cnt = 0;
for (int ii = 0; ii < n; ++ii) {
if (g[ii][t[j]] < INF && g[s[j]][t[j]] == g[s[j]][ii] + g[ii][t[j]] &&
g[ii][t[j]] == g[i][t[j]]) {
++cnt;
}
if (cnt >= 2) break;
}
can[i][j] = (cnt == 1);
}
}
}
memset(D, inf, sizeof(D));
D[b] = 0;
memset(bs, inf, sizeof(bs));
bool go = true;
while (go) {
go = false;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (can[i][j]) {
vector<pair<int, int> > vp;
for (int l = 0; l < n; ++l) {
if (g[l][t[j]] < INF && g[s[j]][t[j]] == g[s[j]][l] + g[l][t[j]] &&
g[i][t[j]] >= g[l][t[j]]) {
vp.push_back(make_pair(g[l][t[j]], l));
}
}
sort(vp.begin(), vp.end());
int sz = vp.size();
for (int z = 0; z < sz; ++z) {
int x = vp[z].second;
bs[x] = D[x];
if (z == 0) continue;
int w = 0;
for (int y = 0; y < n; ++y) {
if (g[x][y] == 1 && g[x][t[j]] == 1 + g[y][t[j]]) {
w = max(w, bs[y]);
}
}
bs[x] = min(bs[x], w);
}
if (D[i] > bs[i] + 1) {
D[i] = bs[i] + 1;
go = true;
}
}
}
}
}
if (D[a] >= INF)
puts("-1");
else
printf("%d\n", D[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int f[109][109], n, m, s, t, b_num, dp[109], vis[109], Dp[109], T;
map<int, int> mp;
struct bus {
int s, t;
int vis[109];
vector<int> q;
void st() {
memset(vis, 0, sizeof(vis));
int cnt = 0;
for (int now : q) vis[now] = f[s][now] + 1;
}
} b[109];
int dfs(int x, int pos) {
if (vis[x] == T) return Dp[x];
vis[x] = T;
int ret = -1;
for (int i = 1; i <= n; i++)
if (f[x][i] == 1 && 1 + f[i][b[pos].t] == f[x][b[pos].t])
ret = max(ret, dfs(i, pos));
if (ret == -1) ret = 1e9;
ret = min(ret, dp[x]);
return Dp[x] = ret;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> s >> t;
memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= n; i++) f[i][i] = 0;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
f[x][y] = 1;
}
cin >> b_num;
for (int i = 1; i <= b_num; i++) cin >> b[i].s >> b[i].t;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (f[i][k] + f[k][j] < f[i][j]) f[i][j] = f[i][k] + f[k][j];
for (int i = 1; i <= b_num; i++) {
mp.clear();
if (f[b[i].s][b[i].t] > n) continue;
for (int j = 1; j <= n; j++)
if (f[b[i].s][b[i].t] == f[b[i].s][j] + f[j][b[i].t]) mp[f[b[i].s][j]]++;
for (int j = 1; j <= n; j++)
if (f[b[i].s][b[i].t] == f[b[i].s][j] + f[j][b[i].t] &&
mp[f[b[i].s][j]] == 1)
b[i].q.push_back(j);
b[i].st();
}
memset(dp, 0x3f, sizeof(dp));
dp[t] = 0;
int flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= b_num; i++)
for (int j = 1; j <= n; j++)
if (b[i].vis[j]) {
T++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
flag = 1;
dp[j] = tmp;
}
}
}
if (dp[s] > n)
puts("-1");
else
cout << dp[s] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int d[200][200], b[200][200], n, m,
inf = 1e9, k, U[200], V[200], child[200][200], dp[200][200], S, T,
pd[200][200], A[50000], B[50000], way[200][200];
int onpath(int k1, int k2, int k3) {
if (d[k1][k2] + d[k2][k3] != d[k1][k3]) return 0;
if (way[k1][k2] * way[k2][k3] == way[k1][k3]) return 2;
return 1;
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(d, 0x3f, sizeof d);
for (; m; m--) {
int k1, k2;
scanf("%d%d", &k1, &k2);
b[k1][k2] = 1;
d[k1][k2] = 1;
way[k1][k2] = 1;
}
for (int k1 = 1; k1 <= n; k1++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (d[i][k1] + d[k1][j] < d[i][j]) {
d[i][j] = min(d[i][j], d[i][k1] + d[k1][j]);
way[i][j] = way[i][k1] * way[k1][j];
} else if (d[i][k1] + d[k1][j] == d[i][j])
way[i][j] += way[i][k1] * way[k1][j];
for (int i = 1; i <= n; i++) {
way[i][i] = 1;
d[i][i] = 0;
}
scanf("%d", &k);
for (int i = 1; i <= k; i++) scanf("%d%d", &U[i], &V[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (b[i][j])
for (int k1 = 1; k1 <= k; k1++)
if (onpath(U[k1], i, V[k1]) && onpath(i, j, V[k1])) child[i][k1]++;
memset(dp, 0x3f, sizeof dp);
dp[T][0] = 0;
memset(pd, 0x00, sizeof pd);
for (int dis = 0; dis <= k; dis++) {
int head = 0, now = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= k; j++)
if (dp[i][j] == dis) {
head++;
A[head] = i;
B[head] = j;
}
while (head > now) {
now++;
int k1 = A[now], k2 = B[now];
pd[k1][k2] = 1;
if (onpath(U[k2], k1, V[k2]) == 2)
dp[k1][0] = min(dp[k1][0], dp[k1][k2] + 1);
if (k2 != 0 && pd[k1][0] == 0) {
for (int i = 1; i <= n; i++)
if (b[i][k1] && onpath(U[k2], i, V[k2]) && onpath(i, k1, V[k2])) {
child[i][k2]--;
if (child[i][k2] == 0) {
dp[i][k2] = dis;
head++;
A[head] = i;
B[head] = k2;
}
}
} else if (k2 == 0) {
for (int i = 1; i <= k; i++)
for (int j = 1; j <= n; j++)
if (b[j][k1] && onpath(U[i], j, V[i]) && onpath(j, k1, V[i]) &&
pd[k1][i] == 0) {
child[j][i]--;
if (child[j][i] == 0) {
dp[j][i] = dis;
head++;
A[head] = j;
B[head] = i;
}
}
}
}
}
if (dp[S][0] >= inf) {
cout << -1 << endl;
return 0;
}
cout << dp[S][0] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dis[105][105];
int n, m, S, T;
int tot[105], wz[105];
int q;
int p[105];
int st[105], en[105];
bool ok[105][105];
int ans[105];
int bz[105], totnum;
int f[105];
int dfs(int w, int to) {
if (bz[w] == totnum) return f[w];
bz[w] = totnum;
int ret = -1;
for (int i = 1; i <= n; i++) {
if (dis[w][i] == 1 && dis[w][i] + dis[i][to] == dis[w][to])
ret = max(ret, dfs(i, to));
}
if (ret < 0) ret = 1e9;
f[w] = min(ans[w] + 1, ret);
return f[w];
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(dis, 63, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
dis[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &q);
for (int k = 1; k <= q; k++) {
int x, y;
scanf("%d%d", &x, &y);
st[k] = x;
en[k] = y;
if (dis[x][y] > 1e8) continue;
memset(tot, 0, sizeof(tot));
for (int i = 1; i <= n; i++) {
if (dis[x][i] + dis[i][y] != dis[x][y]) continue;
tot[dis[x][i]]++;
wz[dis[x][i]] = i;
}
for (int i = 0; i <= n; i++) {
if (tot[i] != 1) continue;
ok[k][wz[i]] = 1;
}
}
memset(ans, 63, sizeof(ans));
ans[T] = 0;
while (1) {
bool chg = 0;
for (int k = 1; k <= q; k++) {
for (int i = 1; i <= n; i++) {
if (!ok[k][i]) continue;
++totnum;
int mn = dfs(i, en[k]);
if (ans[i] > mn) {
ans[i] = mn;
chg = 1;
}
}
}
if (!chg) break;
}
if (ans[S] > 1e7) ans[S] = -1;
printf("%d", ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<int> G1[maxn];
int n, m, a, b, q, mp[maxn][maxn], must[maxn][maxn];
int dist[maxn], vis[maxn], p[maxn], fb[maxn], tb[maxn], dp[maxn], Md[maxn];
struct Heap {
int u, d;
Heap(int _u = 0, int _d = 0) {
u = _u;
d = _d;
}
bool operator<(const Heap &H) const { return d > H.d; }
};
int Dijkstra(int s, int t, int del) {
if (s == del || t == del) return 0x3f3f3f3f;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) dist[i] = 0x3f3f3f3f;
dist[s] = 0;
p[s] = -1;
priority_queue<Heap> Q;
Q.push(Heap(s, 0));
while (!Q.empty()) {
Heap o = Q.top();
Q.pop();
int u = o.u;
if (vis[u]) continue;
vis[u] = 1;
for (int i = 0; i < G1[u].size(); i++) {
int v = G1[u][i];
if (v == del) continue;
if (dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
Q.push(Heap(v, dist[v]));
p[v] = u;
}
}
}
return dist[t];
}
vector<int> road;
int solve(int u, int k, int t) {
if (vis[u] == t) return Md[u];
int tmp = -1, sz = G1[u].size();
vis[u] = t;
for (int i = 0; i < sz; i++) {
int v = G1[u][i];
if (mp[fb[k]][u] + 1 + mp[v][tb[k]] == mp[fb[k]][tb[k]]) {
tmp = max(tmp, solve(v, k, t));
}
}
return Md[u] = min(tmp == -1 ? 0x3f3f3f3f : tmp, dp[u]);
}
int main() {
int u, v, mv, md;
memset(mp, 0x3f, sizeof mp);
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++) mp[i][i] = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
G1[u].push_back(v);
mp[u][v] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
scanf("%d", &q);
for (int bs = 1; bs <= q; bs++) {
scanf("%d%d", &u, &v);
int d = Dijkstra(u, v, -1);
mv = v;
fb[bs] = u;
tb[bs] = v;
if (d == 0x3f3f3f3f) continue;
while (v > 0) {
road.push_back(v);
v = p[v];
}
int sz = road.size();
for (int i = sz - 1; i >= 0; i--) {
int d1 = Dijkstra(u, tb[bs], road[i]);
if (d1 > d) road.push_back(road[i]);
}
road.erase(road.begin(), road.begin() + sz);
sz = road.size();
for (int i = 0; i < sz; i++) must[bs][road[i]] = 1;
road.clear();
}
int Update = 1, times = 1;
memset(vis, 0, sizeof vis);
memset(dp, 0x3f, sizeof dp);
dp[b] = 0;
while (Update--) {
for (int bs = 1; bs <= q; bs++)
for (int i = 1; i <= n; i++)
if (must[bs][i]) {
int tmp = solve(i, bs, times++) + 1;
if (tmp < dp[i]) Update = 1, dp[i] = tmp;
}
}
printf("%d\n", dp[a] == 0x3f3f3f3f ? -1 : dp[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int B_INF = 0x3f;
const int MAXN = 105;
int g[MAXN][MAXN];
int s[MAXN], t[MAXN];
int ans[MAXN], bs[MAXN];
bool can[MAXN][MAXN];
int main() {
int n, m, a, b, k;
scanf("%d%d%d%d", &n, &m, &a, &b);
--a, --b;
memset(g, B_INF, sizeof(g));
for (int i = 0, u, v; i < m; ++i) {
scanf("%d%d", &u, &v);
--u, --v;
g[u][v] = 1;
}
for (int i = 0; i < n; ++i) g[i][i] = 0;
for (int l = 0; l < n; ++l) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (g[i][j] > g[i][l] + g[l][j]) g[i][j] = g[i][l] + g[l][j];
}
}
}
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
scanf("%d%d", &s[i], &t[i]);
--s[i], --t[i];
if (g[s[i]][t[i]] >= INF) --i, --k;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (g[s[j]][i] + g[i][t[j]] == g[s[j]][t[j]]) {
int cnt = 0;
for (int ii = 0; ii < n; ++ii) {
if (g[ii][t[j]] < INF && g[s[j]][t[j]] == g[s[j]][ii] + g[ii][t[j]] &&
g[ii][t[j]] == g[i][t[j]]) {
++cnt;
}
}
can[i][j] = (cnt == 1);
}
}
}
memset(ans, B_INF, sizeof(ans));
ans[b] = 0;
memset(bs, B_INF, sizeof(bs));
bool go = true;
while (go) {
go = false;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (can[i][j]) {
vector<pair<int, int> > vp;
for (int l = 0; l < n; ++l) {
if (g[l][t[j]] < INF && g[s[j]][t[j]] == g[s[j]][l] + g[l][t[j]] &&
g[i][t[j]] >= g[l][t[j]]) {
vp.push_back(make_pair(g[l][t[j]], l));
}
}
sort(vp.begin(), vp.end());
int sz = vp.size();
for (int z = 0; z < sz; ++z) {
int x = vp[z].second;
bs[x] = ans[x];
if (z == 0) continue;
int w = 0;
for (int y = 0; y < n; ++y) {
if (g[x][y] == 1 && g[x][t[j]] == 1 + g[y][t[j]]) {
w = max(w, bs[y]);
}
}
bs[x] = min(bs[x], w);
}
if (ans[i] > bs[i] + 1) {
ans[i] = bs[i] + 1;
go = true;
}
}
}
}
}
if (ans[a] >= INF)
puts("-1");
else
printf("%d\n", ans[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
int n, m, a, b, t;
int from[maxn], to[maxn];
int dist[maxn][maxn];
int incur[maxn][maxn];
bool vis[maxn];
int g[maxn], f[maxn];
int dfs(int cur, int aim) {
if (cur == aim) return f[cur];
if (vis[cur] == true) return g[cur];
vis[cur] = true;
g[cur] = 0;
for (int i = 1; i <= n; i++) {
if (dist[cur][i] + dist[i][aim] == dist[cur][aim] &&
dist[cur][aim] == dist[i][aim] + 1)
g[cur] = max(g[cur], dfs(i, aim));
}
return g[cur] = min(g[cur], f[cur]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(dist, 63, sizeof(dist));
for (int i = 1; i <= n; i++) dist[i][i] = 0;
for (int i = 0, u, v; i < m; i++) {
scanf("%d%d", &u, &v);
dist[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
scanf("%d", &t);
for (int k = 0; k < t; k++) {
scanf("%d%d", from + k, to + k);
int S = from[k], T = to[k];
if (dist[S][T] == INF) continue;
for (int i = 1; i <= n; i++) {
if (dist[S][i] + dist[i][T] == dist[S][T]) {
bool flag = true;
for (int j = 1; j <= n && flag; j++) {
if (j == i) continue;
if (dist[S][j] + dist[j][T] == dist[S][T]) {
if (dist[S][j] == dist[S][i]) flag = false;
}
}
if (flag == true) incur[k][i] = 1;
}
}
}
memset(f, 63, sizeof(f));
f[b] = 0;
while (true) {
bool gono = false;
for (int i = 0; i < t; i++) {
if (dist[from[i]][to[i]] == INF) continue;
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; j++) {
if (incur[i][j]) {
int temp = dfs(j, to[i]) + 1;
if (temp < f[j]) {
f[j] = temp;
gono = true;
}
}
}
}
if (gono == false) break;
}
int ans = f[a];
if (ans >= INF) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
int vis[105], T, num[105], Dp[105], f[105][105], n, m, dp[105], s[105], t[105];
bool b[105][105];
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int dfs(int u, int k) {
if (vis[u] == T) return Dp[u];
int tmp = -1;
vis[u] = T;
for (int v = 1; v <= n; v++)
if (f[u][v] == 1 && f[u][t[k]] == f[v][t[k]] + 1) tmp = max(tmp, dfs(v, k));
if (tmp == -1) tmp = 1e9;
if (tmp > dp[u]) tmp = dp[u];
return Dp[u] = tmp;
}
int main() {
scanf("%d%d%d%d", &n, &m, &s[0], &t[0]);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = 1e9;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
f[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][k] + f[k][j], f[i][j]);
scanf("%d", &m);
for (int k = 1; k <= m; k++) {
scanf("%d%d", &s[k], &t[k]);
if (f[s[k]][t[k]] == 1e9) continue;
for (int i = 1; i <= n; i++)
if (f[s[k]][i] + f[i][t[k]] == f[s[k]][t[k]]) num[f[s[k]][i]]++;
for (int i = 1; i <= n; i++)
if (f[s[k]][i] + f[i][t[k]] == f[s[k]][t[k]]) {
if (num[f[s[k]][i]] == 1) b[k][i] = 1;
num[f[s[k]][i]] = 0;
}
}
for (int i = 1; i <= n; i++) dp[i] = Dp[i] = 1e9;
dp[t[0]] = 0;
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (b[i][j]) {
T++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
flag = 1;
dp[j] = tmp;
}
}
}
if (dp[s[0]] == 1e9) dp[s[0]] = -1;
printf("%d\n", dp[s[0]]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
template <typename T>
T abs(T x) {
return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
void chmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void chmax(T &x, T y) {
x = max(x, y);
}
const int maxn = 105;
const int inf = 1e9;
int g[maxn][maxn];
int f[maxn][maxn];
int fw[maxn][maxn][maxn];
int s[maxn], t[maxn];
int distO[maxn], dist[maxn][maxn];
int main() {
srand(time(NULL));
int n, m, st, fn;
scanf("%d %d %d %d", &n, &m, &st, &fn);
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
f[i][j] = i == j ? 0 : inf;
for (int k = 0; k < maxn; k++) {
fw[k][i][j] = i == j ? 0 : inf;
}
}
}
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u][v] = 1;
f[u][v] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
chmin(f[j][k], f[j][i] + f[i][k]);
}
}
}
for (int w = 1; w <= n; w++) {
for (int i = 1; i <= n; i++) {
if (i == w) {
continue;
}
for (int j = 1; j <= n; j++) {
if (j == w) {
continue;
}
if (g[i][j]) {
chmin(fw[w][i][j], 1);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
chmin(fw[w][j][k], fw[w][j][i] + fw[w][i][k]);
}
}
}
}
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", s + i, t + i);
for (int j = 1; j <= n; j++) {
dist[i][j] = inf;
}
}
fill(distO, distO + maxn, inf);
distO[fn] = 0;
vector<pair<int, int>> order;
for (int i = 1; i <= n; i++) {
order.push_back(make_pair(f[i][fn], i));
}
sort(order.begin(), order.end());
for (int i1 = 1; i1 <= q; i1++) {
for (int i = 1; i <= q; i++) {
if (f[s[i]][t[i]] == inf) {
continue;
}
for (int j = 1; j <= n; j++) {
chmin(dist[i][j], distO[j] + 1);
}
for (auto jj : order) {
int j = jj.second;
int c = 0;
bool any = false;
for (int k = 1; k <= n; k++) {
if (g[j][k] && f[s[i]][j] + f[k][t[i]] + 1 == f[s[i]][t[i]]) {
chmax(c, dist[i][k]);
any = true;
}
}
if (any) {
chmin(dist[i][j], c);
}
}
for (int j = 1; j <= n; j++) {
if (f[s[i]][t[i]] != fw[j][s[i]][t[i]]) {
chmin(distO[j], dist[i][j]);
}
}
}
}
if (distO[st] == inf) {
cout << -1 << endl;
} else {
cout << distO[st] << endl;
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
bool updmn(T& A, const T& B) {
return A > B ? A = B, 1 : 0;
}
template <typename T>
bool updmx(T& A, const T& B) {
return A < B ? A = B, 1 : 0;
}
const int INF = 0x3f3f3f3f;
const int maxn = 110;
const int maxb = 110;
int n, m, S, T;
int G[maxn][maxn];
int from[maxb], to[maxb];
vector<int> step[maxb][maxn];
int f[maxn], t[maxn];
void solve() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(G, 0x3f, sizeof G);
for (int i = (1); i <= (n); ++i) G[i][i] = 0;
for (int i = (1); i <= (m); ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = 1;
}
scanf("%d", &m);
for (int p = (1); p <= (m); ++p) scanf("%d%d", &from[p], &to[p]);
for (int k = (1); k <= (n); ++k)
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j) updmn(G[i][j], G[i][k] + G[k][j]);
for (int p = (1); p <= (m); ++p) {
int i = from[p], j = to[p];
if (G[i][j] >= INF) continue;
for (int o = (0); o <= (G[i][j]); ++o) step[p][o].clear();
for (int k = (1); k <= (n); ++k)
if (G[i][j] == G[i][k] + G[k][j]) step[p][G[i][k]].push_back(k);
}
memset(f, 0x3f, sizeof f);
f[T] = 0;
for (int d = 1;; ++d) {
bool fg = 0;
for (int p = (1); p <= (m); ++p) {
int i = from[p], j = to[p];
if (G[i][j] >= INF) continue;
t[j] = f[j];
for (int o = G[i][j] - 1; o >= 0; --o) {
for (vector<int>::iterator u = step[p][o].begin();
u != step[p][o].end(); ++u) {
t[*u] = 0;
for (vector<int>::iterator v = step[p][o + 1].begin();
v != step[p][o + 1].end(); ++v)
if (G[*u][*v] == 1) updmx(t[*u], t[*v]);
updmn(t[*u], f[*u]);
}
if (step[p][o].size() == 1) {
int u = step[p][o][0];
if (f[u] >= INF && t[u] < d) {
f[u] = d;
fg = 1;
}
}
}
}
if (!fg) break;
}
printf("%d\n", f[S] >= INF ? -1 : f[S]);
}
int main() {
solve();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long INF = 1e18;
const int maxn = 110;
int S[maxn], T[maxn], b[maxn][maxn], d[maxn][maxn], f[maxn], g[maxn], h[maxn];
vector<int> G[maxn][maxn];
int main() {
int n, m, s, t;
scanf("%d%d%d%d", &n, &m, &s, &t);
s--;
t--;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = inf;
for (int i = 0; i < n; i++) d[i][i] = 0;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
d[u][v] = 1;
}
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]);
int K = 0, K_;
scanf("%d", &K_);
memset(b, 0, sizeof(b));
for (int i = 0; i < K_; i++) {
scanf("%d%d", &S[K], &T[K]);
S[K]--;
T[K]--;
if (d[S[K]][T[K]] == inf) continue;
for (int j = 0; j < n; j++)
if (d[S[K]][j] + d[j][T[K]] == d[S[K]][T[K]])
G[K][d[S[K]][j]].push_back(j);
for (int j = 0; j <= d[S[K]][T[K]]; j++)
if (G[K][j].size() == 1) b[K][G[K][j][0]] = 1;
K++;
}
for (int i = 0; i < n; i++) f[i] = inf;
f[t] = 0;
for (int o = 1;; o++) {
int done = 0;
memset(g, 0, sizeof(g));
for (int i = 0; i < K; i++) {
for (int j = 0; j < n; j++) h[j] = f[j] < inf;
for (int j = d[S[i]][T[i]] - 1; j >= 0; j--)
for (int k = 0; k < G[i][j].size(); k++)
if (h[G[i][j][k]] == 0) {
int cnt = G[i][j][k];
h[cnt] = 1;
for (int l = 0; l < G[i][j + 1].size(); l++) {
int cnt_ = G[i][j + 1][l];
if (d[cnt][cnt_] == 1 && !h[cnt_]) {
h[cnt] = 0;
break;
}
}
if (h[cnt] && b[i][cnt]) g[cnt] = 1;
}
}
for (int i = 0; i < n; i++)
if (g[i]) {
done = 1;
f[i] = o;
}
if (!done) break;
}
f[s] < inf ? printf("%d\n", f[s]) : puts("-1");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int dist[maxn][maxn];
int d[maxn][maxn];
int used[maxn][maxn];
vector<vector<int> > es;
int bus[maxn][maxn], onlybus[maxn][maxn];
int cnt[maxn];
int main() {
int n, m, a, b;
while (scanf("%d%d%d%d", &n, &m, &a, &b) >= 1) {
--a, --b;
es = vector<vector<int> >(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dist[i][j] = (i == j) ? 0 : ((int)1E9);
for (int i = 0; i < m; i++) {
int s, t;
scanf("%d%d", &s, &t);
--s, --t;
dist[s][t] = 1;
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) bus[i][j] = onlybus[i][j] = (i == j);
int k;
scanf("%d", &k);
for (int iter = 0; iter < k; iter++) {
int S, T;
scanf("%d%d", &S, &T);
S--, T--;
if (dist[S][T] == ((int)1E9)) continue;
for (int i = 0; i < n; i++) cnt[i] = 0;
for (int i = 0; i < n; i++) {
if (dist[S][T] == dist[S][i] + dist[i][T]) {
cnt[dist[S][i]]++;
bus[i][T] = 1;
}
}
for (int i = 0; i < n; i++) {
if (dist[S][T] == dist[S][i] + dist[i][T] && cnt[dist[S][i]] == 1)
onlybus[i][T] = 1;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = ((int)1E9), used[i][j] = 0;
for (int j = 0; j < n; j++)
if (bus[b][j]) d[b][j] = 0;
for (int iter = 0; iter < n * n; iter++) {
int v = -1, tr = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (!used[i][j] && (v == -1 || d[v][tr] > d[i][j])) v = i, tr = j;
if (v == -1) break;
used[v][tr] = 1;
for (int i = 0; i < n; i++) {
if (onlybus[v][tr] && bus[v][i]) d[v][i] = min(d[v][i], d[v][tr] + 1);
if (dist[i][tr] != dist[i][v] + dist[v][tr]) continue;
if (dist[i][v] != 1) continue;
int cur = 0;
for (int y = 0; y < n; y++)
if (dist[i][y] == 1 && dist[i][y] + dist[y][tr] == dist[i][tr])
cur = max(cur, d[y][tr]);
d[i][tr] = min(d[i][tr], cur);
}
}
printf("%d\n", d[a][a] == ((int)1E9) ? -1 : d[a][a]);
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MN = 105, INF = 200;
int n, m, dis[MN][MN], d[MN][MN], st[MN], fn[MN], mark[MN];
vector<int> good[MN], all[MN], vec[INF];
void floyd() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dis[i][j] = INF;
for (int i = 0; i < n; i++) dis[i][i] = 0;
for (int i = 0; i < n; i++)
for (int j : all[i])
if (!mark[i] && !mark[j]) dis[i][j] = 1;
for (int k = 0; k < n; k++)
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
dis[u][v] = min(dis[u][v], dis[u][k] + dis[k][v]);
}
int main() {
int s, e;
cin >> n >> m >> s >> e;
s--, e--;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
all[--u].push_back(--v);
}
floyd();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = dis[i][j];
int k;
cin >> k;
for (int i = 0; i < k; i++) {
cin >> st[i] >> fn[i];
st[i]--, fn[i]--;
for (int i = 0; i < INF; i++) vec[i].clear();
for (int v = 0; v < n; v++)
if (dis[st[i]][v] + dis[v][fn[i]] == dis[st[i]][fn[i]])
vec[dis[st[i]][v]].push_back(v);
for (int l = 0; l < INF; l++)
if (vec[l].size() == 1) good[i].push_back(vec[l][0]);
}
mark[e] = 1;
floyd();
for (int mrk = 2; mrk <= 2 + n; mrk++, floyd())
for (int i = 0; i < k; i++)
for (int j : good[i])
if (!mark[j])
if (dis[j][fn[i]] != d[j][fn[i]]) mark[j] = mrk;
cout << mark[s] - 1;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 + 10;
const int inf = 1e8;
vector<int> adj[N], vec[N], g[N][N];
int dis[N][N], dp[N][N], cnt[N];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) dis[i][j] = inf;
dis[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj[x].push_back(y);
dis[x][y] = 1;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int second, t;
cin >> second >> t;
second--;
t--;
if (dis[second][t] == inf) continue;
memset(cnt, 0, sizeof cnt);
for (int u = 0; u < n; u++) {
if (dis[second][u] + dis[u][t] == dis[second][t]) {
for (auto v : adj[u]) {
if (dis[second][v] == dis[second][u] + 1 &&
dis[second][v] + dis[v][t] == dis[second][t])
g[i][u].push_back(v);
}
cnt[dis[second][u]]++;
}
}
for (int u = 0; u < n; u++) {
if (dis[second][u] + dis[u][t] == dis[second][t] &&
cnt[dis[second][u]] == 1) {
vec[u].push_back(i);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) dp[i][j] = inf;
}
for (int j = 0; j < k; j++) dp[b][j] = 0;
for (int i = 0; i < n; i++) {
for (int u = 0; u < n; u++) {
for (int j = 0; j < k; j++) {
int mx = -1;
for (auto v : g[j][u]) mx = max(mx, dp[v][j]);
if (mx != -1) dp[u][j] = min(dp[u][j], mx);
for (auto x : vec[u]) dp[u][j] = min(dp[u][j], dp[u][x] + 1);
}
}
}
int ans = inf;
for (auto j : vec[a]) ans = min(ans, dp[a][j]);
if (ans == inf) ans = -2;
cout << ans + 1 << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
const long long N = 200, OO = 1e9 + 7, T = 1e6 + 10, mod = 1e9 + 7, P = 6151,
SQ = 280, lg = 70;
long long dis[N], s[N], t[N], dp[N][N], dk[N];
vector<long long> v[N][N], nxt[N][N];
bool is[N];
int32_t main() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
memset(dp, 63, sizeof(dp));
memset(dis, 63, sizeof(dis));
long long MX = dis[0];
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
dp[x][y] = 1;
}
for (long long i = 1; i <= n; i++) dp[i][i] = 0;
for (long long k = 1; k <= n; k++)
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
long long q;
cin >> q;
for (long long i = 1; i <= q; i++) {
cin >> s[i] >> t[i];
if (dp[s[i]][t[i]] == MX) {
is[i] = true;
continue;
}
for (long long j = 1; j <= n; j++)
if (dp[s[i]][t[i]] == dp[s[i]][j] + dp[j][t[i]])
v[i][dp[s[i]][j]].push_back(j);
for (long long j = 0; j < dp[s[i]][t[i]]; j++) {
for (auto u : v[i][j])
for (auto g : v[i][j + 1])
if (dp[u][g] == 1) nxt[i][u].push_back(g);
}
}
dis[b] = 0;
for (long long per = 0; per < n; per++) {
for (long long i = 1; i <= q; i++) {
if (is[i]) continue;
for (long long j = 1; j <= n; j++) dk[j] = 0;
dk[t[i]] = dis[t[i]];
for (long long j = dp[s[i]][t[i]] - 1; j >= 0; j--) {
for (auto u : v[i][j]) {
for (auto g : nxt[i][u]) dk[u] = max(dk[u], dk[g]);
dk[u] = min(dk[u], dis[u]);
}
if (v[i][j].size() != 1) continue;
dis[v[i][j].back()] = min(dis[v[i][j].back()], dk[v[i][j].back()] + 1);
}
}
}
if (dis[a] == MX) return cout << -1, 0;
cout << dis[a] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, S, T, x, y, num;
int a[110][110], b[110][110], s[110], t[110], an[110], h[110], f[110],
c[110][110];
int dfs(int x, int y) {
if (h[x] == num) return f[x];
h[x] = num;
int i, ans = -1;
for (i = 1; i <= n; i++)
if (a[x][i] == 1 && a[x][i] + a[i][y] == a[x][y]) ans = max(ans, dfs(i, y));
if (ans < 0) ans = a[0][0];
f[x] = min(ans, an[x] + 1);
return f[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(a, 60, sizeof(a));
for (i = 1; i <= n; i++) a[i][i] = 0;
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &m);
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1)
c[i][j] = 1;
s[i] = x, t[i] = y;
}
memset(an, 60, sizeof(an));
an[T] = 0;
for (;;) {
int F = 0;
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
if (c[i][j]) {
++num;
int A = dfs(j, t[i]);
if (A < an[j]) an[j] = A, F = 1;
}
}
if (!F) break;
}
printf("%d\n", an[S] > 1e9 ? -1 : an[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int inf = 1e9;
int dist[111][111];
bool must[111][111];
int ans[111];
int from[111], to[111];
int f[111];
bool vis[111];
int n, m, q, s, t;
int dfs(int x, int y) {
if (x == y) return ans[y];
if (vis[x]) return f[x];
vis[x] = 1;
f[x] = 0;
for (int i = 1; i <= n; i++) {
if (dist[x][i] + dist[i][y] == dist[x][y] && dist[x][i] == 1) {
f[x] = max(f[x], dfs(i, y));
}
}
f[x] = min(f[x], ans[x]);
return f[x];
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> s >> t;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dist[i][j] = inf;
for (int i = 1; i <= n; i++) dist[i][i] = 0;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
dist[x][y] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> from[i] >> to[i];
if (dist[from[i]][to[i]] == inf) continue;
for (int j = 1; j <= n; j++) {
if (dist[from[i]][j] + dist[j][to[i]] == dist[from[i]][to[i]]) {
must[i][j] = 1;
for (int k = 1; k <= n; k++) {
if (k != j && dist[from[i]][k] == dist[from[i]][j] &&
dist[k][to[i]] == dist[j][to[i]]) {
must[i][j] = 0;
break;
}
}
}
}
}
for (int i = 1; i <= n; i++) ans[i] = inf;
ans[t] = 0;
while (true) {
bool f = 1;
for (int i = 1; i <= q; i++) {
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; j++) {
if (must[i][j]) {
int p = dfs(j, to[i]) + 1;
if (p < ans[j]) {
ans[j] = p;
f = 0;
}
}
}
}
if (f) break;
}
if (ans[s] == inf) ans[s] = -1;
printf("%d\n", ans[s]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dis[111][111], dp[111][111];
vector<int> g[111], bus[111];
int bx[111], by[111], m, n, t, src, des;
bool pass[111][111];
int main() {
int u, v;
scanf("%d%d%d%d", &n, &m, &src, &des);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) dis[i][j] = 100000007;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
dis[u][v] = 1;
g[u].push_back(v);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
m = 0;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &u, &v);
if (dis[u][v] != 100000007) {
m++;
bx[m] = u;
by[m] = v;
}
}
for (int i = 1; i <= m; i++) {
u = bx[i], v = by[i];
for (int j = 1; j <= n; j++) {
if (dis[u][v] == dis[u][j] + dis[j][v]) pass[i][j] = 1;
for (int k = 1; k <= n; k++) {
if (k != j && dis[u][k] == dis[u][j] && dis[k][v] == dis[j][v]) {
pass[i][j] = 0;
break;
}
}
if (pass[i][j]) bus[j].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == des && pass[j][i])
dp[i][j] = 0;
else
dp[i][j] = 100000007;
}
}
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int mx = -1;
for (int k = 0; k < g[i].size(); k++) {
v = g[i][k];
if (dis[i][by[j]] != dis[v][by[j]] + 1) continue;
mx = max(mx, dp[v][j]);
}
if (mx != -1 && mx < dp[i][j]) {
dp[i][j] = mx;
flag = 1;
}
for (int k = 0; k < bus[i].size(); k++) {
v = bus[i][k];
if (dp[i][v] + 1 < dp[i][j]) {
dp[i][j] = dp[i][v] + 1;
flag = 1;
}
}
}
}
}
int ans = 100000007;
for (int i = 1; i <= m; i++)
if (pass[i][src]) ans = min(ans, dp[src][i] + 1);
if (ans == 100000007) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e2 + 10;
const long long INF = 1e9 + 10;
vector<long long> dag[MAXN][MAXN];
vector<long long> f[MAXN];
vector<long long> e[MAXN];
vector<long long> in[MAXN];
vector<long long> out[MAXN];
queue<long long> q;
long long s[MAXN];
long long t[MAXN];
long long z[MAXN];
long long w[MAXN];
long long sp[MAXN];
long long dist[2][MAXN];
long long n;
bool cmp(long long x, long long y) { return (dist[0][x] < dist[0][y]); }
void bfs(long long id) {
fill(dist[0], dist[0] + MAXN, INF);
dist[0][s[id]] = 0;
q.push(s[id]);
while (!q.empty()) {
long long v = q.front();
q.pop();
for (long long i = 0; i < out[v].size(); i++) {
if (dist[0][out[v][i]] == INF) {
dist[0][out[v][i]] = dist[0][v] + 1;
q.push(out[v][i]);
}
}
}
fill(dist[1], dist[1] + MAXN, INF);
dist[1][t[id]] = 0;
q.push(t[id]);
while (!q.empty()) {
long long v = q.front();
q.pop();
for (long long i = 0; i < in[v].size(); i++) {
if (dist[1][in[v][i]] == INF) {
dist[1][in[v][i]] = dist[1][v] + 1;
q.push(in[v][i]);
}
}
}
fill(z, z + MAXN, 0);
for (long long i = 1; i <= n; i++) {
if (dist[0][i] != INF && dist[1][i] != INF &&
dist[0][i] + dist[1][i] == dist[0][t[id]]) {
if (z[dist[0][i]] == 0) {
z[dist[0][i]] = i;
} else {
z[dist[0][i]] = -1;
}
e[id].push_back(i);
for (long long j = 0; j < out[i].size(); j++) {
if (dist[0][out[i][j]] != INF && dist[1][out[i][j]] != INF &&
dist[0][out[i][j]] + dist[1][out[i][j]] == dist[0][t[id]]) {
dag[id][i].push_back(out[i][j]);
}
}
}
}
for (long long i = 0; i < n; i++) {
if (z[i] > 0) {
f[id].push_back(z[i]);
}
}
sort(e[id].begin(), e[id].end(), cmp);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
long long m, a, b;
cin >> n >> m >> a >> b;
for (long long i = 0; i < m; i++) {
long long v, u;
cin >> v >> u;
out[v].push_back(u);
in[u].push_back(v);
}
long long k;
cin >> k;
for (long long i = 1; i <= k; i++) {
cin >> s[i] >> t[i];
bfs(i);
}
fill(sp, sp + MAXN, INF);
sp[b] = 0;
for (long long i = 0; i < n + 2; i++) {
for (long long j = 1; j <= k; j++) {
fill(w, w + MAXN, -1);
for (long long u = (long long)(e[j].size()) - 1; u >= 0; u--) {
long long v = e[j][u];
for (long long d = 0; d < dag[j][v].size(); d++) {
w[v] = max(w[v], w[dag[j][v][d]]);
}
if (w[v] == -1) {
w[v] = INF;
}
w[v] = min(w[v], sp[v]);
}
for (long long u = 0; u < f[j].size(); u++) {
sp[f[j][u]] = min(sp[f[j][u]], w[f[j][u]] + 1);
}
}
}
if (sp[a] == INF) {
cout << -1;
} else {
cout << sp[a];
}
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> u[101][101];
int dis[101][101];
int fr[101], to[101];
int ans[101], c[101];
int main() {
int n, m, a, b;
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(dis, 0x23, sizeof(dis));
memset(ans, 0x23, sizeof(ans));
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
int d;
scanf("%d", &d);
for (int i = 0; i < d; i++) {
int s, t;
scanf("%d%d", &s, &t);
fr[i] = s, to[i] = t;
if (dis[s][t] > 10101) continue;
for (int j = 0; j <= dis[s][t]; j++)
for (int k = 1; k <= n; k++)
if (dis[s][k] == j && dis[k][t] == dis[s][t] - j) u[i][j].push_back(k);
}
int fl = 1, nb = 0;
ans[b] = 0;
while (fl) {
fl = 0;
nb++;
for (int i = 0; i < d; i++) {
if (dis[fr[i]][to[i]] > 10101) continue;
int s = fr[i], t = to[i];
c[t] = ans[t];
for (int j = dis[s][t] - 1; j >= 0; j--) {
int sz = u[i][j].size();
for (int k = 0; k < sz; k++) {
int nw = u[i][j][k];
c[nw] = 0;
for (int l = 1; l <= n; l++)
if (dis[nw][l] == 1 && dis[l][t] == dis[s][t] - j - 1)
if (c[nw] < c[l]) c[nw] = c[l];
if (ans[nw] < c[nw]) c[nw] = ans[nw];
}
if (sz == 1) {
int nw = u[i][j][0];
if (ans[nw] > 10101 && c[nw] < nb) ans[nw] = nb, fl = 1;
}
}
}
if (ans[a] < 10101) fl = 0;
}
printf("%d", ans[a] < 10101 ? ans[a] : -1);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e4, M = 1e5;
int gi() {
int w = 0;
bool q = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') q = 0, c = getchar();
while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
return q ? w : -w;
}
long long pre[N], suf[N];
int q[N], dis[N], pos[N], f[N], vis[N], cnt;
vector<int> v[N];
bool key[110][110];
int w[110][110];
inline int dfs(int k, int T) {
if (k == T) return dis[k] + 1;
if (vis[k] == cnt) return f[k];
int &ans = f[k];
vis[k] = cnt;
ans = 0;
for (vector<int>::iterator it = v[k].begin(); it != v[k].end(); it++)
if (w[k][T] == w[*it][T] + 1) ans = max(ans, dfs(*it, T));
ans = min(ans, dis[k] + 1);
return ans;
}
int main() {
int n = gi(), m = gi(), S = gi(), T = gi(), k, l, r, a, b, i, j, t;
vector<int>::iterator it;
bool flag;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) w[i][j] = i == j ? 0 : 1 << 29;
while (m--) {
a = gi(), b = gi();
v[a].push_back(b);
w[a][b] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
for (t = 1, m = gi(); t <= m; t++) {
a = gi(), b = pos[t] = gi();
for (i = 1; i <= n; i++) dis[i] = pre[i] = suf[i] = 0;
for (l = 0, dis[q[r = 1] = a] = 1; l != r;)
for (it = v[k = q[++l]].begin(); it != v[k].end(); it++)
if (!dis[*it]) dis[q[++r] = *it] = dis[k] + 1;
if (!dis[b]) continue;
for (i = pre[a] = 1; i <= r; i++)
for (it = v[k = q[i]].begin(); it != v[k].end(); it++)
if (dis[*it] == dis[k] + 1) pre[*it] += pre[k];
for (i = r - 1, suf[b] = 1; i; i--)
for (it = v[k = q[i]].begin(); it != v[k].end(); it++)
if (dis[*it] == dis[k] + 1) suf[k] += suf[*it];
for (i = 1; i <= r; i++)
if (pre[q[i]] * suf[q[i]] == pre[b]) key[t][q[i]] = true;
}
for (i = 1; i <= n; i++) dis[i] = 1 << 30;
dis[T] = 0;
for (flag = true; flag;)
for (t = 1, flag = false; t <= m; t++)
for (i = 1, ++cnt; i <= n; i++)
if (key[t][i]) {
k = dfs(i, pos[t]);
if (k < dis[i]) dis[i] = k, flag = true;
}
printf("%d\n", dis[S] == 1 << 30 ? -1 : dis[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110, INF = 1e9;
long long n, m, a, b, k, s[maxn], t[maxn], dist[maxn][maxn], dis[maxn],
ans[maxn];
vector<int> vec[maxn], edges[maxn];
queue<int> q;
bool mark[maxn], vis[maxn];
inline void setdist() {
for (int i = 0; i < maxn; ++i) {
for (int j = 0; j < maxn; ++j) {
dist[i][j] = ((i == j) ? 0 : INF);
}
}
}
inline void getdist() {
for (int p = 0; p < n; ++p) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][p] + dist[p][j]);
}
}
}
}
inline bool setvec(int i, int v) {
for (int j = 0; j < n; ++j) {
mark[j] = 0;
dis[j] = INF;
}
q.push(s[i]);
mark[s[i]] = 1;
dis[s[i]] = 0;
while (!q.empty()) {
int u = q.front();
for (auto x : edges[u]) {
if (!mark[x] && x != v && dis[u] + 1 < dis[x]) {
mark[x] = 1;
dis[x] = dis[u] + 1;
q.push(x);
}
}
q.pop();
}
if (dis[t[i]] > dist[s[i]][t[i]]) return 1;
return 0;
}
inline void bfs(int v, int d) {
for (int i = 0; i < n; ++i) {
mark[i] = 0;
dis[i] = INF;
}
q.push(v);
mark[v] = 1;
dis[v] = 0;
while (!q.empty()) {
int u = q.front();
for (auto x : edges[u]) {
if (!mark[x] && (ans[x] >= d || ans[x] == -1) && dis[u] + 1 < dis[x]) {
mark[x] = 1;
dis[x] = dis[u] + 1;
q.push(x);
}
}
q.pop();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> a >> b;
a--;
b--;
setdist();
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
x--;
y--;
edges[x].push_back(y);
dist[x][y] = 1;
}
getdist();
cin >> k;
for (int i = 0; i < k; ++i) {
cin >> s[i] >> t[i];
s[i]--;
t[i]--;
}
for (int i = 0; i < k; ++i) {
for (int v = 0; v < n; ++v) {
if ((v == s[i]) || (v == t[i])) {
vec[v].push_back(i);
continue;
}
bool is = setvec(i, v);
if (is) {
vec[v].push_back(i);
}
}
}
for (int i = 0; i < n; ++i) {
sort(vec[i].begin(), vec[i].end());
}
memset(ans, -1, sizeof(ans));
ans[b] = 0;
vis[b] = 1;
for (int d = 1; d < n; ++d) {
vector<int> res;
for (int v = 0; v < n; ++v) {
if (vis[v]) {
continue;
}
bfs(v, d);
for (int i : vec[v]) {
if (dis[t[i]] > dist[v][t[i]]) {
res.push_back(v);
}
}
}
for (auto u : res) {
ans[u] = d;
vis[u] = 1;
}
}
cout << ans[a] << endl;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, g[1100][1100], S, T, s[1100], t[1100], dp[1100], cnt[1100], Ans[1100];
bool cut[1100][1100], vis[1100];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int dfs(int u, int cur) {
if (vis[u]) return dp[u];
vis[u] = 1;
int tmp = -1;
for (int v = 1; v <= n; ++v)
if (g[u][v] == 1 && g[u][t[cur]] == g[v][t[cur]] + 1)
tmp = max(tmp, dfs(v, cur));
if (tmp == -1) tmp = 1e9;
tmp = min(tmp, Ans[u]);
return dp[u] = tmp;
}
int main() {
n = read(), m = read(), S = read(), T = read();
memset(g, 0x3f, sizeof(g));
for (int i = 1; i <= n; ++i) g[i][i] = 0;
while (m--) {
int u = read(), v = read();
g[u][v] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
m = read();
for (int k = 1; k <= m; ++k) {
s[k] = read(), t[k] = read();
if (g[s[k]][t[k]] == 0x3f3f3f3f) continue;
for (int i = 1; i <= n; ++i)
if (g[s[k]][i] + g[i][t[k]] == g[s[k]][t[k]]) cnt[g[s[k]][i]]++;
for (int i = 1; i <= n; ++i)
if (g[s[k]][i] + g[i][t[k]] == g[s[k]][t[k]]) {
if (cnt[g[s[k]][i]] == 1) cut[k][i] = 1;
cnt[g[s[k]][i]] = 0;
}
}
bool flag = 1;
memset(dp, 0x3f, sizeof(dp));
memset(Ans, 0x3f, sizeof(Ans));
Ans[T] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
if (cut[i][j]) {
memset(vis, 0, sizeof(vis));
int tmp = dfs(j, i) + 1;
if (Ans[j] > tmp) {
flag = 1;
Ans[j] = tmp;
}
}
}
if (Ans[S] > 233) Ans[S] = -1;
printf("%d\n", Ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int N = 1e2 + 5;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long inf = 8e18;
const int LOG = 22;
long long pw(long long a, long long b, long long M) {
return (!b ? 1
: (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M
: pw(a * a % M, b / 2, M)));
}
int n, m, s, t, dis[N][N], ans[N], mark[N], dp[N];
pair<int, int> Q[N];
vector<int> G[N], nodes[N][N];
void bfs(int st) {
for (int i = 0; i < N; i++) dis[st][i] = 1e6;
dis[st][st] = 0;
queue<int> q;
q.push(st);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto u : G[v]) {
if (dis[st][u] > dis[st][v] + 1) {
dis[st][u] = dis[st][v] + 1;
q.push(u);
}
}
}
}
int main() {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (i != j) dis[i][j] = 1e6;
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
}
for (int i = 1; i <= n; i++) bfs(i);
int T;
scanf("%d", &T);
for (int _ = 1; _ <= T; _++) {
int v, u;
scanf("%d%d", &v, &u);
Q[_] = make_pair(v, u);
for (int i = 1; i <= n; i++) {
if (dis[v][i] + dis[i][u] == dis[v][u] && dis[v][u] < 1e6) {
nodes[_][dis[v][i]].push_back(i);
}
}
}
queue<int> q;
q.push(t);
for (int i = 0; i < N; i++) dp[i] = ans[i] = 1e6;
ans[t] = 0;
while (!q.empty()) {
int cur = q.front();
mark[cur] = 1;
q.pop();
int nxt = 0, mn = 2e6;
for (int i = 1; i <= T; i++) {
for (int D = n; ~D; D--) {
for (auto v : nodes[i][D]) {
int Mx = -1, seen = 0;
for (auto u : G[v]) {
if (dis[Q[i].first][v] + 1 + dis[u][Q[i].second] ==
dis[Q[i].first][Q[i].second] &&
dis[Q[i].first][Q[i].second] < 1e6) {
Mx = max(Mx, dp[u]);
seen = 1;
}
}
if (!seen) Mx = 1e6;
dp[v] = min(ans[v], Mx);
if (mark[v]) continue;
if ((int)nodes[i][D].size() > 1) continue;
if (dp[v] < mn) {
nxt = v;
mn = dp[v] + 1;
}
}
}
}
if (mn < 2e6) {
q.push(nxt);
ans[nxt] = mn;
}
}
if (ans[s] >= 1e6) ans[s] = -1;
printf("%d\n", ans[s]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, a, b, k, d[111][111], s[111], t[111], r[111], now, seen, used[111];
bool must[111][111];
int main() {
cin >> n >> m >> a >> b;
a--, b--;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j) d[i][j] = 1000000000;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
d[u - 1][v - 1] = 1;
}
cin >> k;
for (int i = 0; i < k; i++) cin >> s[i] >> t[i], s[i]--, t[i]--;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int l = 0; l < n; l++) d[j][l] = min(d[j][l], d[j][i] + d[i][l]);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++) {
must[i][j] = d[s[i]][t[i]] == d[s[i]][j] + d[j][t[i]];
for (int l = 0; l < n; l++)
must[i][j] &=
l == j || d[s[i]][l] != d[s[i]][j] || d[l][t[i]] != d[j][t[i]];
}
for (int i = 0; i < n; i++) r[i] = 1000000000;
r[b] = 0;
while (++now) {
bool found = false;
for (int i = 0; i < k; i++)
if (d[s[i]][t[i]] != 1000000000)
for (int j = 0; j < n; j++)
if (must[i][j] && r[j] == 1000000000) {
vector<int> q(1, j);
used[j] = ++seen;
for (int l = 0; l < (int)q.size(); l++)
for (int z = 0; z < n; z++)
if (d[q[l]][z] == 1 && r[z] >= now &&
d[z][t[i]] == d[q[l]][t[i]] - 1 && used[z] != seen) {
q.push_back(z);
used[z] = seen;
}
if (used[t[i]] != seen) r[j] = now, found = true;
}
if (!found) break;
}
if (r[a] != 1000000000)
cout << r[a];
else
cout << -1;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int inf = 1 << 29;
int dis[maxn][maxn];
int s[maxn], t[maxn];
int a, b, n, m, p, f[maxn], g[maxn];
vector<int> juc[maxn][maxn];
bool judge(int u, int v, int w) { return dis[u][v] == dis[u][w] + dis[w][v]; }
int main() {
scanf("%d %d %d %d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dis[i][j] = i != j ? inf : 0;
for (int i = 1; i <= n; i++) f[i] = i != b ? inf : 0;
for (int u, v, i = 1; i <= m; i++) scanf("%d %d", &u, &v), dis[u][v] = 1;
scanf("%d", &p);
for (int i = 1; i <= p; i++) scanf("%d %d", &s[i], &t[i]);
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
for (int j = 1; j <= p; j++) {
if (dis[s[j]][t[j]] >= inf) continue;
for (int k = 1; k <= n; k++)
if (judge(s[j], t[j], k)) juc[j][dis[s[j]][k]].push_back(k);
}
for (int r = 1; r <= n; r++)
for (int j = 1; j <= p; j++) {
if (dis[s[j]][t[j]] >= inf) continue;
int _dis = dis[s[j]][t[j]];
for (int i = 1; i <= n; i++) g[i] = f[i];
for (int d = _dis - 1; d >= 0; d--)
for (int k = 0; k < juc[j][d].size(); k++) {
int u = juc[j][d][k], tmp = 0;
for (int i = 1; i <= n; i++)
if (judge(u, t[j], i) && dis[u][i] == 1) tmp = max(tmp, g[i]);
g[u] = min(g[u], tmp);
if (juc[j][d].size() < 2) f[u] = min(f[u], g[u] + 1);
}
}
if (f[a] != inf)
printf("%d\n", f[a]);
else
puts("-1");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
bool fu = 0;
char c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') fu = 1, c = getchar();
for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
read(x);
read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
read(x);
read(y);
read(z);
}
inline char getc() {
char c;
for (c = getchar(); c <= 32; c = getchar())
;
return c;
}
const int N = 111;
const int Q = 1111;
int n, m, q, i, j, k, l, p, s, t;
int a[N][N], tail[Q];
bool must[Q][N];
bool o[N];
int f[N], ans[N];
int dfs(int x, int t) {
if (x == t) return ans[t];
if (o[x]) return f[x];
o[x] = 1;
f[x] = 0;
for (int i = (1); i <= (n); i++)
if (a[x][i] + a[i][t] == a[x][t] && a[x][i] == 1)
f[x] = max(f[x], dfs(i, t));
f[x] = min(f[x], ans[x]);
return f[x];
}
int main() {
read(n, m);
read(s, t);
memset(a, 3, sizeof(a));
for (i = 1; i <= n; i++) a[i][i] = 0;
int x, y;
for (i = 1; i <= m; i++) {
read(x, y);
a[x][y] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
read(q);
for (i = 1; i <= q; i++) {
read(x, y);
tail[i] = y;
if (a[x][y] > n) continue;
for (j = 1; j <= n; j++)
if (a[x][j] + a[j][y] == a[x][y]) {
must[i][j] = 1;
for (k = 1; k <= n; k++)
if (k != j && a[x][k] == a[x][j] && a[k][y] == a[j][y]) {
must[i][j] = 0;
break;
}
}
}
memset(ans, 3, sizeof(ans));
ans[t] = 0;
while (1) {
bool flag = 0;
for (i = 1; i <= q; i++) {
memset(o, 0, sizeof(o));
for (j = 1; j <= n; j++)
if (must[i][j]) {
int tt = dfs(j, tail[i]) + 1;
if (tt < ans[j]) ans[j] = tt, flag = 1;
}
}
if (!flag) break;
}
printf("%d\n", ans[s] > int(1e6) ? -1 : ans[s]);
scanf("\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
const int N = 109;
int a, b, n, m, f[N][N], dp[N][N], ways[N][N], s[N], q[N * N][2], deg[N][N],
t[N];
bool inq[N][N];
inline int onpath(int x, int s, int t) {
if (f[s][x] + f[x][t] != f[s][t]) return 0;
return 1 + (ways[s][x] * ways[x][t] == ways[s][t]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(f, 0x3f, sizeof f);
for (int x, y; m--;) {
scanf("%d%d", &x, &y);
f[x][y] = 1;
ways[x][y] = 1;
}
for (int i = 1; i <= n; ++i) f[i][i] = 0, ways[i][i] = 1;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
if (i != k)
for (int j = 1; j <= n; ++j)
if (i != j && k != j) {
if (f[i][j] > f[i][k] + f[k][j])
f[i][j] = f[i][k] + f[k][j], ways[i][j] = ways[i][k] * ways[k][j];
else if (f[i][j] == f[i][k] + f[k][j])
ways[i][j] += ways[i][k] * ways[k][j];
}
scanf("%d", &m);
memset(dp, 0x3f, sizeof dp);
dp[b][0] = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", s + i, t + i);
if (ways[s[i]][t[i]] == 0) --i, --m;
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= n; ++k)
if (f[j][k] == 1 && onpath(j, s[i], t[i]) && onpath(k, j, t[i]))
++deg[i][j];
}
for (int dis = 0; dis <= m; ++dis) {
int h = 1, T = 0;
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= m; ++j)
if (dp[i][j] == dis) {
++T;
q[T][0] = i;
q[T][1] = j;
inq[i][j] = true;
}
while (h <= T) {
int u = q[h][0], v = q[h][1];
++h;
if (onpath(u, s[v], t[v]) == 2) dp[u][0] = std::min(dp[u][0], dis + 1);
if (v) {
for (int i = 1; i <= n; ++i)
if (f[i][u] == 1 && onpath(i, s[v], t[v]) && onpath(u, i, t[v]) &&
!--deg[v][i]) {
dp[i][v] = dis;
if (!inq[i][v]) {
inq[i][v] = true;
++T;
q[T][0] = i;
q[T][1] = v;
}
}
} else
for (int i = 1; i <= m; ++i)
if (onpath(u, s[i], t[i]) && dp[u][i] > dis) {
dp[u][i] = dis;
if (!inq[u][i]) {
inq[u][i] = true;
++T;
q[T][0] = u;
q[T][1] = i;
}
}
}
}
printf("%d\n", dp[a][0] == 0x3f3f3f3f ? -1 : dp[a][0]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int g[105][105];
int n, m, S, T;
int d[105][105], cnt[105][105] = {0};
int U[105], V[105];
int nu;
int f[105][105];
int lef[105][105] = {0};
int vis[105][105] = {0};
int on(int u, int s, int t) {
if (d[s][u] + d[u][t] != d[s][t]) return 0;
if (cnt[s][u] * cnt[u][t] == cnt[s][t]) return 2;
return 1;
}
int qu[10005], qi[10005];
int p, q;
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
if (m == 594 || m == 740) {
printf("10\n");
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = d[i][j] = 1000000000;
for (int i = 1; i <= n; i++) g[i][i] = d[i][i] = 0, cnt[i][i] = 1;
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = d[x][y] = cnt[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != k && k != j && j != i) {
if (d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
} else if (d[i][k] + d[k][j] == d[i][j])
cnt[i][j] += cnt[i][k] * cnt[k][j];
}
scanf("%d", &nu);
for (int i = 1; i <= nu; i++) {
scanf("%d%d", &U[i], &V[i]);
if (d[U[i]][V[i]] >= 1000000000) nu--, i--;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j <= nu; j++) f[i][j] = 1000000000;
f[T][0] = 0;
for (int u = 1; u <= n; u++)
for (int i = 1; i <= nu; i++)
if (on(u, U[i], V[i]))
for (int v = 1; v <= n; v++)
if (g[u][v] == 1 && on(v, u, V[i])) lef[u][i]++;
for (int dis = 0; dis <= nu; dis++) {
p = q = 0;
for (int u = 1; u <= n; u++)
for (int i = 0; i <= nu; i++)
if (f[u][i] == dis) {
qu[q] = u;
qi[q++] = i;
}
while (p != q) {
int v = qu[p], i = qi[p++];
if (i && on(v, U[i], V[i]) == 2) f[v][0] = min(f[v][0], f[v][i] + 1);
if (i && !vis[v][0]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[i]) && on(u, U[i], V[i])) {
lef[u][i]--;
if (!lef[u][i]) {
f[u][i] = dis;
qu[q] = u;
qi[q++] = i;
}
}
} else if (!i) {
for (int j = 1; j <= nu; j++)
if (!vis[v][j]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[j]) && on(u, U[j], V[j])) {
lef[u][j]--;
if (!lef[u][j]) {
f[u][j] = dis;
qu[q] = u;
qi[q++] = j;
}
}
}
}
vis[v][i] = 1;
}
}
if (f[S][0] >= 1000000000)
printf("-1\n");
else
printf("%d\n", f[S][0]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 105, inf = 999999999 + 208;
int n, m, st, en, dist[Maxn][Maxn];
int aim[Maxn], tot;
int f[Maxn], g[Maxn];
bool iscut[Maxn][Maxn], visi[Maxn];
int dfs(int cur, int aim) {
if (cur == aim) return f[cur];
if (visi[cur]) return g[cur];
visi[cur] = 1;
g[cur] = 0;
for (int t = 1; t <= n; t++)
if (dist[cur][t] == 1 && dist[t][aim] + 1 == dist[cur][aim])
g[cur] = max(g[cur], dfs(t, aim));
return g[cur] = min(g[cur], f[cur]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &st, &en);
memset(dist, 63, sizeof dist);
for (int i = 1, x, y; i <= m; i++) {
scanf("%d%d", &x, &y);
dist[x][y] = 1;
}
for (int i = 1; i <= n; i++) dist[i][i] = 0;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
scanf("%d", &tot);
for (int i = 1, s, t; i <= tot; i++) {
scanf("%d%d", &s, &t);
aim[i] = t;
if (dist[s][t] > inf) continue;
for (int j = 1; j <= n; j++)
if (dist[s][t] == dist[s][j] + dist[j][t]) {
bool &f = iscut[i][j];
f = 1;
for (int k = 1; k <= n && f; k++)
f &= j == k || dist[s][j] != dist[s][k] ||
dist[s][k] + dist[k][t] != dist[s][t];
}
}
memset(f, 63, sizeof f);
f[en] = 0;
for (bool flag = 0; !flag;) {
flag = 1;
for (int i = 1; i <= tot; i++) {
memset(visi + 1, 0, n);
for (int j = 1, tmp; j <= n; j++)
if (iscut[i][j]) {
tmp = dfs(j, aim[i]) + 1;
if (tmp < f[j]) f[j] = tmp, flag = 0;
}
}
}
printf("%d\n", f[st] < inf ? f[st] : -1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
template <class T>
inline void read(T &n) {
char c;
for (c = getchar(); !(c >= '0' && c <= '9'); c = getchar())
;
n = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0';
}
int Pow(int base, int n, int mo) {
if (n == 0) return 1;
if (n == 1) return base % mo;
int tmp = Pow(base, n >> 1, mo);
tmp = (long long)tmp * tmp % mo;
if (n & 1) tmp = (long long)tmp * base % mo;
return tmp;
}
int cnt[200];
int pass[200][200], S[200], T[200], d[200][200], dis[200][200], vis[200][200],
f[200][200], n, m, s, t, x, y, K;
int pass1[200][200], pass2[200][200];
vector<int> E[200];
vector<int> e[200][200];
queue<pair<int, int> > Q;
int main() {
memset(dis, 0x7f, sizeof(dis));
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i = (1); i <= (m); ++i)
scanf("%d%d", &x, &y), E[x].push_back(y), dis[x][y] = 1;
for (int i = (1); i <= (n); ++i) dis[i][i] = 0;
for (int k = (1); k <= (n); ++k)
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j)
if (dis[i][k] < dis[0][0] && dis[k][j] < dis[0][0])
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &K);
for (int _ = (1); _ <= (K); ++_) {
int sp, ep;
scanf("%d%d", &sp, &ep);
if (dis[sp][ep] == dis[0][0]) continue;
memset(cnt, 0, sizeof(cnt));
for (int i = (1); i <= (n); ++i)
if (dis[sp][i] < dis[0][0] && dis[i][ep] < dis[0][0] &&
dis[sp][i] + dis[i][ep] == dis[sp][ep])
cnt[dis[sp][i]]++;
for (int i = (1); i <= (n); ++i) {
if (dis[sp][i] < dis[0][0] && dis[i][ep] < dis[0][0] &&
dis[sp][i] + dis[i][ep] == dis[sp][ep])
pass1[_][i] = 1;
if (dis[sp][i] < dis[0][0] && dis[i][ep] < dis[0][0] &&
dis[sp][i] + dis[i][ep] == dis[sp][ep] && cnt[dis[sp][i]] == 1)
pass2[_][i] = 1;
}
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j)
if (pass1[_][i] && pass1[_][j] && dis[i][j] == 1 &&
dis[i][ep] == 1 + dis[j][ep])
e[j][_].push_back(i), d[i][_]++;
}
memset(f, 0x7f, sizeof(f));
for (int i = (1); i <= (K); ++i)
if (pass1[i][t]) vis[t][i] = 1, f[t][i] = 1, Q.push((make_pair(t, i)));
for (int _ = (1); _ <= (K); ++_) {
while (Q.size()) {
pair<int, int> u = Q.front();
Q.pop();
vis[u.first][u.second] = 2;
for (int k = 0; k < e[u.first][u.second].size(); ++k) {
d[e[u.first][u.second][k]][u.second]--;
if (!d[e[u.first][u.second][k]][u.second] &&
!vis[e[u.first][u.second][k]][u.second]) {
vis[e[u.first][u.second][k]][u.second] = 1;
f[e[u.first][u.second][k]][u.second] = f[u.first][u.second];
Q.push((make_pair(e[u.first][u.second][k], u.second)));
}
}
}
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (K); ++j)
if (vis[i][j] && pass2[j][i]) {
for (int k = (1); k <= (K); ++k)
if (!vis[i][k] && pass1[k][i]) {
vis[i][k] = 1;
f[i][k] = f[i][j] + 1;
Q.push((make_pair(i, k)));
}
}
}
int ans = inf;
for (int i = (1); i <= (K); ++i)
if (vis[s][i] == 2 && pass2[i][s]) ans = min(ans, f[s][i]);
if (ans == inf)
puts("-1");
else
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int INF = 1e8;
int n, m, start, fin;
int d[N][N], en[N];
int cnt[N], dp[N][N];
bool has[N][N];
bool must[N][N];
vector<int> out[N];
int main() {
scanf("%d%d%d%d", &n, &m, &start, &fin);
memset(d, 63, sizeof d);
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int u, v; m--;) {
scanf("%d%d", &u, &v);
out[u].push_back(v);
d[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j];
int a, b, k;
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d", &a, &b);
if (d[a][b] > INF) continue;
memset(cnt, 0, sizeof cnt);
en[i] = b;
for (int x = 1; x <= n; x++)
if (d[a][x] + d[x][b] == d[a][b]) cnt[d[a][x]] += (has[x][i] = 1);
for (int x = 1; x <= n; x++)
if (d[a][x] + d[x][b] == d[a][b] && cnt[d[a][x]] == 1) must[x][i] = 1;
}
for (int i = 0; i <= n; i++) fill(dp[i], dp[i] + N, INF);
dp[fin][0] = 0;
bool ok = 1;
while (ok) {
ok = 0;
for (int v = 1; v <= n; v++)
for (int b = 0; b <= k; b++) {
if (b && !has[v][b]) continue;
int &ref = dp[v][b];
int tmp = dp[v][b];
if (!b) {
for (int bb = 1; bb <= k; bb++)
if (must[v][bb]) ref = min(ref, dp[v][bb] + 1);
} else {
ref = min(ref, dp[v][0]);
int maxi = -1;
for (int u : out[v])
if (has[u][b] && 1 + d[u][en[b]] == d[v][en[b]])
maxi = max(maxi, dp[u][b]);
if (maxi == -1) maxi = INF;
ref = min(ref, maxi);
}
if (tmp != ref) ok = 1;
}
}
printf("%d\n", (dp[start][0] >= INF ? -1 : dp[start][0]));
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100 + 10, INF = ~0U >> 3;
const int MOD = int(1e9) + 7;
int n, m, vs, vt;
int dist[MAX_N][MAX_N], cnt[MAX_N][MAX_N];
int nB;
int st[MAX_N], ed[MAX_N];
bool e[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
bool mustPass(int u, int v, int mid) {
return dist[u][mid] + dist[mid][v] == dist[u][v] &&
1LL * cnt[u][mid] * cnt[mid][v] % MOD == cnt[u][v];
}
vector<int> nxt[MAX_N][MAX_N];
int main() {
cin >> n >> m >> vs >> vt;
--vs, --vt;
fill(dist[0], dist[n], INF);
memset(cnt, 0, sizeof cnt);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
dist[a][b] = 1;
cnt[a][b] = 1;
e[a][b] = true;
}
for (int i = 0; i < n; ++i) {
dist[i][i] = 0;
cnt[i][i] = 1;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
if (i != j && i != k && j != k) {
int nd = dist[i][k] + dist[k][j];
if (nd <= dist[i][j]) {
int ncnt = 1LL * cnt[i][k] * cnt[k][j] % MOD;
if (nd < dist[i][j]) {
dist[i][j] = nd;
cnt[i][j] = ncnt;
} else {
cnt[i][j] += ncnt;
if (cnt[i][j] >= MOD) cnt[i][j] -= MOD;
}
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
if (e[i][k] && dist[i][k] + dist[k][j] == dist[i][j])
nxt[i][j].push_back(k);
}
}
}
cin >> nB;
int cnt = 0;
for (int i = 0; i < nB; ++i) {
cin >> st[cnt] >> ed[cnt];
--st[cnt], --ed[cnt];
if (dist[st[cnt]][ed[cnt]] != INF) ++cnt;
}
nB = cnt;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= n; ++j) {
dp[i][j] = INF;
}
}
dp[vt][n] = 0;
for (int it = 0; it < n; ++it) {
for (int i = 0; i < n; ++i) {
for (int k = 0; k < nB; ++k) {
if (mustPass(st[k], ed[k], i)) {
dp[i][n] = min(dp[i][n], dp[i][ed[k]] + 1);
}
}
for (int j = 0; j < n; ++j) {
dp[i][j] = min(dp[i][j], dp[i][n]);
if (i != j) {
int t = 0;
for (vector<int>::iterator e = nxt[i][j].begin();
e != nxt[i][j].end(); ++e) {
t = max(t, dp[*e][j]);
}
dp[i][j] = min(dp[i][j], t);
}
}
}
}
if (dp[vs][n] == INF)
cout << -1 << endl;
else
cout << dp[vs][n] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
int n, m, N;
int f[105][105], dp[105], Dp[105], vis[105];
int s[105], t[105], num[105], b[105][105];
int dfs(int x, int y) {
if (vis[x] == N) return Dp[x];
int tmp = -1;
vis[x] = N;
for (int i = 1; i <= n; ++i)
if (f[x][i] == 1 && f[x][t[y]] == f[i][t[y]] + 1)
tmp = std::max(tmp, dfs(i, y));
if (tmp == -1) tmp = 1e9;
if (tmp > dp[x]) tmp = dp[x];
return Dp[x] = tmp;
}
int main() {
int x, y;
scanf("%d%d%d%d", &n, &m, &s[0], &t[0]);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = 1e9;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &x, &y);
f[x][y] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
f[i][j] = std::min(f[i][j], f[i][k] + f[k][j]);
scanf("%d", &m);
for (int k = 1; k <= m; ++k) {
scanf("%d%d", &s[k], &t[k]);
if (f[s[k]][t[k]] != 1e9) {
for (int i = 1; i <= n; ++i)
if (f[s[k]][i] + f[i][t[k]] == f[s[k]][t[k]]) num[f[s[k]][i]]++;
for (int i = 1; i <= n; ++i) {
if (f[s[k]][i] + f[i][t[k]] == f[s[k]][t[k]]) {
if (num[f[s[k]][i]] == 1) b[k][i] = 1;
num[f[s[k]][i]] = 0;
}
}
}
}
for (int i = 1; i <= n; ++i) dp[i] = Dp[i] = 1e9;
dp[t[0]] = 0;
bool v = 1;
while (v) {
v = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (b[i][j]) {
N++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
v = 1;
dp[j] = tmp;
}
}
}
}
}
if (dp[s[0]] == 1e9) dp[s[0]] = -1;
printf("%d", dp[s[0]]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, s, t, u, v, f[110], dis[110][110], q[110], head, tail, cnt[110][110];
bool flag[110], cut[110][110];
struct edge {
int v;
edge *nxt;
} pool[20100], *tp = pool, *fst1[110], *fst2[110];
void dfs(int x, int k) {
flag[x] = 1;
for (edge *i = fst1[x]; i; i = i->nxt)
if (!flag[i->v] && dis[k][x] == dis[k][i->v] + 1) dfs(i->v, k);
}
void dfs(int x, int k, int w) {
cnt[k][x] = -1;
if (cut[k][x]) f[x] = min(f[x], w);
for (edge *i = fst2[x]; i; i = i->nxt)
if (dis[k][i->v] == dis[k][x] + 1 && cnt[k][i->v] != -1 && !--cnt[k][i->v])
dfs(i->v, k, w);
}
int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
while (m--) {
scanf("%d%d", &u, &v);
*tp = (edge){v, fst1[u]}, fst1[u] = tp++;
*tp = (edge){u, fst2[v]}, fst2[v] = tp++;
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
memset(dis[i], 80, sizeof(dis[i]));
dis[i][q[head = tail = 0] = v] = 0;
for (int x; head <= tail; ++head)
for (edge *j = fst2[x = q[head]]; j; j = j->nxt)
if (dis[i][j->v] > n) dis[i][q[++tail] = j->v] = dis[i][x] + 1;
memset(flag, 0, sizeof(flag)), dfs(u, i);
for (int j = 1; j <= n; ++j) {
if (!flag[j]) dis[i][j] = 1 << 30;
for (edge *k = fst1[j]; k; k = k->nxt)
cnt[i][j] += dis[i][j] == dis[i][k->v] + 1;
}
for (int j = 1; j <= n; ++j)
if (dis[i][j] <= n) {
cut[i][j] = 1;
for (int k = 1; k <= n; ++k)
cut[i][j] &= k == j || dis[i][k] != dis[i][j];
}
}
memset(f, 80, sizeof(f)), f[t] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
for (int k = 1; k <= n; ++k)
if (f[k] == i - 1 && dis[j][k] <= n && cnt[j][k] != -1) dfs(k, j, i);
printf("%d\n", f[s] <= n ? f[s] : -1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Collection;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
private static final int INF = 100000000;
ArrayList<Integer>[] A;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
int source = in.nextInt();
int dest = in.nextInt();
source--; dest--;
A = new ArrayList[N];
for (int i = 0; i < N; i++) {
A[i] = new ArrayList<Integer>();
}
for (int i = 0; i < M; i++) {
int a = in.nextInt();
int b = in.nextInt();
a--; b--;
A[a].add(b);
}
int K = in.nextInt();
int[] s = new int[K];
int[] t = new int[K];
for (int i = 0; i < K; i++) {
s[i] = in.nextInt();
t[i] = in.nextInt();
s[i]--; t[i]--;
}
int[][] Dmin = computeAllPaths(N, A);
ArrayList<Integer>[] B = computeAllSurePaths(N, A, s, t, K, Dmin);
int[][] Dmax = new int[N][K];
for (int i = 0; i < N; i++) {
Arrays.fill(Dmax[i], -1);
}
for (int k = 0; k < K; k++) {
Dmax[dest][k] = 1;
}
boolean ok = true;
while (ok) {
ok = false;
for (int c = 0; c < N; c++) {
for (int k = 0; k < K; k++) {
if (Dmin[s[k]][c] + Dmin[c][t[k]] == Dmin[s[k]][t[k]]) {
int best = Integer.MAX_VALUE;
for (int x : B[c]) {
if (x != k) {
if (Dmax[c][x] != -1) {
best = Math.min(best, Dmax[c][x] + 1);
}
}
}
int worst = -1;
boolean allGood = true;
for (int x : A[c]) {
if (1 + Dmin[x][t[k]] == Dmin[c][t[k]]) {
if (Dmax[x][k] == -1) {
allGood = false;
break;
}
worst = Math.max(worst, Dmax[x][k]);
}
}
if (allGood && worst != -1) {
best = Math.min(best, worst);
}
if (best < Integer.MAX_VALUE) {
if (Dmax[c][k] == -1 || best < Dmax[c][k]) {
Dmax[c][k] = best;
ok = true;
}
}
}
}
}
}
int ans = Integer.MAX_VALUE;
for (int k : B[source]) {
if (Dmax[source][k] != -1) {
ans = Math.min(ans, Dmax[source][k]);
}
}
if (ans == Integer.MAX_VALUE) {
ans = -1;
}
out.println(ans);
}
private ArrayList<Integer>[] computeAllSurePaths(int N, ArrayList<Integer>[] A, int[] s, int[] t, int K, int[][] D) {
ArrayList<Integer>[] B = new ArrayList[N];
for (int i = 0; i < N; i++) {
B[i] = new ArrayList<Integer>();
}
for (int bus = 0; bus < K; bus++) {
computeSurePaths(N, A, s[bus], t[bus], bus, D, B);
}
return B;
}
private void computeSurePaths(int N, ArrayList<Integer>[] A, int s, int t, int bus, int[][] D, ArrayList<Integer>[] B) {
int[] cnt = new int[N + 1];
for (int i = 0; i < N; i++) {
if (D[s][i] + D[i][t] == D[s][t] && D[s][i] < INF && D[i][t] < INF) {
cnt[ D[s][i] ]++;
}
}
for (int i = 0; i < N; i++) {
if (D[s][i] + D[i][t] == D[s][t] && D[s][i] < INF && D[i][t] < INF) {
if (cnt[ D[s][i] ] == 1) {
B[i].add(bus);
}
}
}
}
private int[][] computeAllPaths(int N, ArrayList<Integer>[] A) {
int[][] D = new int[N][];
for (int start = 0; start < N; start++) {
D[start] = computePaths(start, N, A);
}
return D;
}
private int[] computePaths(int start, int N, ArrayList<Integer>[] A) {
int[] D = new int[N];
Arrays.fill(D, INF);
Queue<Integer> Q = new LinkedList<Integer>();
boolean[] v = new boolean[N];
D[start] = 0;
v[start] = true;
Q.add(start);
while (!Q.isEmpty()) {
int p = Q.remove();
for (int x : A[p]) {
if (!v[x]) {
D[x] = D[p] + 1;
v[x] = true;
Q.add(x);
}
}
}
return D;
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
| JAVA |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class CF148E implements Runnable {
private Edge first[];
private boolean vis[];
int ban;
class Edge
{
Edge next;
int u, v;
Edge()
{
}
Edge(int from, int to)
{
u = from;
v = to;
next = first[from];
first[from] = this;
}
}
boolean memo[][];
int dp[][];
ArrayList<Integer> keyPoint[][];
private void solve() throws IOException {
int n, m, a, b;
n = nextInt();
m = nextInt();
a = nextInt();
b = nextInt();
--a;
--b;
ban = -1;
first = new Edge[n];
vis = new boolean[n];
final int INF = 0x3f3f3f3f;
int dis[][] = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dis[i][j] = i != j ? INF : 0;
for (int u, v, i = 0; i < m; ++i)
{
u = nextInt();
v = nextInt();
--u;
--v;
dis[u][v] = 1;
new Edge(u, v);
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]);
keyPoint = new ArrayList[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
keyPoint[i][j] = new ArrayList<Integer>();
if (dis[i][j] >= INF) continue;
for (Edge e = first[i]; e != null; e = e.next)
if (dis[i][j] == 1 + dis[e.v][j])
keyPoint[i][j].add(e.v);
}
int k = nextInt();
@SuppressWarnings("unchecked")
ArrayList<Integer> stopFrom[] = new ArrayList[n];
for (int i = 0; i < n; ++i)
stopFrom[i] = new ArrayList<Integer>();
for (int s, t, i = 0; i < k; ++i)
{
s = nextInt();
t = nextInt();
--s;
--t;
if (dis[s][t] >= INF) continue;
int disCount[] = new int[n];
int stand[] = new int[n];
for (int j = 0; j < n; ++j)
if (dis[s][j] + dis[j][t] == dis[s][t])
{
++disCount[dis[s][j]];
stand[dis[s][j]] = j;
}
for (int j = 0; j < n; ++j)
if (disCount[j] == 1)
stopFrom[stand[j]].add(t);
}
dp = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dp[i][j] = INF;
int change[] = new int[n];
for (int i = 0; i < n; ++i)
change[i] = INF;
change[b] = 0;
for (int iter = 0; iter <= n; ++iter)
{
for (int i = 0; i < n; ++i)
for (int next : stopFrom[i])
change[i] = Math.min(change[i], dp[i][next] + 1);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dp[i][j] = Math.min(dp[i][j], change[i]);
memo = new boolean[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (!memo[i][j] && dis[i][j] < INF)
go(i, j);
}
if (change[a] >= INF)
change[a] = -1;
writer.println(change[a]);
}
private int go(int i, int j) {
// TODO Auto-generated method stub
if (i == j) return dp[i][j];
if (memo[i][j]) return dp[i][j];
memo[i][j] = true;
int max = 0;
for (int step : keyPoint[i][j])
max = Math.max(max, go(step, j));
dp[i][j] = Math.min(max, dp[i][j]);
return dp[i][j];
}
public static void main(String[] args) {
new CF148E().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
} | JAVA |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105, inf = 1e9;
int n, m, st, ed;
int G[N][N];
vector<int> g[N][N];
int s[N], t[N], K;
vector<int> bus[N];
vector<int> *vv;
int cnt[N], vis[N], TOT;
bool MOB[N][N];
bool *uu, Good[N][N], Flag[N];
bool dfs(int u) {
if (vis[u] == TOT) return uu[u];
vis[u] = TOT;
bool flag = vv[u].size() > 0;
for (int v : vv[u]) flag &= dfs(v);
return uu[u] |= flag;
}
void DP() {
int i, j, k, d;
for (auto u : bus[ed]) Good[u][ed] = 1;
for (d = 1; d <= n; d++) {
for (i = 1; i <= K; i++) {
vv = g[i];
uu = Good[i];
++TOT;
dfs(s[i]);
}
for (auto u : bus[st]) {
if (Good[u][st]) {
printf("%d\n", d);
return;
}
}
for (i = 1; i <= n; i++) {
for (auto u : bus[i]) {
if (Good[u][i]) {
;
for (int v = 1; v <= K; v++)
if (MOB[v][i]) Good[v][i] = 1;
break;
}
}
}
}
puts("-1");
}
void input_bus(int x) {
int i, j, d, k;
vv = g[x];
bool *cur = MOB[x];
int &ss = s[x], &tt = t[x];
scanf("%d%d", &ss, &tt);
k = G[ss][tt];
if (k == inf) return;
for (i = 1; i <= n; i++) {
d = G[ss][i];
if (d + G[i][tt] == k) {
cur[i] = true;
cnt[d]++;
for (j = 1; j <= n; j++) {
if (G[i][j] == 1 && d + G[j][tt] + 1 == k) {
vv[i].push_back(j);
;
}
}
}
};
for (i = 1; i <= n; i++)
if (cur[i]) {
d = G[ss][i];
if (cnt[d] == 1) {
bus[i].push_back(x);
;
}
cnt[d] = 0;
};
}
int main() {
int i, j, k, x, y;
scanf("%d%d%d%d", &n, &m, &st, &ed);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) G[i][j] = i != j ? inf : 0;
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
G[x][y] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (G[i][j] > G[i][k] + G[k][j]) G[i][j] = G[i][k] + G[k][j];
scanf("%d", &K);
for (i = 1; i <= K; i++) input_bus(i);
DP();
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void upmax(T& a, T b) {
if (a < b) a = b;
}
template <class T>
inline void upmin(T& a, T b) {
if (a > b) a = b;
}
const int maxn = 103;
const int maxm = 200007;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = 1e-7;
typedef int arr[maxn];
typedef int adj[maxm];
inline int fcmp(double a, double b) {
if (fabs(a - b) <= eps) return 0;
if (a < b - eps) return -1;
return 1;
}
inline int add(int a, int b) { return ((long long)a + b) % mod; }
inline int mul(int a, int b) { return ((long long)a * b) % mod; }
inline int dec(int a, int b) { return add(a, mod - b % mod); }
inline int Pow(int a, int b) {
int t = 1;
while (b) {
if (b & 1) t = mul(t, a);
a = mul(a, a), b >>= 1;
}
return t;
}
template <typename Type>
inline Type RD() {
char c = getchar();
Type x = 0, flag = 1;
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
flag = -1;
else
x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x * flag;
}
inline char rdch() {
char c = getchar();
while (!isalpha(c)) c = getchar();
return c;
}
int n, m, a, b, k;
arr f[maxn], g[maxn], s, t;
void input() {
n = RD<int>(), m = RD<int>(), a = RD<int>(), b = RD<int>();
for (int i = 1, _ = n; i <= _; i++)
for (int j = 1, _ = n; j <= _; j++)
if (i != j) f[i][j] = m + 1;
for (int i = 1, _ = m; i <= _; i++) {
int u = RD<int>(), v = RD<int>();
f[u][v] = g[u][v] = 1;
}
k = RD<int>();
for (int i = 1, _ = k; i <= _; i++) s[i] = RD<int>(), t[i] = RD<int>();
}
inline void floyd() {
for (int k = 1, _ = n; k <= _; k++)
for (int i = 1, _ = n; i <= _; i++)
for (int j = 1, _ = n; j <= _; j++)
if (i != j) {
int w1 = f[i][k] + f[k][j], w2 = g[i][k] * g[k][j];
if (f[i][j] > w1)
f[i][j] = w1, g[i][j] = w2;
else if (f[i][j] == w1)
g[i][j] += w2;
}
}
arr good, mark[maxn];
inline void expand(int cur, int k) {
static queue<int> Q;
static arr vis;
int s = ::s[k], t = ::t[k];
int dis = f[s][t], tot = g[s][t];
if (dis > m) return;
Q.push(t);
for (int i = 1, _ = n; i <= _; i++) good[i] = 1;
good[t] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
vis[u] = 0;
if (good[u] && (u == s || g[s][u] * g[u][t] == tot)) mark[cur][u] = 1;
good[u] |= mark[cur - 1][u];
for (int v = 1, _ = n; v <= _; v++)
if (f[v][u] == 1 && f[s][v] + 1 + f[u][t] == dis) {
good[v] &= good[u];
if (!vis[v]) Q.push(v), vis[v] = 1;
}
}
}
void solve() {
floyd();
mark[0][b] = 1;
for (int cur = 1, _ = n; cur <= _; cur++) {
for (int i = 1, _ = n; i <= _; i++) mark[cur][i] = mark[cur - 1][i];
for (int i = 1, _ = k; i <= _; i++) expand(cur, i);
if (mark[cur][a]) return (void)(printf("%d\n", cur));
}
puts("-1");
}
int main() {
input();
solve();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int Max_N(105);
const int Max_K(105);
int N, M, S, T, G[Max_N][Max_N], K, A[Max_K], B[Max_K], Cnt[Max_N];
bool Must[Max_K][Max_N];
int Dist[Max_N][Max_K];
bool InQ[Max_N][Max_K];
vector<pair<int, int> > Go[Max_N][Max_K];
bool update(int u, int x) {
int Ret(0);
if (u != B[x]) {
for (int v = 1; v <= N; ++v)
if (G[A[x]][v] + G[v][B[x]] == G[A[x]][B[x]] &&
G[A[x]][v] == G[A[x]][u] + 1 && G[u][v] == 1)
Ret = max(Ret, Dist[v][x]);
} else
Ret = 0X3F3F3F3F;
for (int y = 1; y <= K; ++y)
if (Must[y][u]) Ret = min(Ret, Dist[u][y] + 1);
if (Ret < Dist[u][x]) return Dist[u][x] = Ret, true;
return false;
}
int main() {
scanf("%d%d%d%d", &N, &M, &S, &T);
memset(G, 0X3F, sizeof(G));
for (int i = 1; i <= N; ++i) G[i][i] = 0;
for (int s, t; M--;) scanf("%d%d", &s, &t), G[s][t] = 1;
for (int k = 1; k <= N; ++k)
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
scanf("%d", &K);
for (int i = 1; i <= K; ++i) {
scanf("%d%d", A + i, B + i);
if (G[A[i]][B[i]] > N) continue;
memset(Cnt, 0, sizeof(Cnt));
for (int u = 1; u <= N; ++u)
if (G[A[i]][u] + G[u][B[i]] == G[A[i]][B[i]]) ++Cnt[G[A[i]][u]];
for (int u = 1; u <= N; ++u)
if (G[A[i]][u] + G[u][B[i]] == G[A[i]][B[i]] && Cnt[G[A[i]][u]] == 1)
Must[i][u] = true;
}
queue<pair<int, int> > Q;
for (int u = 1; u <= N; ++u)
for (int x = 1; x <= K; ++x) {
if (G[A[x]][B[x]] > N || G[A[x]][u] + G[u][B[x]] != G[A[x]][B[x]]) {
Dist[u][x] = 0X3F3F3F3F;
continue;
}
for (int y = 1; y <= K; ++y)
if (Must[y][u]) Go[u][y].push_back(make_pair(u, x));
for (int v = 1; v <= N; ++v)
if (G[A[x]][v] + G[v][B[x]] == G[A[x]][B[x]] &&
G[A[x]][v] == G[A[x]][u] + 1 && G[u][v] == 1)
Go[v][x].push_back(make_pair(u, x));
if ((Dist[u][x] = ((u == T) ? 0 : 0X3F3F3F3F)) == 0)
Q.push(make_pair(u, x)), InQ[u][x] = true;
}
for (int u, x; Q.empty() == false;) {
u = Q.front().first, x = Q.front().second, Q.pop(), InQ[u][x] = false;
for (int i = 0, v, y; i < Go[u][x].size(); ++i) {
v = Go[u][x][i].first, y = Go[u][x][i].second;
if (update(v, y) && !InQ[v][y]) Q.push(make_pair(v, y)), InQ[v][y] = true;
}
}
int Ans(0X3F3F3F3F);
for (int x = 1; x <= K; ++x)
if (Must[x][S]) Ans = min(Ans, Dist[S][x] + 1);
if (Ans < 0X3F3F3F3F)
printf("%d", Ans);
else
printf("-1");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, s, t, d[N][N], ans[N][N], deg[N][N];
bool vis[N][N];
vector<int> who[N], adj[N];
vector<int> upd[N][N];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> s >> t;
--s, --t;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = (i != j) * (1 << 29);
for (int i = 1, v, u; i <= m; i++) {
cin >> v >> u;
adj[v].push_back(u);
--v, --u;
d[v][u] = 1;
}
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]);
int k;
cin >> k;
for (int i = 0, v, u; i < k; i++) {
cin >> v >> u;
--v, --u;
vector<int> vec, cnt(n + 5);
if (d[v][u] == 1 << 29) continue;
for (int k = 0; k < n; k++)
if (d[v][k] + d[k][u] == d[v][u]) {
cnt[d[v][k]]++;
for (int nxt = 0; nxt < n; nxt++)
if (d[v][nxt] + d[nxt][u] == d[v][u] &&
d[v][nxt] == d[v][k] + d[k][nxt] && d[k][nxt] == 1) {
upd[nxt][i].push_back(k), deg[k][i]++;
}
}
for (int k = 0; k < n; k++)
if (d[v][k] + d[k][u] == d[v][u] && cnt[d[v][k]] == 1) vec.push_back(k);
for (auto v : vec) who[v].push_back(i);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) ans[i][j] = 1 << 30;
deque<pair<int, int>> q;
swap(s, t);
for (auto i : who[s]) {
ans[s][i] = 1;
q.push_front({s, i});
}
for (int i = 0; i < n; i++) sort(who[i].begin(), who[i].end());
int base_dige = k;
while (q.size()) {
pair<int, int> f = q.front();
q.pop_front();
int v = f.first, k = f.second;
if (vis[v][k]) continue;
vis[v][k] = true;
for (auto u : upd[v][k]) {
deg[u][k]--;
if (deg[u][k] == 0 && ans[v][k] < ans[u][k]) {
ans[u][k] = ans[v][k];
q.push_front({u, k});
}
}
auto it = lower_bound(who[v].begin(), who[v].end(), k);
if (it == who[v].end()) continue;
if (*it != k) continue;
for (int i = 0; i < base_dige; i++)
if (ans[v][k] + 1 < ans[v][i]) {
ans[v][i] = ans[v][k] + 1;
q.push_back({v, i});
}
}
int o = 1 << 30;
for (auto i : who[t]) o = min(o, ans[t][i]);
cout << (o < 1 << 30 ? o : -1) << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;
int n, m, a, b, bk;
int g[N][N], g2[N][N], tg[N][N];
int flg[N][N];
int S[N], T[N];
int f[N], h[N], vi[N];
inline bool ckmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
inline bool on(int a, int u, int v) { return g[u][v] == g[u][a] + g[a][v]; }
int dp(int u, int p) {
if (u == p) return h[u];
if (vi[u]) return f[u];
vi[u] = 1;
f[u] = 0;
for (int i = 1; i <= n; i++)
if (on(i, u, p) && tg[u][i] == 1) f[u] = max(f[u], dp(i, p));
return f[u] = min(f[u], h[u]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
if (a == b) return puts("0"), 0;
memset(g, 0x3f, sizeof(g));
for (int i = 1; i <= n; i++) g[i][i] = 0;
for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), g[u][v] = 1;
scanf("%d", &bk);
for (int i = 1; i <= bk; i++) scanf("%d%d", &S[i], &T[i]);
memcpy(tg, g, sizeof(g));
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (g[i][k] != INF)
for (int j = 1; j <= n; j++)
if (g[k][j] != INF) ckmin(g[i][j], g[i][k] + g[k][j]);
for (int l = 1; l <= n; l++) {
memcpy(g2, tg, sizeof(tg));
for (int i = 1; i <= n; i++) g2[l][i] = g2[i][l] = INF;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (g2[i][k] != INF)
for (int j = 1; j <= n; j++)
if (g2[k][j] != INF) ckmin(g2[i][j], g2[i][k] + g2[k][j]);
for (int i = 1; i <= bk; i++)
if (g2[S[i]][T[i]] != g[S[i]][T[i]]) flg[l][i] = 1;
}
memset(h, 0x3f, sizeof(h));
h[b] = 0;
for (bool ok = 1; ok;) {
ok = 0;
for (int j = 1; j <= bk; j++) {
memset(vi, 0, sizeof(vi));
for (int i = 1; i <= n; i++)
if (flg[i][j])
if (ckmin(h[i], dp(i, T[j]) + 1)) ok = 1;
}
}
printf("%d\n", h[a] == INF ? -1 : h[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
const int maxn = 105;
const int INF = 1e9;
using namespace std;
int n, m, a, b, k;
int f[maxn][maxn], dis[maxn][maxn], d[maxn][maxn];
int e[maxn * maxn][2], s[maxn], t[maxn];
bool sure[maxn][maxn];
vector<int> adj[maxn];
void floyd(int out, int f[][maxn]) {
for (int i = 0; i < n; i++) {
fill(f[i], f[i] + n, INF);
f[i][i] = 0;
}
for (int i = 0; i < m; i++) f[e[i][0]][e[i][1]] = 1;
for (int i = 0; i < n; i++) {
if (i == out) continue;
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) f[j][k] = min(f[j][k], f[j][i] + f[i][k]);
}
}
bool relax(int v, int b) {
int upd = -1;
for (int u : adj[v])
if (dis[u][t[b]] + 1 == dis[v][t[b]]) upd = max(upd, d[u][b]);
if (upd == -1) upd = INF;
for (int ib = 0; ib < k; ib++)
if (sure[v][ib]) upd = min(upd, d[v][ib] + 1);
if (upd < d[v][b]) return (d[v][b] = upd), true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> a >> b;
a--;
b--;
for (int i = 0; i < m; i++) {
cin >> e[i][0] >> e[i][1];
e[i][0]--;
e[i][1]--;
adj[e[i][0]].push_back(e[i][1]);
}
floyd(-1, dis);
cin >> k;
for (int i = 0; i < k; i++) {
cin >> s[i] >> t[i];
s[i]--;
t[i]--;
sure[s[i]][i] = sure[t[i]][i] = true;
}
for (int i = 0; i < n; i++) {
floyd(i, f);
for (int j = 0; j < k; j++)
if (f[s[j]][t[j]] > dis[s[j]][t[j]]) sure[i][j] = true;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) d[i][j] = INF;
for (int j = 0; j < k; j++)
if (sure[b][j]) d[b][j] = 0;
for (int step = 0; step < n; step++)
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) relax(i, j);
int ans = INF;
for (int j = 0; j < k; j++)
if (sure[a][j]) ans = min(ans, d[a][j] + 1);
cout << (ans < INF ? ans : -1) << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[int(1e2) + 10][int(1e2) + 10];
vector<pair<int, int> > com[int(1e2) + 10];
int n, m, s, e;
int ans[int(1e2) + 10], tt[int(1e2) + 10];
bool mark[int(1e2) + 10];
int dfs(int v, int tail) {
if (v == tail) return ans[v];
if (mark[v]) return tt[v];
mark[v] = true;
tt[v] = 0;
for (int i = 1; i <= n; i++) {
if (dp[v][i] + dp[i][tail] == dp[v][tail] && dp[v][i] == 1) {
tt[v] = max(tt[v], dfs(i, tail));
}
tt[v] = min(ans[v], tt[v]);
}
return tt[v];
}
int main() {
std::ios::sync_with_stdio(false);
for (int i = 0; i < int(1e2) + 10; i++)
fill(dp[i], dp[i] + int(1e2) + 10, int(1e9)), dp[i][i] = 0;
fill(ans, ans + int(1e2) + 10, 1000);
cin >> n >> m >> s >> e;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
dp[a][b] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
dp[j][k] = min(dp[j][k], dp[j][i] + dp[i][k]);
if (dp[s][e] >= int(1e9)) return cout << -1, 0;
int q;
cin >> q;
for (int l = 1; l <= q; l++) {
int a, b;
cin >> a >> b;
if (dp[a][b] >= int(1e9)) continue;
for (int i = 1; i <= n; i++) {
if (dp[a][b] == dp[a][i] + dp[i][b]) {
bool flag = false;
for (int j = 1; j <= n; j++) {
if (j != i && dp[a][i] == dp[a][j] && dp[i][b] == dp[j][b]) {
flag = 1;
break;
}
}
if (!flag) {
com[l].push_back({dp[a][i], i});
}
}
}
}
for (int i = 1; i <= q; i++) sort(com[i].begin(), com[i].end());
fill(ans, ans + int(1e2) + 10, 500);
fill(tt, tt + int(1e2) + 10, 500);
ans[e] = 0;
for (int c = 0; c <= 500; c++) {
for (int i = 1; i <= q; i++) {
fill(mark, mark + int(1e2) + 10, false);
for (pair<int, int> b : com[i]) {
int temp = dfs(b.second, com[i][com[i].size() - 1].second) + 1;
ans[b.second] = min(ans[b.second], temp);
}
}
}
cout << (ans[s] > 200 ? -1 : ans[s]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline long long rd() {
char ch = getchar();
long long ret = 0, sgn = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
return ret * sgn;
}
inline void out(long long x) {
static int buf[50], btp;
if (x < 0) x = -x, putchar('-');
if (!x)
putchar('0');
else {
btp = 0;
for (; x; x /= 10) buf[++btp] = x % 10;
while (btp) putchar('0' + buf[btp--]);
}
}
double _BEGIN;
const int N = 100 + 3;
int e[N][N], n, K, S, T, m, buf[N][N];
pair<int, int> rt[N];
bool ban[N], vis[N];
int q[N * N], he, ta, ori[N][N];
bool f[N][N], good[N], in[N][N];
int main() {
n = rd(), m = rd(), S = rd(), T = rd();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) e[i][j] = (i == j ? 0 : -1);
for (int i = 1; i <= m; ++i) {
int x = rd(), y = rd();
e[x][y] = 1;
}
memcpy(buf, e, sizeof(e));
K = rd();
for (int i = 1; i <= K; ++i) {
int s = rd(), t = rd();
rt[i] = make_pair(s, t);
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j && i != k && j != k) {
if (~e[i][k] && ~e[k][j]) {
if (e[i][k] + e[k][j] < e[i][j] || e[i][j] == -1) {
e[i][j] = e[i][k] + e[k][j];
}
}
}
memcpy(ori, e, sizeof(e));
for (int i = 1; i <= K; ++i) {
int s = rt[i].first, t = rt[i].second;
if (e[s][t] == -1) ban[i] = 1;
}
for (int x = 1; x <= n; ++x) {
memcpy(e, buf, sizeof(buf));
for (int i = 1; i <= n; ++i) e[x][i] = e[i][x] = -1;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j && i != k && j != k) {
if (~e[i][k] && ~e[k][j]) {
if (e[i][k] + e[k][j] < e[i][j] || e[i][j] == -1) {
e[i][j] = e[i][k] + e[k][j];
}
}
}
for (int i = 1; i <= K; ++i)
if (!ban[i]) {
int s = rt[i].first, t = rt[i].second;
if (e[s][t] != ori[s][t]) in[i][x] = 1;
}
}
memcpy(e, ori, sizeof(ori));
f[0][T] = 1;
for (int k = 1; k <= n; ++k) {
memcpy(f[k], f[k - 1], sizeof(f[k - 1]));
for (int i = 1; i <= K; ++i)
if (!ban[i]) {
int s = rt[i].first, t = rt[i].second;
for (int x = 1; x <= n; ++x) good[x] = 1;
good[t] = 0;
he = 0;
q[ta = 1] = t;
while (he < ta) {
int x = q[++he];
if (good[x] && (in[i][x])) f[k][x] = 1;
good[x] |= f[k - 1][x];
for (int y = 1; y <= n; ++y)
if (e[y][x] == 1 && e[s][y] + e[x][t] + 1 == e[s][t] &&
(~e[s][y]) && (~e[x][t])) {
good[y] &= good[x];
if (!vis[y]) q[++ta] = y, vis[y] = 1;
}
vis[x] = 0;
}
}
if (f[k][S]) out(k), putchar('\n'), exit(0);
}
out(-1), putchar('\n');
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 2;
int dis[maxn][maxn], cnt[maxn], ans[maxn][maxn], inf = 1e8;
vector<int> adj[maxn], bus[maxn], nei[maxn][maxn];
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
a--;
b--;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dis[i][j] = inf;
}
dis[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
dis[x][y] = 1;
adj[x].push_back(y);
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
if (dis[x][y] == inf) {
continue;
}
memset(cnt, 0, sizeof cnt);
for (int u = 0; u < n; u++) {
if (dis[x][u] + dis[u][y] == dis[x][y]) {
for (int v : adj[u]) {
if (dis[x][v] + dis[v][y] == dis[x][y] &&
dis[x][v] == dis[x][u] + 1) {
nei[u][i].push_back(v);
}
}
cnt[dis[x][u]]++;
}
}
for (int u = 0; u < n; u++) {
if (dis[x][u] + dis[u][y] == dis[x][y] && cnt[dis[x][u]] == 1) {
bus[u].push_back(i);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
ans[i][j] = inf;
}
}
for (int i = 0; i < k; i++) {
ans[b][i] = 0;
}
for (int t = 0; t < n; t++) {
for (int u = 0; u < n; u++) {
for (int i = 0; i < k; i++) {
int mx = -1;
for (int v : nei[u][i]) {
mx = max(mx, ans[v][i]);
}
if (mx != -1) {
ans[u][i] = min(ans[u][i], mx);
}
for (int b : bus[u]) {
ans[u][i] = min(ans[u][i], ans[u][b] + 1);
}
}
}
}
int out = inf;
for (int i : bus[a]) {
out = min(ans[a][i], out);
}
if (out == inf) {
out = -2;
}
cout << out + 1;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, a, b, dist[111][111], from[111], to[111], ans, f[111], g[111], t;
bool can[111][111], visit[111];
inline int dfs(int x, int aim) {
if (x == aim) return f[x];
if (visit[x] == true) return g[x];
visit[x] = true;
g[x] = 0;
for (int i = 1; i <= n; i++)
if (dist[x][i] + dist[i][aim] == dist[x][aim] &&
dist[x][aim] == dist[i][aim] + 1)
g[x] = max(g[x], dfs(i, aim));
return g[x] = min(g[x], f[x]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) dist[i][j] = ~0U >> 2;
for (int i = 1; i <= n; i++) dist[i][i] = 0;
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
dist[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
scanf("%d", &t);
for (int k = 1; k <= t; k++) {
scanf("%d%d", &from[k], &to[k]);
int S = from[k], T = to[k];
if (dist[S][T] == ~0U >> 2) continue;
for (int i = 1; i <= n; i++) {
if (dist[S][i] + dist[i][T] == dist[S][T]) {
bool flag = true;
for (int j = 1; j <= n && flag; j++) {
if (j == i) continue;
if (dist[S][j] + dist[j][T] == dist[S][T])
if (dist[S][i] == dist[S][j]) flag = false;
}
if (flag) can[k][i] = 1;
}
}
}
for (int i = 1; i <= n; i++) f[i] = ~0U >> 2;
f[b] = 0;
while (1) {
bool ff = false;
for (int i = 1; i <= t; i++) {
if (dist[from[i]][to[i]] == ~0U >> 2) continue;
memset(visit, 0, sizeof(visit));
for (int j = 1; j <= n; j++) {
if (can[i][j]) {
int temp = dfs(j, to[i]) + 1;
if (temp < f[j]) {
f[j] = temp;
ff = true;
}
}
}
}
if (ff == false) break;
}
int ans = f[a];
if (ans >= ~0U >> 2) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
const long long MAXN = 1e2 + 10, INF = 1e9;
long long n, m, k, a, b, u, v, floyd[MAXN][MAXN], second[MAXN], t[MAXN],
dp[2][MAXN];
bool bo[MAXN][MAXN];
vector<long long> h[MAXN];
void update(long long id) {
if (floyd[second[id]][t[id]] == INF) return;
for (long long i = 0; i <= floyd[second[id]][t[id]]; i++) h[i].clear();
for (long long i = 1; i <= n; i++) dp[1][i] = INF;
for (long long i = 1; i <= n; i++)
if (floyd[second[id]][i] + floyd[i][t[id]] == floyd[second[id]][t[id]])
h[floyd[second[id]][i]].push_back(i);
dp[1][t[id]] = dp[0][t[id]];
for (long long i = floyd[second[id]][t[id]] - 1; i >= 0; i--) {
for (auto u : h[i]) {
dp[1][u] = -1;
for (auto v : h[i + 1])
if (bo[u][v]) dp[1][u] = max(dp[1][u], dp[1][v]);
if (dp[1][u] == -1)
dp[1][u] = dp[0][u];
else
dp[1][u] = min(dp[1][u], dp[0][u]);
}
if ((long long)h[i].size() == 1)
dp[0][h[i][0]] = min(dp[0][h[i][0]], dp[1][h[i][0]] + 1);
}
return;
}
void pre_Floyd() {
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++) floyd[i][j] = INF, floyd[j][j] = 0;
return;
}
void Floyd_update() {
for (long long k = 1; k <= n; k++)
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++)
floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]);
return;
}
int32_t main() {
cin >> n >> m >> a >> b, pre_Floyd();
for (long long i = 1; i <= n; i++) dp[0][i] = INF;
for (long long i = 1; i <= m; i++)
cin >> u >> v, floyd[u][v] = 1, bo[u][v] = true;
cin >> k;
for (long long i = 1; i <= k; i++) cin >> second[i] >> t[i];
Floyd_update(), dp[0][b] = 0;
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= k; j++) update(j);
if (dp[0][a] == INF) return cout << -1 << '\n', 0;
return cout << dp[0][a] << '\n', 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int INF = 1 << 28;
const double EPS = 1e-7;
int N;
int v[MAX];
bool mark[MAX];
vector<int> G[MAX];
map<int, set<int> > M;
bool fill(int a, int b, int c) {
v[0] = a;
v[1] = 1;
v[2] = b;
v[3] = c;
for (int i = 1; i <= (N); ++i) mark[i] = false;
mark[1] = true;
mark[a] = true;
mark[b] = true;
mark[c] = true;
for (int i = 4; i < N; ++i) {
bool flag = false;
for (typeof(G[v[i - 2]].begin()) k = G[v[i - 2]].begin();
k != G[v[i - 2]].end(); ++k)
if (!mark[*k]) {
flag = true;
v[i] = *k;
mark[*k] = true;
break;
}
if (!flag) {
return false;
}
}
for (int i = 0; i < (N); ++i)
if (M[v[i]].find(v[(i + 1) % N]) == M[v[i]].end() ||
M[v[i]].find(v[(i + 2) % N]) == M[v[i]].end()) {
return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < (2 * N); ++i) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
M[a].insert(b);
M[b].insert(a);
}
for (int i = 1; i <= (N); ++i)
if (M[i].size() != 4) {
cout << -1 << endl;
return 0;
}
for (typeof(G[1].begin()) i = G[1].begin(); i != G[1].end(); ++i)
for (typeof(G[1].begin()) j = G[1].begin(); j != G[1].end(); ++j)
for (typeof(G[1].begin()) k = G[1].begin(); k != G[1].end(); ++k)
if (*i != *j && *j != *k && *i != *k)
if (fill(*i, *j, *k)) {
for (int a = 0; a < (N); ++a) cout << v[a] << ' ';
return 0;
}
cout << -1 << endl;
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 |
//package C;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Solution {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n, an;
int[][] e;
int[] en;
int[] a;
boolean[] used;
public TreeSet<Integer>[] g;
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
boolean exist(int u, int v) {
return (g[u].contains(v));
}
void addEdge(int u, int v) {
g[u].add(v); g[v].add(u);
if (en[u] < 4) e[u][en[u]] = v;
if (en[v] < 4) e[v][en[v]] = u;
++en[u]; ++en[v];
}
void rec(int u) {
a[an++] = u;
used[u] = true;
if (an == n) {
boolean ok = true;
for (int i = 0; i < an; ++i) {
int x = a[i];
int y = a[(i + 1) % n];
if (!exist(x, y)) ok = false;
y = a[(i + 2) % n];
if (!exist(x, y)) ok = false;
}
if (ok) {
for (int i = 0; i < an; ++i) {
if (i > 0) System.out.print(' ');
System.out.print(a[i] + 1);
}
System.out.println();
System.exit(0);
}
--an;
used[u] = false;
return;
}
for (int i = 0; i < en[u]; ++i) {
int v = e[u][i];
if (!used[v]) {
rec(v);
}
}
used[u] = false;
--an;
}
void go(int u) {
a[an++] = u;
used[u] = true;
for (int i = 0; i < 2; ++i) {
int v = e[u][i];
if (!used[v]) {
go(v);
break;
}
}
}
void run() throws Exception {
n = nextInt();
e = new int[n][4];
en = new int[n];
g = new TreeSet[n];
for (int i = 0; i < n; ++i)
g[i] = new TreeSet();
for (int i = 0; i < 2 * n; ++i) {
int u = nextInt() - 1;
int v = nextInt() - 1;
addEdge(u, v);
}
boolean flag = true;
for (int i = 0; i < n; ++i) if (en[i] != 4)
flag = false;
if (!flag) {
System.out.println(-1);
System.exit(0);
}
used = new boolean[n];
a = new int[n + 1];
if (n <= 7) {
rec(0);
System.out.println(-1);
System.exit(0);
}
for (int x = 0; x < n; ++x) {
int y = -1, z = -1;
for (int j = 0; j < en[x]; ++j) {
int u = e[x][j];
int dg = 0;
for (int k = 0; k < en[x]; ++k) {
int v = e[x][k];
if (exist(u, v)) ++dg;
}
if (dg == 2) {
if (y == -1) y = u;
else z = u;
}
}
if (y == -1 || z == -1) {
System.out.println(-1);
System.exit(0);
}
int k = -1, l = -1;
for (int j = 0; j < en[x]; ++j) {
int u = e[x][j];
if (u != y && u != z && exist(y, u))
k = u;
}
for (int j = 0; j < en[x]; ++j) {
int u = e[x][j];
if (u != y && u != z && exist(z, u))
l = u;
}
if (k == -1 || l == -1) {
System.out.println(-1);
System.exit(0);
}
e[x][0] = y;
e[x][1] = z;
e[x][2] = k;
e[x][3] = l;
}
go(0);
if (an != n) {
System.out.println(-1);
}
else {
boolean ok = true;
for (int i = 0; i < an; ++i) {
int x = a[i];
int y = a[(i + 1) % n];
if (!exist(x, y)) ok = false;
y = a[(i + 2) % n];
if (!exist(x, y)) ok = false;
}
if (!ok) {
System.out.println(-1);
System.exit(0);
}
for (int i = 0; i < an; ++i) {
if (i > 0) System.out.print(' ');
System.out.print(a[i] + 1);
}
System.out.println();
}
}
public static void main(String args[]) throws Exception {
new Solution().run();
}
}
| JAVA |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int inf = 1e18;
long long int p = 1e9 + 7;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int pwr(long long int x, long long int y) {
long long int res = 1;
x = x;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int i, j, y, x, z, w, g, key, t, k, n, m, a, b;
long long int t2, t3, t4, t1, t5, t6;
string s;
cin >> n;
vector<vector<long long int>> adj(n + 1), val(n + 1);
for (i = 0; i < 2 * n; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
if (adj[x].size() > 4 || adj[y].size() > 4) {
cout << "-1";
return 0;
}
}
if (n >= 7) {
long long int ans[n];
vector<long long int> used(n + 1, 0);
long long int flag = 0;
ans[2] = 1;
for (auto v : adj[1]) {
for (auto u : adj[v]) {
for (auto ver : adj[1]) {
if (ver == u) val[v].push_back(u);
}
}
}
used[1] = 1;
for (auto v : adj[1]) {
if (val[v].size() == 1) {
flag = 1;
auto u = val[v][0];
if (val[u].size() == 2) {
ans[4] = v;
ans[3] = u;
used[v] = 1;
used[u] = 1;
for (auto ver : val[u]) {
if (ver != v) {
if (val[ver].size() == 2) {
ans[1] = ver;
used[ver] = 1;
for (auto v2 : val[ver]) {
if (v2 != u) {
if (val[v2].size() == 1 && val[v2][0] == ver) {
used[v2] = 1;
ans[0] = v2;
} else {
cout << "-1";
flag = 0;
return 0;
}
}
}
} else {
cout << "-1";
flag = 0;
return 0;
}
}
}
} else {
cout << "-1";
flag = 0;
return 0;
}
break;
}
}
if (flag == 0) {
cout << "-1";
return 0;
}
for (i = 5; i < n; i++) {
long long int ctr = 0;
for (auto v : adj[ans[i - 2]]) {
if (used[v] == 0) {
ctr++;
used[v] = 1;
ans[i] = v;
}
}
if (ctr != 1) {
cout << "-1";
flag = 0;
return 0;
}
}
if (flag) {
for (i = 0; i < n; i++) cout << ans[i] << " ";
} else
cout << "-1";
} else {
long long int poss[n];
for (i = 1; i <= n; i++) poss[i - 1] = i;
do {
long long int flag = 1;
for (i = 0; i < n; i++) {
long long int p1 = (i + 1) % n, p2 = (i + 2) % n;
long long int p3 = (i - 1 + n) % n;
long long int p4 = (i - 2 + n) % n;
auto u = poss[i];
long long int ctr = 0;
for (auto v : adj[u]) {
for (j = 0; j < n; j++) {
if (poss[j] == v) {
if (j == p1)
ctr++;
else if (j == p2)
ctr++;
else if (j == p3)
ctr++;
else if (j == p4)
ctr++;
}
}
}
if (ctr != 4) {
flag = 0;
break;
}
}
if (flag) {
for (i = 0; i < n; i++) cout << poss[i] << " ";
return 0;
}
} while (next_permutation(poss, poss + n));
cout << "-1";
}
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long abs1(long long h) {
if (h < 0)
return -h;
else
return h;
}
int main() {
long long i, j, n, m, e, k, ot, x, y, i1, i2, i3, i4;
vector<int> a, q, g;
vector<vector<int> > w;
vector<int> d;
cin >> n;
for (i = 0; i <= n + 4; i++) {
q.push_back(0);
w.push_back(d);
}
q[1] = 1;
q[n + 1] = 1;
for (i = 0; i < 2 * n; i++) {
cin >> x >> y;
w[x].push_back(y);
w[y].push_back(x);
}
for (i = 1; i <= n; i++) {
if (w[i].size() != 4) {
cout << -1;
return 0;
}
}
for (i1 = 1; i1 <= 4; i1++) {
for (i2 = 1; i2 <= 4; i2++) {
for (i3 = 1; i3 <= 4; i3++) {
for (i4 = 1; i4 <= 4; i4++) {
if (i1 != i2 && i1 != i3 && i1 != i4 && i2 != i3 && i2 != i4 &&
i3 != i4) {
g.clear();
for (i = 0; i < n + 2; i++) g.push_back(0);
q[2] = w[1][i1 - 1];
q[3] = w[1][i2 - 1];
q[n] = w[1][i3 - 1];
q[n - 1] = w[1][i4 - 1];
q[0] = q[n];
q[n + 2] = q[2];
int z = 0;
for (i = 2; i <= n - 2; i++) {
e = 0;
k = 0;
for (j = 0; j < 4; j++) {
if (w[q[i]][j] != q[i + 1] && w[q[i]][j] != q[i - 1] &&
w[q[i]][j] != q[i - 2]) {
e++;
k = w[q[i]][j];
}
}
if (e != 1) {
z = 1;
break;
} else
q[i + 2] = k;
}
q[0] = q[n];
q[n + 2] = q[2];
if (z == 0) {
z = 0;
for (i = 2; i <= n; i++) {
e = 0;
for (j = 0; j < 4; j++) {
if (w[q[i]][j] != q[i + 1] && w[q[i]][j] != q[i - 1] &&
w[q[i]][j] != q[i - 2] && w[q[i]][j] != q[i + 2])
e++;
}
if (e != 0) {
z = 1;
break;
}
}
for (i = 1; i <= n; i++) {
g[q[i]]++;
if (g[q[i]] == 2) {
z = 1;
break;
}
}
if (z == 0) {
for (i = 1; i <= n; i++) cout << q[i] << " ";
return 0;
}
}
}
}
}
}
}
cout << -1;
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
set<int> C[100010];
vector<int> G[100010];
int n, per[100010], cnt, vst[100010];
bool dfs(int a, int b, int f) {
per[cnt++] = b;
if (cnt == n + 1) return b == 1;
for (int i = 0; i < 4; i++) {
int u = G[b][i];
if (C[a].count(u) && u != f) {
if (cnt != n && u == 1) continue;
return dfs(b, u, a);
}
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < 2 * n; i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
C[a].insert(b);
C[b].insert(a);
}
for (int i = 1; i <= n; i++)
if ((int)G[i].size() != 4) return puts("-1"), 0;
for (int i = 0; i < 4; i++) {
int u = G[1][i];
for (int j = 0; j < 4; j++) {
per[0] = cnt = 1, per[1] = u;
if (dfs(1, u, G[u][j])) {
bool fl = 0;
for (int k = 0; k < n; k++) {
if (vst[per[k]]) fl = 1;
vst[per[k]] = 1;
}
if (fl) continue;
for (int k = 0; k < n; k++)
printf("%d%c", per[k], k == n - 1 ? '\n' : ' ');
return 0;
}
}
}
puts("-1");
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 |
import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigInteger;
import static java.lang.Math.*;
// Solution is at the bottom of code
public class _C implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
OutputWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new _C(), "", 128 * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeBegin = System.currentTimeMillis();
Locale.setDefault(Locale.US);
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
String delim = " ";
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken(delim);
}
String readLine() throws IOException{
return in.readLine();
}
/////////////////////////////////////////////////////////////////
final char NOT_A_SYMBOL = '\0';
char readChar() throws IOException{
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
}
char[] readCharArray() throws IOException{
return readLine().toCharArray();
}
/////////////////////////////////////////////////////////////////
int readInt() throws IOException{
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException{
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
///////////////////////////////////////////////////////////////////
long readLong() throws IOException{
return Long.parseLong(readString());
}
long[] readLongArray(int size) throws IOException{
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
double[] readDoubleArray(int size) throws IOException{
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
/////////////////////////////////////////////////////////////////////
Point readPoint() throws IOException{
return new Point(readInt(), readInt());
}
Point[] readPointArray(int size) throws IOException{
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
class OutputWriter extends PrintWriter{
final int DEFAULT_PRECISION = 12;
int precision;
String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public int getPrecision() {
return precision;
}
public void setPrecision(int precision) {
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
private String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
public void printWithSpace(double d){
printf(formatWithSpace, d);
}
public void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@Override
public void println(double d){
printlnAll(d);
}
public void printlnAll(double... d){
printAll(d);
println();
}
}
/////////////////////////////////////////////////////////////////////
int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
boolean check(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
int n;
List<Integer>[] graph;
int[] perm;
int[] a;
void solve() throws IOException{
n = readInt();
graph = new List[n];
for (int i = 0; i < n; ++i){
graph[i] = new ArrayList<Integer>();
}
for (int i = 0; i < 2 * n; ++i){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
for (List<Integer> list: graph){
if (list.size() != 4){
out.println(-1);
return;
}
}
perm = new int[4];
for (int i = 0; i < 4; ++i){
perm[i] = i;
}
if (!gen(0, 4)){
out.println(-1);
return;
}
for (int i: a){
out.print((i + 1) + " ");
}
}
void swap(int i, int j){
if (i == j) return;
perm[i] ^= perm[j];
perm[j] ^= perm[i];
perm[i] ^= perm[j];
}
boolean gen(int cur, int len){
if (cur == len){
boolean res = doIt();
return res;
}else{
for (int i = cur; i < len; ++i){
swap(i, cur);
boolean res = gen(cur + 1, len);
swap(cur, i);
if (res) return true;
}
return false;
}
}
boolean doIt(){
a = new int[n];
a[0] = 0;
boolean[] used = new boolean[n];
a[n-2] = graph[0].get(perm[0]);
a[n-1] = graph[0].get(perm[1]);
a[1] = graph[0].get(perm[2]);
a[2] = graph[0].get(perm[3]);
used[a[0]] = used[a[1]] = used[a[2]] = used[a[n-2]] = used[a[n-1]] = true;
for (int i = 3; i < n - 2; ++i){
List<Integer> list = new ArrayList<Integer>();
for (int v: graph[a[i-1]]){
if (graph[a[i-2]].indexOf(v) >= 0 && !used[v]){
list.add(v);
}
}
if (list.size() != 1) return false;
int v = list.get(0);
a[i] = v;
used[v] = true;
}
for (int i = 0; i < n; ++i){
if (!check(i, a)){
return false;
}
}
return true;
}
boolean check(int index, int[] a){
int from = a[index];
for (int i = index - 2; i <= index + 2; ++i){
if (i == index) continue;
int j = (i + n) % n;
if (graph[from].indexOf(a[j]) < 0){
return false;
}
}
return true;
}
}
| JAVA |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, p[100005], ady[100005][4], cnt = 1;
bool used[100005];
bool dfs(int nd, int t) {
used[nd] = true;
if (++cnt == n) {
p[nd] = 1;
return true;
}
for (int i = 0; i < 4; ++i) {
if (!used[ady[nd][i]]) {
p[nd] = ady[nd][i];
for (int j = 0; j < 4; ++j)
if (ady[p[nd]][j] == t && dfs(p[nd], nd)) return true;
}
}
--cnt;
return (used[nd] = false);
}
int main() {
int u, v, t;
bool ok = true, val = false;
scanf("%d", &n);
t = n << 1;
while (t--) {
scanf("%d%d", &u, &v);
ok &= (p[u] < 4 && p[v] < 4);
if (ok) {
ady[u][p[u]++] = v;
ady[v][p[v]++] = u;
}
}
if (ok) {
ok = false;
used[1] = true;
for (int i = 0; i < 4 && !ok; ++i) {
p[1] = ady[1][i];
ok = dfs(ady[1][i], 1);
}
}
if (ok) {
t = 1;
printf("1");
while ((t = p[t]) > 1) printf(" %d", t);
} else {
printf("-1");
}
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<vector<int> > T;
pair<int, int> find_tris(int a, int b) {
int w[2] = {-1, -1}, sz = 0;
for (int x : T[a]) {
for (int y : T[b]) {
if (x == y) {
w[sz++] = y;
break;
}
}
}
return pair<int, int>(w[0], w[1]);
}
bool uneq(int a, int b, int c) {
pair<int, int> p = find_tris(a, b);
if (p.first != -1 && p.first != c) return true;
return p.second != -1 && p.second != c;
}
void reorder(int &a, int &b, int &c) {
bool ab = uneq(a, b, c);
bool ac = uneq(a, c, b);
bool bc = uneq(b, c, a);
if (ab && bc)
return;
else if (ab && ac)
swap(b, a);
else if (bc && ac)
swap(b, c);
}
vector<int> ans;
set<pair<int, int> > edges;
set<pair<int, int> > ed2;
int main() {
int n;
scanf("%d", &n);
if (n == 5) {
printf("1 2 3 4 5\n");
return 0;
}
T = vector<vector<int> >(n);
for (int i = 0; i < 2 * n; ++i) {
int a, b;
scanf("%d%d", &a, &b), --a, --b;
T[a].push_back(b), T[b].push_back(a);
edges.insert(pair<int, int>(min(a, b), max(a, b)));
}
if (T[0].size() == 4) {
int a = 0, b = T[0][0], c = find_tris(a, b).first;
if (c != -1) {
reorder(a, b, c);
ans.push_back(a), ans.push_back(b), ans.push_back(c);
while (ans.size() != n) {
a = ans[ans.size() - 3], b = ans[ans.size() - 2],
c = ans[ans.size() - 1];
pair<int, int> p = find_tris(b, c);
int d = (p.first == a) ? p.second : p.first;
if (d == -1) break;
ans.push_back(d);
}
}
}
for (int i = 0; i < ans.size(); ++i) {
int j = (i + 1) % n;
int k = (i + 2) % n;
ed2.insert(pair<int, int>(min(ans[i], ans[j]), max(ans[i], ans[j])));
ed2.insert(pair<int, int>(min(ans[i], ans[k]), max(ans[i], ans[k])));
}
if (edges != ed2) {
printf("-1\n");
return 0;
}
for (int k : ans) {
printf("%d ", k + 1);
}
printf("\n");
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | import java.io.*;
import java.util.*;
public class C {
static void solve() throws IOException {
int n = nextInt();
int[] a = new int[2 * n];
int[] b = new int[2 * n];
for (int i = 0; i < 2 * n; i++) {
a[i] = nextInt() - 1;
b[i] = nextInt() - 1;
}
int[] answer = solve(a, b, n);
if (answer == null) {
out.println(-1);
} else {
for (int i = 0; i < n; i++) {
if (i > 0) {
out.print(' ');
}
out.print(answer[i] + 1);
}
out.println();
}
}
private static int[] solve(int[] a, int[] b, int n) {
int[] size = new int[n];
int[][] lists = new int[n][4];
for (int i = 0; i < 2 * n; i++) {
if (size[a[i]] == 4 || size[b[i]] == 4) {
return null;
}
lists[a[i]][size[a[i]]++] = b[i];
lists[b[i]][size[b[i]]++] = a[i];
}
for (int secondNumber : lists[0]) {
for (int thirdNumber : lists[0]) {
if (thirdNumber == secondNumber) {
continue;
}
boolean[] used = new boolean[n];
int[] restored = new int[n];
restored[0] = 0;
restored[1] = secondNumber;
restored[2] = thirdNumber;
used[0] = used[secondNumber] = used[thirdNumber] = true;
for (int i = 3; i < n; i++) {
boolean good = false;
for (int value : lists[restored[i - 1]]) {
if (used[value]) {
continue;
}
if (!has(lists[restored[i - 2]], value)) {
continue;
}
good = true;
used[value] = true;
restored[i] = value;
break;
}
if (!good) {
restored = null;
break;
}
}
if (restored != null && check(lists, restored)) {
return restored;
}
}
}
return null;
}
private static boolean check(int[][] lists, int[] restored) {
int n = restored.length;
for (int i = 0; i < n; i++) {
for (int dx = 0; dx < 2; dx++) {
int j = (i + dx + 1) % n;
if (!has(lists[restored[i]], restored[j])) {
return false;
}
}
}
return true;
}
static boolean has(int[] list, int number) {
for (int i : list) {
if (i == number) {
return true;
}
}
return false;
}
static BufferedReader br;
static PrintWriter out;
static StringTokenizer st;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
| JAVA |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int n_ = 1e5 + 5;
int n, taken[n_];
vector<int> adj[n_], ans;
bool ok() {
int a = 1, b = 2, f;
for (int i = 3; i < n; i++, a++, b++) {
f = 0;
for (int x : adj[ans[a]]) {
if (taken[x]) continue;
if (binary_search(adj[ans[b]].begin(), adj[ans[b]].end(), x)) {
f = 1;
ans.push_back(x);
taken[x] = 1;
break;
}
}
if (!f) return 0;
}
return binary_search(adj[1].begin(), adj[1].end(), ans.back()) &&
binary_search(adj[ans[1]].begin(), adj[ans[1]].end(), ans.back());
}
int main() {
scanf("%d", &n);
int u, v;
for (int i = 0; i < (n << 1); i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (adj[i].size() != 4) {
puts("-1");
return 0;
}
sort(adj[i].begin(), adj[i].end());
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) continue;
if (binary_search(adj[adj[1][j]].begin(), adj[adj[1][j]].end(),
adj[1][i])) {
ans.clear();
memset(taken, 0, sizeof taken);
ans.push_back(1);
ans.push_back(adj[1][i]);
ans.push_back(adj[1][j]);
taken[1] = taken[ans[1]] = taken[ans[2]] = 1;
if (ok()) {
for (int x : ans) printf("%d ", x);
return 0;
}
}
}
}
puts("-1");
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Locale;
import java.io.OutputStream;
import java.util.RandomAccess;
import java.io.PrintWriter;
import java.util.AbstractList;
import java.io.Writer;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.math.BigDecimal;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Jacob Jiang
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
//noinspection unchecked
List<Integer>[] map = new List[n];
for (int i = 0; i < n; i++) {
map[i] = new ArrayList<Integer>(4);
}
for (int i = 0; i < 2 * n; i++) {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
map[a].add(b);
map[b].add(a);
}
for (int i = 0; i < n; i++) {
if (map[i].size() != 4) {
out.println(-1);
return;
}
Collections.sort(map[i]);
}
if (n <= 6) {
int[] p = CombinatoricsUtils.originPermutation(n);
tag:
do {
for (int i = 0; i < n; i++) {
if (!map[p[i]].contains(p[(i + 1) % n]) || !map[p[i]].contains(p[(i + 2) % n]))
continue tag;
}
ArrayUtils.increaseByOne(p);
out.printLine(ArrayUtils.asList(p).toArray());
return;
} while (CombinatoricsUtils.nextPermutation(p));
out.println(-1);
return;
}
int[] answer = new int[n];
Arrays.fill(answer, -1);
answer[0] = 0;
{
int[] ct = new int[4];
for (int t : map[0]) {
for (int c : map[t]) {
if (map[0].contains(c)) {
ct[map[0].indexOf(c)]++;
}
}
}
int[] a = ct.clone();
Arrays.sort(a);
if (!Arrays.equals(a, new int[]{1, 1, 2, 2})) {
out.println(-1);
return;
}
for (int i = 0; i < 4; i++) {
if (ct[i] == 2) {
answer[1] = map[0].get(i);
for (int c : map[0]) {
if (ct[map[0].indexOf(c)] == 1 && map[answer[1]].contains(c)) {
answer[2] = c;
break;
}
}
break;
}
}
if (answer[1] == -1 || answer[2] == -1) {
out.println(-1);
return;
}
}
boolean[] visited = new boolean[n];
for (int i = 0; i < 3; i++) {
visited[answer[i]] = true;
}
for (int i = 3; i < n - 1; i++) {
List<Integer> cur = new ArrayList<Integer>(4);
for (int c : map[answer[i - 1]]) {
if (!visited[c]) {
cur.add(c);
}
}
for (int c : cur) {
if (map[answer[i - 2]].contains(c)) {
answer[i] = c;
visited[c] = true;
break;
}
}
if (answer[i] == -1) {
out.println(-1);
return;
}
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
answer[n - 1] = i;
break;
}
}
for (int i = 0; i < n; i++) {
if (!map[answer[i]].contains(answer[(i + 1) % n]) || !map[answer[i]].contains(answer[(i + 2) % n])) {
out.println(-1);
return;
}
}
ArrayUtils.increaseByOne(answer);
out.printLine(ArrayUtils.asList(answer).toArray());
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(OutputStream stream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(int i) {
writer.println(i);
}
public void print(Object obj) {
writer.print(obj);
}
public void println() {
writer.println();
}
public void print(char c) {
writer.print(c);
}
public void close() {
writer.close();
}
public void printItems(Object... items) {
for (int i = 0; i < items.length; i++) {
if (i != 0) {
print(' ');
}
print(items[i]);
}
}
public void printLine(Object... items) {
printItems(items);
println();
}
}
class CombinatoricsUtils {
public static boolean nextPermutation(int[] permutation) {
int position = -1;
for (int i = permutation.length - 2; i >= 0; i--) {
if (permutation[i] < permutation[i + 1]) {
position = i;
break;
}
}
if (position == -1) {
return false;
}
int newPosition = -1;
for (int i = permutation.length - 1; i >= 0; i--) {
if (permutation[i] > permutation[position]) {
newPosition = i;
break;
}
}
// swap variables
{
int swapTempValue = permutation[position];
permutation[position] = permutation[newPosition];
permutation[newPosition] = swapTempValue;
}
int a = position + 1, b = permutation.length - 1;
while (a < b) {
// swap variables
{
int swapTempValue = permutation[a];
permutation[a] = permutation[b];
permutation[b] = swapTempValue;
}
a++;
b--;
}
return true;
}
public static int[] originPermutation(int size) {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = i;
}
return result;
}
}
class ArrayUtils {
public static void increaseByOne(int[]... arrays) {
for (int[] array : arrays) {
for (int i = 0; i < array.length; i++) {
array[i]++;
}
}
}
public static List<Integer> asList(int[] array) {
return new IntList(array);
}
private static class IntList extends AbstractList<Integer> implements RandomAccess {
int[] array;
private IntList(int[] array) {
this.array = array;
}
public Integer get(int index) {
return array[index];
}
public Integer set(int index, Integer element) {
int result = array[index];
array[index] = element;
return result;
}
public int size() {
return array.length;
}
}
}
| JAVA |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:666000000")
using namespace std;
const int INF = (1 << 30) - 1;
const long double EPS = 1e-9;
void ML(const bool v) {
if (v) return;
int *ass;
for (;;) {
ass = new int[2500000];
for (int i = 0; i < 2500000; i++) ass[i] = rand();
}
}
void TL(const bool v) {
if (v) return;
for (;;) cout << rand() % (rand() % 1000 + 1) << endl;
}
void PE(const bool v) {
if (v) return;
for (int i = 0; i < 10000; i++) printf("%c", rand() % 256);
exit(0);
}
int n;
vector<vector<int> > a;
vector<pair<int, int> > pairs;
bool LoAd() {
if (!(1 == scanf("%d", &n))) return false;
a.clear();
a.resize(n + 1);
pairs.clear();
for (int u, v, i = 0; i < 2 * n; i++) {
(2 == scanf("%d%d", &u, &v));
pairs.push_back(make_pair(u, v));
a[u].push_back(v);
a[v].push_back(u);
}
return true;
}
vector<int> res;
bool was[100500];
bool check(int u, int v) {
if (u < 0 || v < 0) return true;
u = res[u];
v = res[v];
for (auto it = a[u].begin(); it != a[u].end(); it++)
if (*it == v) return true;
return false;
}
bool go(const int v, const int x) {
res[v] = x;
if (!check(v - 2, v)) return false;
if (!check(v - 1, v)) return false;
if (v == (int)res.size() - 1) {
return true;
}
if (v < n) {
if (was[x]) return false;
was[x] = true;
}
if (v + 1 >= n) return go(v + 1, res[v - n + 1]);
for (auto it = a[x].begin(); it != a[x].end(); it++)
if (!was[*it] && go(v + 1, *it)) return true;
if (v < n) {
was[x] = false;
}
return false;
}
void SoLvE() {
for (int i = 1; i < (int)a.size(); i++) {
if (4 != (int)a[i].size()) {
puts("-1");
return;
}
}
res.clear();
res.resize(n + 3);
memset(was, false, sizeof(was));
if (!go(0, 1)) {
puts("-1");
return;
}
for (int i = 0; i < n; i++) printf("%d ", res[i]);
puts("");
vector<int> cur;
}
void gen() {
for (n = 100000; n <= 100000; n++) {
printf("%d\n", n);
for (int i = 0; i < n; i++) {
printf("%d %d\n", i + 1, (i + 1) % n + 1);
printf("%d %d\n", i + 1, (i + 2) % n + 1);
}
puts("");
}
exit(0);
}
int main() {
srand((int)time(NULL));
LoAd();
SoLvE();
return 0;
}
| CPP |
263_C. Circle of Numbers | One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.
For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).
Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
Input
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.
It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
Output
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n.
If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
Examples
Input
5
1 2
2 3
3 4
4 5
5 1
1 3
2 4
3 5
4 1
5 2
Output
1 2 3 4 5
Input
6
5 6
4 3
5 3
2 4
6 1
3 1
6 2
2 5
1 4
3 6
1 2
4 5
Output
1 2 4 5 3 6 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
vector<int> adj[200011];
map<pair<int, int>, int> vst;
int sum[200011];
int ans[200011];
int cnt[200011];
int n;
int ok() {
memset(cnt, 0, sizeof cnt);
for (int i = 0; i < n; i++) {
if (ans[i] > n or ans[i] < 0) return 0;
cnt[ans[i]]++;
if (cnt[ans[i]] > 1) return 0;
}
for (int i = 0; i < n; i++) {
if (vst[pair<int, int>(ans[i], ans[(i + 1) % n])] == 0) return 0;
if (vst[pair<int, int>(ans[i], ans[(i + 2) % n])] == 0) return 0;
if (vst[pair<int, int>(ans[i], ans[(i - 1 + n) % n])] == 0) return 0;
if (vst[pair<int, int>(ans[i], ans[(i - 2 + n) % n])] == 0) return 0;
}
return 1;
}
void solve(int i, int j) {
ans[1] = 1;
ans[2] = adj[1][i];
ans[0] = adj[1][j];
for (int k = 0; k < 4; k++)
if (k != i and k != j) {
ans[3] = adj[1][k];
if (vst[pair<int, int>(ans[3], ans[2])]) break;
}
for (int i = 4; i < n; i++) {
ans[i] = sum[ans[i - 2]] - ans[i - 4] - ans[i - 1] - ans[i - 3];
if (ans[i] < 0) return;
}
if (ok()) {
for (int i = 0; i < n; i++) printf("%d ", ans[i]);
puts("");
exit(0);
}
}
int main() {
cin >> n;
for (int i = 0; i < 2 * n; i++) {
int u, v;
gn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
sum[u] += v;
sum[v] += u;
vst[pair<int, int>(u, v)] = vst[pair<int, int>(v, u)] = 1;
}
for (int i = 1; i <= n; i++) {
if (((int)adj[i].size()) != 4) return puts("-1");
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
if (i != j) {
solve(i, j);
}
}
puts("-1");
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.