結果
| 問題 |
No.2780 The Bottle Imp
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-07 21:50:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 82 ms / 2,000 ms |
| コード長 | 2,160 bytes |
| コンパイル時間 | 2,788 ms |
| コンパイル使用メモリ | 219,356 KB |
| 最終ジャッジ日時 | 2025-02-21 20:13:23 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class SCC{
public:
int siz = 0,name = 0,nowc = 0;
vector<bool> already;
vector<int> ord,low,belong,topo;
vector<vector<int>> Graph,cycle;
stack<int> St;
void dfs(int pos){
low.at(pos) = (ord.at(pos) = name++); St.push(pos);
for(auto to : Graph.at(pos)){
if(ord.at(to) == -1){
dfs(to);
low.at(pos) = min(low.at(pos),low.at(to));
}
else if(!already.at(to)) low.at(pos) = min(low.at(pos),ord.at(to));
}
if(low.at(pos) == ord.at(pos)){
cycle.push_back({});
while(true){
int s = St.top(); St.pop();
cycle.back().push_back(s);
belong.at(s) = nowc; already.at(s) = true;
if(s == pos) break;
}
nowc++;
}
}
void make(vector<vector<int>> &G){
Graph = G; siz = G.size();
ord.resize(siz,-1); low.resize(siz,1001001001);
already.resize(siz); belong.resize(siz);
for(int i=0; i<siz; i++) if(ord.at(i) == -1) dfs(i);
for(int i=0; i<siz; i++) belong.at(i) = (nowc-1)-belong.at(i);
reverse(cycle.begin(),cycle.end());
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
vector<vector<int>> Graph(N);
for(int i=0; i<N; i++){
int K; cin >> K;
for(int k=0; k<K; k++){
int to; cin >> to; to--;
Graph.at(i).push_back(to);
}
}
SCC Z; Z.make(Graph);
if(Z.belong.at(0) != 0){cout << "No" << endl; return 0;}
reverse(Z.cycle.begin(),Z.cycle.end());
set<int> S;
bool end = true;
for(auto &Vs : Z.cycle){
set<int> S2;
for(auto pos : Vs) S2.insert(pos);
if(end){end = false; S = S2; continue;}
bool ok = false;
for(auto pos : Vs){
for(auto to : Graph.at(pos)) if(S.count(to)){ok = true; break;}
if(ok) break;
}
if(!ok){cout << "No" << endl; return 0;}
S = S2; continue;
}
cout << "Yes" << endl;
}