# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
301508 | 2020-09-18T03:32:09 Z | qpwoeirut | Comparing Plants (IOI20_plants) | C++17 | 0 ms | 0 KB |
#include "plants.h" #include <bits/stdc++.h> using namespace std; int N; vector<set<int>> adj; void init(int k, vector<int> r) { N = r.size(); adj = vector<set<int>>(N, set<int>()); radj = vector<set<int>>(N, set<int>()); for (int i=0; i<N; ++i) { int u = i, v = (i+1) % N; if (r[u] == 0) { adj[u].insert(v); } else { adj[v].insert(u); } } } int compare_plants(int x, int y) { vector<bool> visited(N); queue<int> q; q.push(x); visited[x] = true; while (q.size() > 0) { int cur = q.front(); q.pop(); for (auto it=adj[cur].begin(); it!=adj[cur].end(); ++it) { if (!visited[*it]) { if (*it == y) { return 1; } visited[*it] = true; q.push(*it); } } } fill(visited.begin(), visited.end(), false); q.push(y); visited[y] = true; while (q.size() > 0) { int cur = q.front(); q.pop(); for (auto it=adj[cur].begin(); it!=adj[cur].end(); ++it) { if (!visited[*it]) { if (*it == x) { return -1; } visited[*it] = true; q.push(*it); } } } return 0; }