結果
問題 | No.1865 Make Cycle |
ユーザー | US_cube |
提出日時 | 2022-03-04 22:30:58 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 321 ms / 3,000 ms |
コード長 | 2,743 bytes |
コンパイル時間 | 1,488 ms |
コンパイル使用メモリ | 127,088 KB |
実行使用メモリ | 15,612 KB |
最終ジャッジ日時 | 2023-09-26 01:38:50 |
合計ジャッジ時間 | 6,449 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 176 ms
11,872 KB |
testcase_01 | AC | 107 ms
9,708 KB |
testcase_02 | AC | 207 ms
12,116 KB |
testcase_03 | AC | 68 ms
10,508 KB |
testcase_04 | AC | 130 ms
12,224 KB |
testcase_05 | AC | 181 ms
12,824 KB |
testcase_06 | AC | 170 ms
11,888 KB |
testcase_07 | AC | 165 ms
11,576 KB |
testcase_08 | AC | 217 ms
14,104 KB |
testcase_09 | AC | 179 ms
12,576 KB |
testcase_10 | AC | 196 ms
13,272 KB |
testcase_11 | AC | 197 ms
12,952 KB |
testcase_12 | AC | 154 ms
11,584 KB |
testcase_13 | AC | 177 ms
11,820 KB |
testcase_14 | AC | 121 ms
10,404 KB |
testcase_15 | AC | 221 ms
12,960 KB |
testcase_16 | AC | 239 ms
13,804 KB |
testcase_17 | AC | 183 ms
10,952 KB |
testcase_18 | AC | 189 ms
13,172 KB |
testcase_19 | AC | 321 ms
15,612 KB |
testcase_20 | AC | 2 ms
4,380 KB |
testcase_21 | AC | 1 ms
4,380 KB |
testcase_22 | AC | 2 ms
4,376 KB |
testcase_23 | AC | 2 ms
4,376 KB |
ソースコード
#include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <climits> #include <functional> #include <cassert> #include <numeric> #define rep(i, n) for(int i = 0; i < (n); i++) #define per(i, n) for(int i = (n) - 1; i >= 0; i--) using ll = long long; #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mod 1000000007 using namespace std; template<class T, class U> bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; } template<class T, class U> bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; } template <class T> struct Edge { int from,to; T cost; int idx; Edge(){}; Edge(int f, int t, T c=1, int i=-1) : from(f), to(t), cost(c), idx(i){} Edge(int t) : to(t), from(-1), cost(1), idx(-1){} operator int() const{ return to; } bool operator<(const Edge &e){ return cost < e.cost; } }; template <class T> struct Graph : vector<vector<Edge<T>>> { Graph(){} Graph(const int &n) : vector<vector<Edge<T>>>(n){} void add_edge(int a, int b, T c=1, int i=-1){ (*this)[a].push_back({ a, b, c, i }); } }; using graph = Graph<int>; namespace cycle { graph g; vector<int> used; vector<Edge<int>> prev, cyc; bool dfs(int pos){ used[pos] = 1; for(auto &x : g[pos]){ if(!used[x]){ prev[x] = x; if(dfs(x)) return true; }else if(used[x] == 1){ int cur = pos; cyc.push_back(x); while(cur != x){ cyc.push_back(prev[cur]); cur = prev[cur].from; } return true; } } used[pos] = 2; return false; } vector<Edge<int>> detect(const graph &root){ g = root; int n = g.size(); used.assign(n, 0); prev.assign(n, -1); for(int i = 0; i < n; i++){ if(!used[i] && dfs(i)){ reverse(cyc.begin(), cyc.end()); return cyc; } } return {}; } }; int main(){ int n,q; cin >> n >> q; vector<Edge<int>> es(q); rep(i, q){ cin >> es[i].from >> es[i].to; es[i].from--; es[i].to--; } int left = 0, right = q; while(left + 1 < right){ int mid = (left + right) / 2; graph root(n); rep(i, mid) root.add_edge(es[i].from, es[i].to); auto res = cycle::detect(root); if(res.empty()) left = mid; else right = mid; } if(right == q) cout << "-1\n"; else cout << right << "\n"; }