結果
| 問題 |
No.1023 Cyclic Tour
|
| コンテスト | |
| ユーザー |
noshi91
|
| 提出日時 | 2020-04-10 22:58:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 478 ms / 2,000 ms |
| コード長 | 7,078 bytes |
| コンパイル時間 | 1,185 ms |
| コンパイル使用メモリ | 92,732 KB |
| 最終ジャッジ日時 | 2025-01-09 16:38:16 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 49 |
ソースコード
//#define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>
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 <class T> auto md_vec(const usize n, const T &value) {
return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
if (b < a)
a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
if (a < b)
a = b;
}
template <class F> class rec_lambda {
F f;
public:
rec_lambda(F &&f) : f(std::move(f)) {}
template <class... Args> auto operator()(Args &&... args) const {
return f(*this, std::forward<Args>(args)...);
}
};
template <class F> auto make_rec(F &&f) { return rec_lambda<F>(std::move(f)); }
template <class T> T scan() {
T ret;
std::cin >> ret;
return ret;
}
constexpr char eoln = '\n';
} // namespace n91
namespace ei1333 {
using namespace std;
template <typename T> 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 <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
template <typename G> struct StronglyConnectedComponents {
const G &g;
UnWeightedGraph gg, rg;
vector<int> 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 <cassert>
#include <cstddef>
#include <utility>
#include <vector>
namespace n91 {
class incremental_connectivity {
protected:
class node_type;
public:
using container_type = std::vector<node_type>;
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<size_type, size_type> 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 <cassert>
#include <list>
namespace n91 {
void main_() {
/*
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
//*/
const usize n = scan<usize>();
const usize m = scan<usize>();
incremental_connectivity ic(n);
ei1333::UnWeightedGraph g(n), buff;
usize ccn = n;
std::vector<std::pair<int, int>> de;
for (const usize i : rep(0, m)) {
const usize a = scan<usize>() - 1;
const usize b = scan<usize>() - 1;
switch (scan<int>()) {
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);
de.emplace_back(a, b);
} break;
}
}
for (const auto &e : de) {
if (ic.find_cc(e.first) == ic.find_cc(e.second)) {
std::cout << "Yes" << eoln;
return;
}
}
ei1333::StronglyConnectedComponents<ei1333::UnWeightedGraph> scc(g);
scc.build(buff);
std::cout << (ccn == buff.size() ? "No" : "Yes") << eoln;
}
} // namespace n91
int main() {
n91::main_();
return 0;
}
noshi91