提出 #58961442
ソースコード 拡げる
#include <bits/stdc++.h>
#include <cstdint>
using namespace std;
// #define int long long
using ll = long long;
using P = pair<int, int>;
template<class T> using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
template<class T> using max_heap = priority_queue<T, std::vector<T>>;
#define LOG(variable) cerr << #variable":\t" << (variable) << "\n"
#define LOGCON(i, container) for(size_t (i) = 0; (i) < (container).size(); ++(i)) cerr << (i) << ":\t" << (container)[(i)] << "\n"
#define REP(i, n) for (long long i = 0; i < (n); ++i)
#define REPS(i, r, n) for (long long i = (r); i < (n); ++i)
#define REPR(i, n) for(long long i = (n); i >= 0; --i) // from n to 0
#define REPRS(i, n, r) for(long long i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj) for(auto itr = (obj).begin(); itr != (obj).end() ; ++itr)
#define REPROBJ(itr, obj) for(auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
template<class T = int> T in() {T a; cin >> a; return a;}
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// dis_map has to be empty
void bfs(unordered_map<int, vector<int>> &edges, unordered_map<int, int> &dis_map) {
queue<P> que;
que.push(P(1, 0));
dis_map[1] = 0;
while (que.size()) {
int curr, dis;
tie(curr, dis) = que.front(); que.pop();
if (edges.find(curr) == edges.end()) continue;
REPOBJ(next_itr, edges[curr]) {
if (dis_map.find(*next_itr) != dis_map.end()) continue;
dis_map[*next_itr] = dis + 1;
que.push(P(*next_itr, dis + 1));
}
}
}
#define INF 20000000
/***** MAIN *****/
signed main() {
int N, M; cin >> N >> M;
unordered_map<int, vector<int>> edges, r_edges;
REP(i, M) {
int a, b; cin >> a >> b;
if (edges.find(a) == edges.end()) edges[a] = vector<int>();
edges[a].emplace_back(b);
if (r_edges.find(b) == r_edges.end()) r_edges[b] = vector<int>();
r_edges[b].emplace_back(a);
}
unordered_map<int, int> dis_from_f, dis_to_f;
bfs(edges, dis_from_f);
bfs(r_edges, dis_to_f);
int ans = INF;
REPS(i, 2, N + 1) {
// Unreachable from 1
if (dis_from_f.find(i) == dis_from_f.end()) continue;
// Unreachable to 1
if (dis_to_f.find(i) == dis_to_f.end()) continue;
chmin(ans, dis_from_f[i] + dis_to_f[i]);
}
if (ans == INF) {
cout << -1;
} else {
cout << ans;
}
cout << "\n";
return 0;
}
/***** MAIN *****/
提出情報
| 提出日時 |
|
| 問題 |
D - Cycle |
| ユーザ |
k1832 |
| 言語 |
C++ 23 (gcc 12.2) |
| 得点 |
400 |
| コード長 |
3125 Byte |
| 結果 |
AC |
| 実行時間 |
310 ms |
| メモリ |
58524 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
400 / 400 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 02_cycle_00.txt, 02_cycle_01.txt, 03_path_00.txt, 03_path_01.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
1 ms |
3408 KiB |
| 00_sample_01.txt |
AC |
1 ms |
3488 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3536 KiB |
| 01_random_00.txt |
AC |
148 ms |
25580 KiB |
| 01_random_01.txt |
AC |
63 ms |
6372 KiB |
| 01_random_02.txt |
AC |
126 ms |
23868 KiB |
| 01_random_03.txt |
AC |
57 ms |
7116 KiB |
| 01_random_04.txt |
AC |
244 ms |
37208 KiB |
| 01_random_05.txt |
AC |
103 ms |
9268 KiB |
| 01_random_06.txt |
AC |
107 ms |
21920 KiB |
| 01_random_07.txt |
AC |
100 ms |
19720 KiB |
| 01_random_08.txt |
AC |
191 ms |
30392 KiB |
| 01_random_09.txt |
AC |
85 ms |
8036 KiB |
| 01_random_10.txt |
AC |
170 ms |
28916 KiB |
| 01_random_11.txt |
AC |
77 ms |
8268 KiB |
| 01_random_12.txt |
AC |
149 ms |
25720 KiB |
| 01_random_13.txt |
AC |
58 ms |
6156 KiB |
| 01_random_14.txt |
AC |
120 ms |
22740 KiB |
| 01_random_15.txt |
AC |
77 ms |
7312 KiB |
| 01_random_16.txt |
AC |
199 ms |
31220 KiB |
| 01_random_17.txt |
AC |
49 ms |
6376 KiB |
| 01_random_18.txt |
AC |
133 ms |
24504 KiB |
| 01_random_19.txt |
AC |
52 ms |
5988 KiB |
| 01_random_20.txt |
AC |
220 ms |
35948 KiB |
| 01_random_21.txt |
AC |
168 ms |
25720 KiB |
| 01_random_22.txt |
AC |
163 ms |
31448 KiB |
| 01_random_23.txt |
AC |
42 ms |
5900 KiB |
| 01_random_24.txt |
AC |
149 ms |
25576 KiB |
| 01_random_25.txt |
AC |
147 ms |
19080 KiB |
| 01_random_26.txt |
AC |
183 ms |
31092 KiB |
| 01_random_27.txt |
AC |
95 ms |
9208 KiB |
| 01_random_28.txt |
AC |
201 ms |
32536 KiB |
| 01_random_29.txt |
AC |
112 ms |
11080 KiB |
| 01_random_30.txt |
AC |
95 ms |
20424 KiB |
| 01_random_31.txt |
AC |
61 ms |
6988 KiB |
| 02_cycle_00.txt |
AC |
239 ms |
58524 KiB |
| 02_cycle_01.txt |
AC |
310 ms |
58488 KiB |
| 03_path_00.txt |
AC |
134 ms |
49432 KiB |
| 03_path_01.txt |
AC |
195 ms |
48756 KiB |