結果
問題 |
No.1194 Replace
|
ユーザー |
|
提出日時 | 2020-08-22 17:58:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 966 bytes |
コンパイル時間 | 1,662 ms |
コンパイル使用メモリ | 170,156 KB |
実行使用メモリ | 818,176 KB |
最終ジャッジ日時 | 2024-10-15 11:52:01 |
合計ジャッジ時間 | 7,326 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | RE * 13 MLE * 1 -- * 13 |
ソースコード
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int N, M; vector<vector<int>> edge(200001); vector<int> f(200001, -1); int solve(int x) { //cout << "solve(" << x << ")\n"; if (f[x] != -1) return f[x]; if (x == N) { f[x] = x; return x; } int ret = x; rep(i, edge[x].size()) { ret = max(ret, solve(edge[x][i])); //cout << ret << "!\n"; } f[x] = ret; return ret; } int main() { int B, C; cin >> N >> M; rep(i, M) { cin >> B >> C; edge[B].push_back(C); } /* rep(i, N + 1) { cout << i << " " << edge[i].size() << "\n"; rep(j, edge[i].size()) { cout << edge[i][j] << " "; } cout << "\n"; } */ ll ans = 0; rep(i, N) { ans += solve(i + 1); //cout << i + 1 << " " << solve(i + 1) << "\n"; } cout << ans << "\n"; }