This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 25.06.2023 11:26:35 *
*************************************/
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
struct DisjointSet {
int n, *lab = nullptr;
DisjointSet(int _n = 0) {
resize(_n);
}
void reset(void) {
memset(lab, -1, (n + 1) * sizeof(int));
}
void resize(int _n) {
if (lab != nullptr) delete[] lab;
n = _n;
lab = new int[n + 1];
reset();
}
int find(int u) {
assert(u <= n);
return lab[u] < 0 ? u : lab[u] = find(lab[u]);
}
bool connected(int u, int v) { return find(u) == find(v); }
bool isRoot(int u) { return lab[u] < 0; }
int size(int u) { return -lab[find(u)]; }
bool join(int u, int v) {
if ((u = find(u)) == (v = find(v))) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
using DSU = DisjointSet;
// end of template
const int MAX = 505;
int timeDfs;
vector <int> tree, r_edge, states;
vector <bool> used;
vector <vector <int>> adj;
vector <int> U, V, low, num, par, low_id, pos;
void dfs(int u) {
num[u] = ++timeDfs;
for (int id: adj[u]) if (!used[id]) {
used[id] = true;
if (u == V[id]) swap(U[id], V[id]);
int v = V[id];
if (!num[v]) {
tree.push_back(id);
par[v] = id;
dfs(v);
if (minimize(low[u], low[v])) low_id[u] = low_id[v];
if (low[v] > num[u]) states[id] = 1;
} else {
r_edge.push_back(id);
if (minimize(low[u], num[v])) low_id[u] = id;
}
}
}
vector <int> trace_path(int u, int p) {
vector <int> res;
while (u != p) {
res.push_back(par[u]);
u = U[par[u]];
}
return res;
}
vector <int> find_roads(int n, vector <int> u, vector <int> v) {
int m = u.size();
adj.assign(n, vector<int>());
tree.clear();
r_edge.clear();
U = u; V = v;
timeDfs = 0;
num.assign(n, 0);
low.assign(n, MAX);
par.assign(n, 0);
low_id.assign(n, 0);
pos.assign(m, -1);
used.assign(m, false);
states.assign(m, -1);
REP(i, m) {
adj[U[i]].push_back(i);
adj[V[i]].push_back(i);
}
dfs(0);
REP(i, n - 1) pos[tree[i]] = i;
int cur = count_common_roads(tree);
for (int e1: tree) if (!~states[e1]) {
int e2 = low_id[V[e1]];
vector <int> p = trace_path(U[e2], V[e2]);
swap(*find(ALL(p), e1), p[0]);
REP(i, p.size()) {
int e = p[i];
tree[pos[e]] = e2;
int nxt = count_common_roads(tree);
tree[pos[e]] = e;
if (nxt != cur) {
states[e2] = nxt > cur;
states[e] = nxt < cur;
REP(j, i) states[p[j]] = states[e2];
break;
} else if (~states[e]) {
states[e2] = states[e];
REP(j, i) states[p[j]] = states[e];
break;
}
}
if (!~states[e1]) for (int e: p) states[e] = 0;
}
REP(u, n) {
vector <int> f;
for (int id: adj[u]) if (!~states[id]) f.push_back(id);
while (true) {
auto ask = [&] (int p) {
if (!p) return 0;
vector <int> a(f.begin(), f.begin() + p);
DSU dsu(n);
for (int x: a) dsu.join(U[x], V[x]);
int cnt = 0;
for (int x: tree) if (dsu.join(U[x], V[x])) {
cnt += states[x];
a.push_back(x);
}
return count_common_roads(a) - cnt;
};
int x = ask(f.size());
if (!x) {
for (int e: f) states[e] = 0;
break;
}
int l = 0, r = (int) f.size() - 1;
while (l <= r) {
int m = l + r >> 1;
if (ask(m + 1) == x) r = m - 1;
else l = m + 1;
}
while (f.size() > l) {
states[f.back()] = (int) f.size() == l + 1;
f.pop_back();
}
}
}
vector <int> res;
REP(i, m) if (states[i] == 1) res.push_back(i);
return res;
}
#ifdef LOCAL
#include "simurgh.h"
#include <cstdio>
#include <cassert>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
static int MAXQ = 30000;
static int n, m, q = 0;
static vector<int> u, v;
static vector<bool> goal;
static void wrong_answer() {
printf("NO\n");
exit(0);
}
static bool is_valid(const vector<int>& r) {
if(int(r.size()) != n - 1)
return false;
for(int i = 0; i < n - 1; i++)
if (r[i] < 0 || r[i] >= m)
return false;
return true;
}
static int _count_common_roads_internal(const vector<int>& r) {
if(!is_valid(r))
wrong_answer();
int common = 0;
for(int i = 0; i < n - 1; i++) {
bool is_common = goal[r[i]];
if (is_common)
common++;
}
return common;
}
int count_common_roads(const vector<int>& r) {
q++;
if(q > MAXQ)
wrong_answer();
return _count_common_roads_internal(r);
}
int main() {
file("simurgh");
assert(2 == scanf("%d %d", &n, &m));
u.resize(m);
v.resize(m);
for(int i = 0; i < m; i++)
assert(2 == scanf("%d %d", &u[i], &v[i]));
goal.resize(m, false);
for(int i = 0; i < n - 1; i++) {
int id;
assert(1 == scanf("%d", &id));
goal[id] = true;
}
vector<int> res = find_roads(n, u, v);
if(_count_common_roads_internal(res) != n - 1)
wrong_answer();
printf("YES\n");
printf("q = %d\n", q);
return 0;
}
#endif
Compilation message (stderr)
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:190:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
190 | int m = l + r >> 1;
| ~~^~~
simurgh.cpp:194:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
194 | while (f.size() > l) {
| ~~~~~~~~~^~~
# | 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... |