結果
問題 | No.1023 Cyclic Tour |
ユーザー | Haar |
提出日時 | 2020-04-23 11:56:47 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 75 ms / 2,000 ms |
コード長 | 5,769 bytes |
コンパイル時間 | 2,600 ms |
コンパイル使用メモリ | 215,084 KB |
実行使用メモリ | 13,988 KB |
最終ジャッジ日時 | 2024-10-13 09:39:06 |
合計ジャッジ時間 | 7,647 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 3 ms
6,816 KB |
testcase_03 | AC | 3 ms
6,816 KB |
testcase_04 | AC | 37 ms
6,912 KB |
testcase_05 | AC | 30 ms
6,816 KB |
testcase_06 | AC | 32 ms
7,040 KB |
testcase_07 | AC | 31 ms
6,912 KB |
testcase_08 | AC | 38 ms
9,036 KB |
testcase_09 | AC | 47 ms
8,916 KB |
testcase_10 | AC | 38 ms
8,960 KB |
testcase_11 | AC | 44 ms
9,364 KB |
testcase_12 | AC | 42 ms
10,072 KB |
testcase_13 | AC | 40 ms
9,488 KB |
testcase_14 | AC | 34 ms
9,568 KB |
testcase_15 | AC | 38 ms
9,756 KB |
testcase_16 | AC | 62 ms
12,312 KB |
testcase_17 | AC | 66 ms
12,384 KB |
testcase_18 | AC | 66 ms
12,260 KB |
testcase_19 | AC | 65 ms
12,028 KB |
testcase_20 | AC | 56 ms
9,612 KB |
testcase_21 | AC | 57 ms
9,732 KB |
testcase_22 | AC | 67 ms
10,332 KB |
testcase_23 | AC | 69 ms
10,544 KB |
testcase_24 | AC | 75 ms
11,812 KB |
testcase_25 | AC | 58 ms
9,516 KB |
testcase_26 | AC | 56 ms
9,812 KB |
testcase_27 | AC | 55 ms
9,172 KB |
testcase_28 | AC | 70 ms
11,544 KB |
testcase_29 | AC | 66 ms
12,052 KB |
testcase_30 | AC | 69 ms
11,556 KB |
testcase_31 | AC | 63 ms
11,288 KB |
testcase_32 | AC | 69 ms
12,308 KB |
testcase_33 | AC | 72 ms
11,680 KB |
testcase_34 | AC | 62 ms
9,948 KB |
testcase_35 | AC | 59 ms
9,668 KB |
testcase_36 | AC | 59 ms
9,904 KB |
testcase_37 | AC | 63 ms
11,176 KB |
testcase_38 | AC | 64 ms
11,784 KB |
testcase_39 | AC | 67 ms
10,736 KB |
testcase_40 | AC | 63 ms
10,868 KB |
testcase_41 | AC | 63 ms
10,872 KB |
testcase_42 | AC | 54 ms
9,532 KB |
testcase_43 | AC | 65 ms
11,624 KB |
testcase_44 | AC | 30 ms
6,912 KB |
testcase_45 | AC | 47 ms
13,988 KB |
testcase_46 | AC | 43 ms
13,988 KB |
testcase_47 | AC | 29 ms
7,040 KB |
testcase_48 | AC | 55 ms
9,764 KB |
testcase_49 | AC | 54 ms
9,760 KB |
testcase_50 | AC | 54 ms
9,828 KB |
testcase_51 | AC | 55 ms
9,796 KB |
testcase_52 | AC | 55 ms
9,696 KB |
ソースコード
#include <bits/stdc++.h> using i32 = int_fast32_t; using i64 = int_fast64_t; using u32 = uint_fast32_t; using u64 = uint_fast64_t; using f64 = double; using f80 = long double; #define FOR(v, a, b) for(i64 v = (a); v < (b); ++v) #define FORE(v, a, b) for(i64 v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for(i64 v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c,x) ((c).find(x) != (c).end()) #define fst first #define snd second #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL<<(i)) #ifdef DEBUG #include <Mylib/Debug/debug.cpp> #else #define dump(...) ((void)0) #endif using namespace std; template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;} template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;} template <typename T> void pout(const T &value){std::cout << value << "\n";} template <typename T, typename ...Args> void pout(const T &value, const Args&... args){std::cout << value << " ";pout(args...);} template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);} template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);} template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);} template <typename T> auto make_vector(int n, int m, const T &value){return vector<vector<T>>(n, vector<T>(m, value));} struct Init{ Init(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } }init; template <typename Cost = int> class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} Edge rev() const {return Edge(to,from,cost);} friend std::ostream& operator<<(std::ostream &os, const Edge &e){ os << "(FROM: " << e.from << "," << "TO: " << e.to << "," << "COST: " << e.cost << ")"; return os; } }; template <typename T> using Graph = std::vector<std::vector<Edge<T>>>; template <typename T> using Tree = std::vector<std::vector<Edge<T>>>; template <typename C, typename T> void add_edge(C &g, int from, int to, T w){ g[from].emplace_back(from, to, w); } template <typename C, typename T> void add_undirected(C &g, int a, int b, T w){ add_edge(g, a, b, w); add_edge(g, b, a, w); } class UnionFind{ std::vector<int> parent, depth, size; int count; public: UnionFind(int n): parent(n), depth(n,1), size(n,1), count(n){ std::iota(parent.begin(), parent.end(), 0); } inline int get_root(int i){ if(parent[i] == i) return i; else return parent[i] = get_root(parent[i]); } inline bool is_same(int i, int j){return get_root(i) == get_root(j);} inline int merge(int i, int j){ int ri = get_root(i), rj = get_root(j); if(ri == rj) return ri; else{ --count; if(depth[ri] < depth[rj]){ parent[ri] = rj; size[rj] += size[ri]; return rj; }else{ parent[rj] = ri; size[ri] += size[rj]; if(depth[ri] == depth[rj]) ++depth[ri]; return ri; } } } inline int get_size(int i){return size[get_root(i)];} inline int count_group(){return count;} }; template <typename T> struct SCC{ std::vector<int> result; int scc_size; private: std::vector<bool> visit; std::vector<int> check; void dfs(int cur, const Graph<T> &graph){ visit[cur] = true; for(auto &e : graph[cur]){ if(not visit[e.to]) dfs(e.to,graph); } check.push_back(cur); } void rdfs(int cur, int i, const Graph<T> &rgraph){ result[cur] = i; for(auto &e : rgraph[cur]){ if(result[e.to] == -1) rdfs(e.to,i,rgraph); } } public: SCC(const Graph<T> &graph){ const int n = graph.size(); result.assign(n, -1); visit.assign(n, false); for(int i = 0; i < n; ++i) if(!visit[i]) dfs(i,graph); std::reverse(check.begin(), check.end()); Graph<T> rgraph(n); for(int i = 0; i < n; ++i) for(auto &e : graph[i]) rgraph[e.to].push_back(e.rev()); int i = 0; for(auto c : check) if(result[c] == -1) {rdfs(c,i,rgraph); ++i;} scc_size = i; } inline const int operator[](int i) const {return result[i];} }; template <typename T> bool detect_cycle(const Graph<T> &g){ const int N = g.size(); std::vector<int> check(N); auto dfs = [&](auto &dfs, int cur) -> bool{ if(check[cur] != 0) return false; check[cur] = 2; for(auto &e : g[cur]){ if(check[e.to] == 2) return true; if(check[e.to] == 0){ if(dfs(dfs, e.to)) return true; } } check[cur] = 1; return false; }; for(int i = 0; i < N; ++i){ if(check[i] == 0 and dfs(dfs, i)) return true; } return false; } int main(){ int N, M; while(cin >> N >> M){ Graph<int> g(N); UnionFind uf(N); bool ans = false; vector<pair<int,int>> de; REP(i,M){ int a, b, c; cin >> a >> b >> c; --a, --b; if(c == 1){ if(uf.is_same(a, b)) ans = true; uf.merge(a, b); }else{ de.emplace_back(a, b); } } for(auto &[a,b] : de){ add_edge(g, uf.get_root(a), uf.get_root(b), 1); } if(detect_cycle(g)) ans = true; pout(ans ? "Yes" : "No"); } return 0; }