結果
| 問題 | No.1023 Cyclic Tour |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-11 00:00:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 325 ms / 2,000 ms |
| コード長 | 4,327 bytes |
| コンパイル時間 | 2,197 ms |
| コンパイル使用メモリ | 186,008 KB |
| 実行使用メモリ | 50,520 KB |
| 最終ジャッジ日時 | 2024-09-16 03:52:49 |
| 合計ジャッジ時間 | 16,773 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 49 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using UnWeightedGraph = vector< vector< int > >;
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);
}
}
}
};
template< typename G >
struct LowLink {
const G &g;
vector< int > used, ord, low;
vector< int > articulation;
vector< pair< int, int > > bridge;
LowLink(const G &g) : g(g) {}
int dfs(int idx, int k, int par) {
used[idx] = true;
ord[idx] = k++;
low[idx] = ord[idx];
bool is_articulation = false;
int cnt = 0;
for(auto &to : g[idx]) {
if(!used[to]) {
++cnt;
k = dfs(to, k, idx);
low[idx] = min(low[idx], low[to]);
is_articulation |= ~par && low[to] >= ord[idx];
if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, (int) to));
} else if(to != par) {
low[idx] = min(low[idx], ord[to]);
}
}
is_articulation |= par == -1 && cnt > 1;
if(is_articulation) articulation.push_back(idx);
return k;
}
virtual void build() {
used.assign(g.size(), 0);
ord.assign(g.size(), 0);
low.assign(g.size(), 0);
int k = 0;
for(int i = 0; i < g.size(); i++) {
if(!used[i]) k = dfs(i, k, -1);
}
}
};
template< typename G >
struct TwoEdgeConnectedComponents : LowLink< G > {
using LL = LowLink< G >;
vector< int > comp;
TwoEdgeConnectedComponents(const G &g) : LL(g) {}
int operator[](const int &k) {
return comp[k];
}
void dfs(int idx, int par, int &k) {
if(~par && this->ord[par] >= this->low[idx]) comp[idx] = comp[par];
else comp[idx] = k++;
for(auto &to : this->g[idx]) {
if(comp[to] == -1) dfs(to, idx, k);
}
}
void build(UnWeightedGraph &t) {
LL::build();
comp.assign(this->g.size(), -1);
int k = 0;
for(int i = 0; i < comp.size(); i++) {
if(comp[i] == -1) dfs(i, -1, k);
}
t.resize(k);
for(auto &e : this->bridge) {
int x = comp[e.first], y = comp[e.second];
t[x].push_back(y);
t[y].push_back(x);
}
}
};
int a[200010];
int b[200010];
int c[200010];
signed main(){
int n, m;
cin >> n >> m;
UnWeightedGraph graph(n);
for(int i = 0;i < m;i++){
cin >> a[i] >> b[i] >> c[i];
a[i]--; b[i]--;
if(c[i] == 1){
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
}
TwoEdgeConnectedComponents<UnWeightedGraph> x(graph);
UnWeightedGraph tmp, tmp2, tmp3;
x.build(tmp);
vector<int> cnt(n, 0);
for(int i = 0;i < n;i++){
cnt[x[i]]++;
if(cnt[x[i]] > 1){
cout << "Yes" << endl;
return 0;
}
}
StronglyConnectedComponents<UnWeightedGraph> scc(graph);
scc.build(tmp2);
for(int i = 0;i < m;i++){
if(c[i] == 2){
if(scc[a[i]] == scc[b[i]]){
cout << "Yes" << endl;
return 0;
}else{
tmp2[scc[a[i]]].push_back(scc[b[i]]);
}
}
}
StronglyConnectedComponents<UnWeightedGraph> scc2(tmp2);
scc2.build(tmp3);
vector<int> cnt2(n, 0);
for(int i = 0;i < tmp2.size();i++){
cnt2[scc2[i]]++;
if(cnt2[scc2[i]] > 1){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}