Submission #765764

#TimeUsernameProblemLanguageResultExecution timeMemory
765764marvinthangSimurgh (IOI17_simurgh)C++17
30 / 100
78 ms2660 KiB
/************************************* * 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 << '}'; } 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 vector <int> find_roads(int n, vector <int> u, vector <int> v) { int m = u.size(); DSU dsu(n); vector <int> on_tree; vector <int> rem; REP(i, m) (dsu.join(u[i], v[i]) ? on_tree : rem).push_back(i); int cur = count_common_roads(on_tree); vector <int> res; // cout << on_tree << '\n'; REP(i, n - 1) { dsu.reset(); REP(j, n - 1) if (j != i) dsu.join(u[on_tree[j]], v[on_tree[j]]); int f = on_tree[i]; vector <int> g; int in_i = 0; REPD(j, rem.size()) if (!dsu.connected(u[rem[j]], v[rem[j]])) { // cout << rem[j] << ' ' << u[rem[j]] << ' ' << v[rem[j]] << '\n'; on_tree[i] = rem[j]; int nxt = count_common_roads(on_tree); if (nxt > cur) { in_i = -1; res.push_back(rem[j]); } else if (nxt < cur) { if (!in_i) { res.push_back(f); in_i = 1; } } else g.push_back(rem[j]); // swap(rem[j], rem.back()); // rem.pop_back(); } if (in_i >= 0) res.insert(res.end(), ALL(g)); if (!in_i) res.push_back(f); // cout << res << '\n'; // cout << "-----------\n"; on_tree[i] = f; } // cout << res << '\n'; sort(ALL(res)); res.erase(unique(ALL(res)), res.end()); 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"); return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...