diff --git a/src/bin/a.out b/src/bin/a.out index f0750c0..828b8be 100755 Binary files a/src/bin/a.out and b/src/bin/a.out differ diff --git a/src/bin/cf-1810e.cc b/src/bin/cf-1810e.cc new file mode 100644 index 0000000..4a136a8 --- /dev/null +++ b/src/bin/cf-1810e.cc @@ -0,0 +1,400 @@ +#ifdef ONLINE_JUDGE +#pragma GCC optimize("Ofast") +#endif +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::value +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() +#define __as_typeof(container) decltype(container)::value_type + +/* type aliases */ +#if LONG_LONG_MAX != INT64_MAX +using ll = int64_t; +using ull = uint64_t; +#else +using ll = long long; +using ull = unsigned long long; +#endif +using int128 = __int128_t; +using uint128 = __uint128_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* constants */ +constexpr int INF = 0x3f3f3f3f; +constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; +constexpr ll MDL1 = 8784491; +constexpr ll MDL2 = PRIME; +constexpr int128 INT128_MAX = numeric_limits::max(); +constexpr uint128 UINT128_MAX = numeric_limits::max(); +constexpr int128 INT128_MIN = numeric_limits::min(); +constexpr uint128 UINT128_MIN = numeric_limits::min(); + +/* random */ + +mt19937 rd(chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count()); + +/* bit-wise operations */ +#define lowbit(x) ((x) & -(x)) +#define popcount(x) (__builtin_popcountll(ll(x))) +#define parity(x) (__builtin_parityll(ll(x))) +#define msp(x) (63LL - __builtin_clzll(ll(x))) +#define lsp(x) (__builtin_ctzll(ll(x))) + +/* arithmetic operations */ +#define mod(x, y) ((((x) % (y)) + (y)) % (y)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1))) +#define u1(p) ((p) >> 32) +#define u2(p) ((p) & ((1ULL << 32) - 1)) +#define ult std::less +#define ugt std::greater + +#define ipair ull +#define imake(x, y) (umake(x, y)) +#define i1(p) (int(u1(ll(p)))) +#define i2(p) (ll(u2(p) << 32) >> 32) +struct ilt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) < i2(b); + else return i1(a) < i1(b); + } +}; +struct igt { + bool operator()(const ipair& a, const ipair& b) const { + if (i1(a) == i1(b)) return i2(a) > i2(b); + else return i1(a) > i1(b); + } +}; + +/* conditions */ +#define loop while (1) +#define if_or(var, val) if (!(var == val)) var = val; else +#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;) +#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;) + +/* hash */ +struct safe_hash { + // https://codeforces.com/blog/entry/62393 + static uint64_t splitmix64(uint64_t x) { + // http://xorshift.di.unimi.it/splitmix64.c + x += 0x9e3779b97f4a7c15; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); + } + + size_t operator()(uint64_t x) const { + static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); + return splitmix64(x + FIXED_RANDOM); + } +}; + +struct pair_hash { + template + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];) +#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];) +#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);) +#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};) +#define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} + +/* io */ +#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL)) +template void __read(T& x) { cin >> x; } +template void __read(T& x, U&... args) { cin >> x; __read(args...); } +#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);) +#define readvec(type, a, n) __AS_PROCEDURE(vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i];) +#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;) +#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;) +#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;) +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} +std::ostream& operator<<(std::ostream& dest, const int128& value) { + // https://stackoverflow.com/a/25115163/23881100 + std::ostream::sentry s( dest ); + if ( s ) { + uint128 tmp = value < 0 ? -value : value; + char buffer[ 128 ]; + char* d = std::end( buffer ); + do { + -- d; + *d = "0123456789"[ tmp % 10 ]; + tmp /= 10; + } while ( tmp != 0 ); + if ( value < 0 ) { + -- d; + *d = '-'; + } + int len = std::end( buffer ) - d; + if ( dest.rdbuf()->sputn( d, len ) != len ) { + dest.setstate( std::ios_base::badbit ); + } + } + return dest; +} + +/* pops */ +#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) +#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();) +#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();) + +/* math */ +constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); } + +void __exgcd(ll a, ll b, ll& x, ll& y) { + if (b == 0) { + x = 1, y = 0; + return; + } + __exgcd(b, a % b, y, x); + y -= a / b * x; +} + +ll inverse(ll a, ll b) { + ll x, y; + __exgcd(a, b, x, y); + return mod(x, b); +} + +vector> decompose(ll x) { + vector> res; + for (int i = 2; i * i <= x; i++) { + if (x % i == 0) { + int cnt = 0; + ll pw = 1; + while (x % i == 0) ++cnt, x /= i, pw *= i; + res.emplace_back(i, cnt, pw); + } + } + if (x != 1) { + res.emplace_back(x, 1, x); + } + return res; +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector pi(n); + for (int i = 1; i < n; i++) { + int j = pi[i - 1]; + while (j > 0 && t[i] != t[j]) j = pi[j - 1]; + if (t[i] == t[j]) j++; + pi[i] = j; + } + return pi; +} +vector calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair prev = {1, -1}; + for (int i = 1; i < m; ++i) { + if (z[i - prev.first] + i <= prev.second) { + z.push_back(z[i - prev.first]); + } else { + int j = max(i, prev.second + 1); + while (j < m && t[j] == t[j - i]) ++j; + z.push_back(j - i); + prev = {i, j - 1}; + } + } + return z; +} +vector kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector lps = calc_next(cur); + for (int i = sz2 + 1; i <= sz1 + sz2; i++) { + if (lps[i] == sz2) v.push_back(i - 2 * sz2); + } + return v; +} +int period(string s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} +///////////////////////////////////////////////////////// + +// #define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 7219 + +void dump() {} + +void dump_ignore() {} + +void prep() {} + +class quick_union { +private: + vector c, sz; +public: + quick_union(size_t n) : c(n), sz(n) { + iota(c.begin(), c.end(), 0); + sz.assign(n, 1); + } + + size_t query(size_t i) { + if (c[i] != i) c[i] = query(c[i]); + return c[i]; + } + + void merge(size_t i, size_t j) { + sz[query(j)] += sz[query(i)]; + c[query(i)] = query(j); + } + + bool connected(size_t i, size_t j) { + return query(i) == query(j); + } + + size_t query_size(size_t i) { + return sz[query(i)]; + } +}; + +void solve() { + read(int, n, m); + adj(cand, n); + vector a(n + 1); + for (int i = 1; i <= n; ++i) { + cin >> a[i]; + } + while (m--) { + read(int, u, v); + edge(cand, u, v); + } + vector idx(n + 1); + quick_union qu(n + 1); + iota(idx.begin(), idx.end(), 0); + sort(idx.begin() + 1, idx.end(), [&] (int u, int v) { return a[u] < a[v]; }); + for (int t = 1; t <= n; ++t) { + int v = idx[t]; + int rest = a[v]; + int mx = 0; + // debug(v), debug(rest); + for (auto&& u : cand[v]) { + if (a[u] > rest or qu.query_size(u) == 1 and a[u]) { + continue; + } + mx = max(mx, int(qu.query_size(u))); + } + if (mx >= rest) { + for (auto&& u : cand[v]) { + if (a[u] > rest or qu.connected(u, v)) { + continue; + } + // debug(curr_sz), debug(my_sz); + qu.merge(v, u); + } + } + // debug(sz); + } + int mx = 0; + for (int i = 1; i <= n; ++i) { + int sz = qu.query_size(i); + if (not (sz == 1 and a[i])) { + mx = max(mx, sz); + } + } + if (mx == n) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } +} + +int main() { +#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__) + assert(false && "incompatible compiler variant detected."); +#endif + untie, cout.tie(NULL); + prep(); +#ifdef SINGLE_TEST_CASE + solve(); +#else + read(int, t); + for (int i = 0; i < t; ++i) { +#ifdef DUMP_TEST_CASE + if (t < (DUMP_TEST_CASE)) { + solve(); + } else if (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + dump_ignore(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/ctext_out.cc b/src/bin/ctext_out.cc index 6daa7bd..4a136a8 100644 --- a/src/bin/ctext_out.cc +++ b/src/bin/ctext_out.cc @@ -294,49 +294,83 @@ void dump_ignore() {} void prep() {} +class quick_union { +private: + vector c, sz; +public: + quick_union(size_t n) : c(n), sz(n) { + iota(c.begin(), c.end(), 0); + sz.assign(n, 1); + } + + size_t query(size_t i) { + if (c[i] != i) c[i] = query(c[i]); + return c[i]; + } + + void merge(size_t i, size_t j) { + sz[query(j)] += sz[query(i)]; + c[query(i)] = query(j); + } + + bool connected(size_t i, size_t j) { + return query(i) == query(j); + } + + size_t query_size(size_t i) { + return sz[query(i)]; + } +}; + void solve() { read(int, n, m); - adj(ch, n); + adj(cand, n); + vector a(n + 1); + for (int i = 1; i <= n; ++i) { + cin >> a[i]; + } while (m--) { read(int, u, v); - if (u == 1) continue; - Edge(ch, v, u); + edge(cand, u, v); } - vector bfn(n + 1); - deque q; - q.emplace_back(1, 1); - bfn[1] = 1; - while (q.size()) { - popfront(q, t, v); - for (auto&& u : ch[v]) { - if (!bfn[u]) { - bfn[u] = t + 1; - q.emplace_back(t + 1, u); - } - } - } - for (int i = 1; i <= n; ++i) { - if (!bfn[i]) { - cout << "INFINITE\n"; - return; - } - } - cout << "FINITE\n"; vector idx(n + 1); + quick_union qu(n + 1); iota(idx.begin(), idx.end(), 0); - sort(idx.begin(), idx.end(), [&] (int i, int j) { return bfn[i] > bfn[j]; }); - vector res; - while (bfn[idx[0]]) { - for (int i = 0; i < n; ++i) { - if (!bfn[idx[i]]) { - break; + sort(idx.begin() + 1, idx.end(), [&] (int u, int v) { return a[u] < a[v]; }); + for (int t = 1; t <= n; ++t) { + int v = idx[t]; + int rest = a[v]; + int mx = 0; + // debug(v), debug(rest); + for (auto&& u : cand[v]) { + if (a[u] > rest or qu.query_size(u) == 1 and a[u]) { + continue; } - bfn[idx[i]] -= 1; - res.emplace_back(idx[i]); + mx = max(mx, int(qu.query_size(u))); + } + if (mx >= rest) { + for (auto&& u : cand[v]) { + if (a[u] > rest or qu.connected(u, v)) { + continue; + } + // debug(curr_sz), debug(my_sz); + qu.merge(v, u); + } + } + // debug(sz); + } + int mx = 0; + for (int i = 1; i <= n; ++i) { + int sz = qu.query_size(i); + if (not (sz == 1 and a[i])) { + mx = max(mx, sz); } } - cout << res.size() << '\n'; - putvec(res); + if (mx == n) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } } int main() { diff --git a/src/bin/std.in b/src/bin/std.in index a6418db..6e9dc99 100644 --- a/src/bin/std.in +++ b/src/bin/std.in @@ -1,16 +1,35 @@ 5 -3 2 -3 1 -2 1 -1 0 -2 0 -2 2 +4 3 +2 1 0 3 1 2 -2 1 -5 5 -2 1 -3 1 -4 2 +2 3 +3 4 +6 6 +0 1 2 3 0 1 +1 2 +2 3 +3 4 4 5 -5 1 +4 6 +5 6 +4 3 +0 1 2 0 +1 2 +2 3 +1 3 +4 6 +1 1 1 0 +1 2 +3 2 +4 3 +2 4 +4 1 +1 3 +5 5 +0 1 3 2 0 +1 2 +2 3 +3 4 +4 5 +3 5