//#define NDEBUG #include #include #include #include #include #include namespace n91 { using i8 = std::int_fast8_t; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; using u8 = std::uint_fast8_t; using u32 = std::uint_fast32_t; using u64 = std::uint_fast64_t; using isize = std::ptrdiff_t; using usize = std::size_t; struct rep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr rep(const usize f, const usize l) noexcept : f(std::min(f, l)), l(l) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; struct revrep { struct itr { usize i; constexpr itr(const usize i) noexcept : i(i) {} void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; const itr f, l; constexpr revrep(const usize f, const usize l) noexcept : f(l - 1), l(std::min(f, l) - 1) {} constexpr auto begin() const noexcept { return f; } constexpr auto end() const noexcept { return l; } }; template auto md_vec(const usize n, const T &value) { return std::vector(n, value); } template auto md_vec(const usize n, Args... args) { return std::vector(n, md_vec(args...)); } template constexpr T difference(const T &a, const T &b) noexcept { return a < b ? b - a : a - b; } template void chmin(T &a, const T &b) noexcept { if (b < a) a = b; } template void chmax(T &a, const T &b) noexcept { if (a < b) a = b; } template class rec_lambda { F f; public: rec_lambda(F &&f) : f(std::move(f)) {} template auto operator()(Args &&... args) const { return f(*this, std::forward(args)...); } }; template auto make_rec(F &&f) { return rec_lambda(std::move(f)); } template T scan() { T ret; std::cin >> ret; return ret; } constexpr char eoln = '\n'; } // namespace n91 namespace ei1333 { using namespace std; template struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template using Edges = vector>; template using WeightedGraph = vector>; using UnWeightedGraph = vector>; template using Matrix = vector>; template struct StronglyConnectedComponents { const G &g; UnWeightedGraph gg, rg; vector comp, order, used; StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) { for (int i = 0; i < g.size(); i++) { for (auto e : g[i]) { gg[i].emplace_back((int)e); rg[(int)e].emplace_back(i); } } } int operator[](int k) { return comp[k]; } void dfs(int idx) { if (used[idx]) return; used[idx] = true; for (int to : gg[idx]) dfs(to); order.push_back(idx); } void rdfs(int idx, int cnt) { if (comp[idx] != -1) return; comp[idx] = cnt; for (int to : rg[idx]) rdfs(to, cnt); } void build(UnWeightedGraph &t) { for (int i = 0; i < gg.size(); i++) dfs(i); reverse(begin(order), end(order)); int ptr = 0; for (int i : order) if (comp[i] == -1) rdfs(i, ptr), ptr++; t.resize(ptr); for (int i = 0; i < g.size(); i++) { for (auto &to : g[i]) { int x = comp[i], y = comp[to]; if (x == y) continue; t[x].push_back(y); } } } }; } // namespace ei1333 #include #include #include #include namespace n91 { class incremental_connectivity { protected: class node_type; public: using container_type = std::vector; using size_type = typename container_type::size_type; class connected_component { friend incremental_connectivity; const node_type &root; constexpr connected_component(const node_type &root) noexcept : root(root) {} public: constexpr size_type representative() const noexcept { return root.parent; } constexpr size_type size() const noexcept { return root.size; } constexpr bool operator==(const connected_component &rhs) const noexcept { return &root == &rhs.root; } constexpr bool operator!=(const connected_component &rhs) const noexcept { return &root != &rhs.root; } }; protected: class node_type { public: size_type parent, size; }; container_type tree; public: incremental_connectivity() : tree() {} explicit incremental_connectivity(const size_type size) : tree(size, {0, 1}) { for (size_type i = 0; i < size; ++i) tree[i].parent = i; } bool empty() const { return tree.empty(); } size_type size() const { return tree.size(); } connected_component find_cc(size_type x) { while (tree[x].parent != x) { x = tree[x].parent = tree[tree[x].parent].parent; } return connected_component(tree[x]); } std::pair unite(size_type x, size_type y) { assert(x < size()); assert(y < size()); x = find_cc(x).representative(); y = find_cc(y).representative(); if (x != y) { if (tree[x].size < tree[y].size) std::swap(x, y); tree[x].size += tree[y].size; tree[y].parent = x; } return {x, y}; } }; } // namespace n91 #include #include namespace n91 { void main_() { /* std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //*/ const usize n = scan(); const usize m = scan(); incremental_connectivity ic(n); ei1333::UnWeightedGraph g(n), buff; usize ccn = n; for (const usize i : rep(0, m)) { const usize a = scan() - 1; const usize b = scan() - 1; switch (scan()) { case 1: { if (ic.find_cc(a) == ic.find_cc(b)) { std::cout << "Yes" << eoln; return; } ic.unite(a, b); ccn -= 1; g[a].push_back(b); g[b].push_back(a); } break; case 2: { g[a].push_back(b); } break; } } ei1333::StronglyConnectedComponents scc(g); scc.build(buff); std::cout << (ccn == buff.size() ? "No" : "Yes") << eoln; } } // namespace n91 int main() { n91::main_(); return 0; }