結果
| 問題 |
No.1023 Cyclic Tour
|
| コンテスト | |
| ユーザー |
hiroaki0615
|
| 提出日時 | 2020-04-10 23:10:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,310 bytes |
| コンパイル時間 | 1,071 ms |
| コンパイル使用メモリ | 95,692 KB |
| 実行使用メモリ | 31,360 KB |
| 最終ジャッジ日時 | 2024-09-16 00:02:53 |
| 合計ジャッジ時間 | 9,949 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 47 WA * 2 |
ソースコード
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}
class UnionFind {
private:
vector<int> parent_;
vector<int> node_rank_;
vector<int> sizes_;
public:
UnionFind(int node_num):
parent_(vector<int>(node_num)), node_rank_(vector<int>(node_num)), sizes_(vector<int>(node_num)) {
for (int i = 0; i < node_num; ++i) {
parent_[i] = i;
node_rank_[i] = 0;
sizes_[i] = 1;
}
}
int getRoot(int u) {
return parent_[u] == u ? u : parent_[u] = getRoot(parent_[u]);
}
bool isSame(int u, int v) {
return getRoot(u) == getRoot(v);
}
void unite(int u, int v) {
u = getRoot(u);
v = getRoot(v);
if (u == v) return;
if (node_rank_[u] < node_rank_[v]) {
parent_[u] = v;
sizes_[v] += sizes_[u];
}
else {
parent_[v] = u;
sizes_[u] += sizes_[v];
if (node_rank_[u] == node_rank_[v]) {
node_rank_[u]++;
}
}
}
int getSize(int u) {
return sizes_[getRoot(u)];
}
};
void dfs(const vector<vector<int>>& graph, vector<bool>& seen, vector<bool>& finished, int node, int& pos) {
seen[node] = true;
for (auto& next_node : graph[node]) {
if (finished[next_node]) {
continue;
}
if (seen[next_node] && !finished[next_node]) {
pos = next_node;
return;
}
dfs(graph, seen, finished, next_node, pos);
if (pos != -1) {
return;
}
}
finished[node] = true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> A(M), B(M), C(M);
rep(i, 0, M) {
cin >> A[i] >> B[i] >> C[i];
--A[i], --B[i];
}
UnionFind uf(N);
rep(i, 0, M) {
if (C[i] == 1) {
uf.unite(A[i], B[i]);
}
}
set<int> S;
rep(i, 0, N) {
S.insert(uf.getRoot(i));
}
map<int, int> node_map;
int node = 0;
for (auto s : S) {
node_map[s] = node++;
}
vector<vector<int>> graph(node);
rep(i, 0, M) {
if (C[i] == 2) {
int u = node_map[uf.getRoot(A[i])];
int v = node_map[uf.getRoot(B[i])];
graph[u].push_back(v);
}
}
vector<bool> seen(node, false), finished(node, false);
int pos = -1;
rep(i, 0, node) {
if (!finished[i]) {
pos = -1;
dfs(graph, seen, finished, i, pos);
if (pos != -1) {
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
return 0;
}
hiroaki0615