1+ /* *
2+ * @file 2122D.cpp
3+ * @author Macesuted (i@macesuted.moe)
4+ * @date 2025-07-19
5+ *
6+ * @copyright Copyright (c) 2025
7+ *
8+ */
9+
10+ #include < bits/stdc++.h>
11+ using namespace std ;
12+
13+ #ifndef LOCAL
14+ #define endl ' \n '
15+ #endif
16+
17+ bool mem1;
18+
19+ #define maxn 5005
20+
21+ vector<vector<int >> graph;
22+ bool vis[2 ][maxn];
23+ int wait[2 ][maxn], ptr[maxn];
24+
25+ void solve (void ) {
26+ int n, m;
27+ cin >> n >> m;
28+ graph.clear (), graph.resize (n + 1 );
29+ for (int i = 1 , x, y; i <= m; i++) cin >> x >> y, graph[x].push_back (y), graph[y].push_back (x);
30+ for (int i = 1 ; i <= n; i++) vis[0 ][i] = false , wait[0 ][i] = 1e8 , ptr[i] = 0 ;
31+ vis[0 ][1 ] = true , wait[0 ][1 ] = 0 ;
32+ for (int t = 0 ;; t++) {
33+ int x = t & 1 ;
34+ if (vis[x][n]) {
35+ cout << t << ' ' << wait[x][n] << endl;
36+ return ;
37+ }
38+ for (int i = 1 ; i <= n; i++) vis[!x][i] = false , wait[!x][i] = 1e8 ;
39+ for (int i = 1 ; i <= n; i++) {
40+ if (!vis[x][i]) continue ;
41+ vis[!x][i] = true , wait[!x][i] = min (wait[!x][i], wait[x][i] + 1 );
42+ int j = graph[i][ptr[i]];
43+ vis[!x][j] = true , wait[!x][j] = min (wait[!x][j], wait[x][i]);
44+ }
45+ for (int i = 1 ; i <= n; i++)
46+ if (++ptr[i] == (int )graph[i].size ()) ptr[i] = 0 ;
47+ }
48+ return ;
49+ }
50+
51+ bool mem2;
52+
53+ int main () {
54+ ios::sync_with_stdio (false ), cin.tie (nullptr );
55+ #ifdef LOCAL
56+ cerr << " Memory Cost: " << abs (&mem1 - &mem2) / 1024 . / 1024 . << " MB" << endl;
57+ #endif
58+
59+ int _ = 1 ;
60+ cin >> _;
61+ while (_--) solve ();
62+
63+ #ifdef LOCAL
64+ cerr << " Time Cost: " << clock () * 1000 . / CLOCKS_PER_SEC << " MS" << endl;
65+ #endif
66+ return 0 ;
67+ }
0 commit comments