結果
| 問題 |
No.1023 Cyclic Tour
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-11 23:51:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,555 bytes |
| コンパイル時間 | 1,756 ms |
| コンパイル使用メモリ | 150,748 KB |
| 最終ジャッジ日時 | 2025-01-19 13:41:18 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 34 WA * 15 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct StronglyConnectedComponents{
using G = vector<vector<int>>;
G graph;
G rev;
int n;
int scc_sz = 0;
G scc_graph;
vector<int> scc_id;
vector<int> vs;
vector<bool> visited;
StronglyConnectedComponents() = default;
StronglyConnectedComponents(int V) : n(V) {
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
for(int u = 0; u < n; ++u){
for(int v : graph[u]) rev[v].push_back(u);
}
}
void resize(int V){
n = V;
graph.resize(n);
rev.resize(n);
scc_id.assign(n, -1);
visited.assign(n, false);
}
void add_edge(int u, int v){
graph[u].emplace_back(v);
rev[v].emplace_back(u);
}
void dfs(int from){
visited[from] = true;
for(int to : graph[from]){
if(!visited[to]) dfs(to);
}
vs.push_back(from);
}
void rdfs(int from, int k){
scc_id[from] = k;
for(int to : rev[from]){
if(scc_id[to] == -1) rdfs(to, k);
}
}
void build(){
for(int v = 0; v < n; ++v){
if(!visited[v]) dfs(v);
}
reverse(vs.begin(), vs.end());
for(int v : vs){
if(scc_id[v] == -1) rdfs(v, scc_sz++);
}
scc_graph.resize(scc_sz);
for(int v = 0; v < n; ++v){
scc_graph[scc_id[v]].push_back(v);
}
return;
}
int size() { return scc_sz; }
vector<int> get_set(int k) { return scc_graph[k]; }
int operator[](int v) { return scc_id[v]; }
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
StronglyConnectedComponents g(N);
map<pair<int, int>, int> edge_cnt;
for(int i = 0; i < M; ++i){
int a, b, c;
cin >> a >> b >> c;
--a, --b;
if(c == 1){
g.add_edge(a, b);
g.add_edge(b, a);
}
else{
g.add_edge(a, b);
if(a > b) swap(a, b);
}
edge_cnt[{a, b}] += 1;
}
g.build();
for(int i = 0; i < g.size(); ++i){
auto vs = g.get_set(i);
if(vs.size() > 2){
cout << "Yes\n";
return 0;
}
if(vs.size() == 2 && edge_cnt[minmax(vs[0], vs[1])] > 1){
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}