結果
| 問題 |
No.1023 Cyclic Tour
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-23 11:56:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 119 ms / 2,000 ms |
| コード長 | 5,769 bytes |
| コンパイル時間 | 3,289 ms |
| コンパイル使用メモリ | 206,904 KB |
| 最終ジャッジ日時 | 2025-01-09 22:45:30 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 49 |
ソースコード
#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;
}