This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include"islands.h"
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
using ll = long long;
const int mx = 200005;
vector<pair<int, int>> fwd[mx];
vector<pair<int, int>> bck[mx];
int vis[mx];
bool is_cycle[mx];
int N;
void dfs1(int cur) {
vis[cur] = 1;
for(auto [nxt, id] : fwd[cur]) {
if(vis[nxt] == 0) dfs1(nxt);
else if(vis[nxt] == 1) {
is_cycle[cur] = true;
}
}
vis[cur] = 2;
}
bool test(int bit) {
{
for(int i = 0; i < N; i++) vis[i] = 0;
vector<int> q;
for(auto [nxt, id] : fwd[0]) {
if((id >> bit) & 1) continue;
q.push_back(nxt);
vis[nxt] = 1;
}
bool cond = false;
int qidx = 0;
while(qidx < q.size()) {
int cur = q[qidx++];
if(is_cycle[cur]) cond = true;
for(auto [nxt, id] : fwd[cur]) {
if(!vis[nxt]) {
q.push_back(nxt);
vis[nxt] = 1;
}
}
}
if(!cond) return false;
}
{
for(int i = 0; i < N; i++) vis[i] = 0;
vector<int> q;
for(auto [nxt, id] : fwd[0]) {
if(!((id >> bit) & 1)) continue;
q.push_back(nxt);
vis[nxt] = 1;
}
bool cond = false;
int qidx = 0;
while(qidx < q.size()) {
int cur = q[qidx++];
if(is_cycle[cur]) cond = true;
for(auto [nxt, id] : fwd[cur]) {
if(!vis[nxt]) {
q.push_back(nxt);
vis[nxt] = 1;
}
}
}
if(!cond) return false;
}
return true;
}
std::variant<bool, std::vector<int>> find_journey(int _N, int M, std::vector<int> U, std::vector<int> V) {
N = _N;
for(int i = 0; i < N; i++) {
fwd[i].clear();
bck[i].clear();
vis[i] = 0;
}
for(int i = 0; i < M; i++) {
fwd[U[i]].push_back({V[i], i});
bck[V[i]].push_back({U[i], i});
}
dfs1(0);
for(int bit = 0; bit < 20; bit++) {
if(test(bit)) return vector<int>(1, 2);
}
return false;
}
Compilation message (stderr)
islands.cpp: In function 'bool test(int)':
islands.cpp:34:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(qidx < q.size()) {
| ~~~~~^~~~~~~~~~
islands.cpp:56:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while(qidx < q.size()) {
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |