fea: more contests
This commit is contained in:
parent
00d23dc313
commit
c65a12a652
103 changed files with 3871 additions and 143 deletions
9
codeforces/903/.clang-format
Normal file
9
codeforces/903/.clang-format
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
BasedOnStyle: Google
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortCompoundRequirementOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLambdasOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
45
codeforces/903/.clangd
Normal file
45
codeforces/903/.clangd
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
CompileFlags:
|
||||
Add:
|
||||
-O2
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wformat=2
|
||||
-Wfloat-equal
|
||||
-Wlogical-op
|
||||
-Wshift-overflow=2
|
||||
-Wnon-virtual-dtor
|
||||
-Wold-style-cast
|
||||
-Wcast-qual
|
||||
-Wuseless-cast
|
||||
-Wno-sign-promotion
|
||||
-Wcast-align
|
||||
-Wunused
|
||||
-Woverloaded-virtual
|
||||
-Wconversion
|
||||
-Wsign-conversion
|
||||
-Wmisleading-indentation
|
||||
-Wduplicated-cond
|
||||
-Wduplicated-branches
|
||||
-Wlogical-op
|
||||
-Wnull-dereference
|
||||
-Wformat=2
|
||||
-Wformat-overflow
|
||||
-Wformat-truncation
|
||||
-Wdouble-promotion
|
||||
-Wundef
|
||||
-DLOCAL
|
||||
-Wno-unknown-pragmas
|
||||
-std=c++23
|
||||
-std=c++20
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
-std=c++23
|
||||
91
codeforces/903/a.cc
Normal file
91
codeforces/903/a.cc
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto &&x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto &&x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 n, m;
|
||||
cin >> n >> m;
|
||||
string N, M;
|
||||
cin >> N >> M;
|
||||
|
||||
string cur = N;
|
||||
for (u32 ans = 0; ans <= 5; ++ans) {
|
||||
if (cur.find(M) != string::npos) {
|
||||
cout << ans << '\n';
|
||||
return;
|
||||
}
|
||||
cur += cur;
|
||||
}
|
||||
|
||||
cout << "-1\n";
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
106
codeforces/903/b.cc
Normal file
106
codeforces/903/b.cc
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 a, b, c;
|
||||
|
||||
cin >> a >> b >> c;
|
||||
|
||||
for (u32 mask = 0; mask < (1 << 9) - 1; ++mask) {
|
||||
if (__builtin_popcount(mask) > 3)
|
||||
continue;
|
||||
|
||||
u32 right = __builtin_popcount(mask & ((1 << 3) - 1));
|
||||
u32 mid = __builtin_popcount((mask >> 3) & ((1 << 3) - 1));
|
||||
u32 left = __builtin_popcount((mask >> 6) & ((1 << 3) - 1));
|
||||
|
||||
if (left && a % (left + 1))
|
||||
continue;
|
||||
if (mid && b % (mid + 1))
|
||||
continue;
|
||||
if (right && c % (right + 1))
|
||||
continue;
|
||||
|
||||
u32 A = left ? a / (left + 1) : a;
|
||||
u32 B = mid ? b / (mid + 1) : b;
|
||||
u32 C = right ? c / (right + 1) : c;
|
||||
|
||||
if (A == B && B == C) {
|
||||
YES();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
NO();
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
113
codeforces/903/c.cc
Normal file
113
codeforces/903/c.cc
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
cin >> n;
|
||||
vector<string> grid(n);
|
||||
for (auto& e : grid)
|
||||
cin >> e;
|
||||
/*
|
||||
core strategy: walk every char in tL region, find other 3 matching chars
|
||||
rotationally
|
||||
- find formula
|
||||
sort, dist is sum of char distances for each smaller than biggest (can only
|
||||
inc char)
|
||||
|
||||
this is ans
|
||||
|
||||
optimizes walking "rin"
|
||||
|
||||
impl: fill in grid as we go fore easer coverage
|
||||
|
||||
actually, just walk top side
|
||||
*/
|
||||
|
||||
u32 ans = 0;
|
||||
for (i32 r = 0; r < n / 2; ++r) {
|
||||
// NOTE: missed this condition + other vars, made unecessary
|
||||
// // no need for pairs
|
||||
// why is auto comment on, fix the autogroup
|
||||
for (i32 c = r; c < n - 1 - r; ++c) {
|
||||
char big = max({grid[r][c], grid[c][n - 1 - r],
|
||||
grid[n - 1 - r][n - 1 - c], grid[n - 1 - c][r]});
|
||||
|
||||
ans += big - grid[r][c];
|
||||
ans += big - grid[c][n - 1 - r];
|
||||
ans += big - grid[n - 1 - r][n - 1 - c];
|
||||
ans += big - grid[n - 1 - c][r];
|
||||
}
|
||||
}
|
||||
cout << ans << endl;
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
30
codeforces/903/compile_flags.txt
Normal file
30
codeforces/903/compile_flags.txt
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
-O2
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wformat=2
|
||||
-Wfloat-equal
|
||||
-Wlogical-op
|
||||
-Wshift-overflow=2
|
||||
-Wnon-virtual-dtor
|
||||
-Wold-style-cast
|
||||
-Wcast-qual
|
||||
-Wuseless-cast
|
||||
-Wno-sign-promotion
|
||||
-Wcast-align
|
||||
-Wunused
|
||||
-Woverloaded-virtual
|
||||
-Wconversion
|
||||
-Wmisleading-indentation
|
||||
-Wduplicated-cond
|
||||
-Wduplicated-branches
|
||||
-Wlogical-op
|
||||
-Wnull-dereference
|
||||
-Wformat=2
|
||||
-Wformat-overflow
|
||||
-Wformat-truncation
|
||||
-Wdouble-promotion
|
||||
-Wundef
|
||||
-DLOCAL
|
||||
-std=c++23
|
||||
132
codeforces/903/d.cc
Normal file
132
codeforces/903/d.cc
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
unordered_map<u32, u32> factor_counts;
|
||||
vector<u32> primes;
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
cin >> n;
|
||||
vector<u32> a(n);
|
||||
for (auto& e : a)
|
||||
cin >> e;
|
||||
|
||||
factor_counts.clear();
|
||||
|
||||
// NOTE: factorization skills are weak,. i.e. number theory and prime
|
||||
// factorization i.e. skipping over 2
|
||||
// also could binary search
|
||||
// // NOTE: thought it was "minimum operations" complicating it
|
||||
for (auto e : a) {
|
||||
for (const auto& p : primes) {
|
||||
if (p * p > e)
|
||||
break;
|
||||
u32 count = 0;
|
||||
while (e % p == 0) {
|
||||
++count;
|
||||
e /= p;
|
||||
}
|
||||
if (count > 0) {
|
||||
factor_counts[p] += count;
|
||||
}
|
||||
}
|
||||
if (e > 1) {
|
||||
++factor_counts[e];
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& [factor, count] : factor_counts) {
|
||||
if (count % n) {
|
||||
NO();
|
||||
return;
|
||||
}
|
||||
}
|
||||
YES();
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
vector<bool> P(10001, true);
|
||||
P[0] = P[1] = false;
|
||||
for (u32 p = 2; p * p <= 10000; ++p) {
|
||||
if (P[p]) {
|
||||
for (u32 i = p * p; i <= 10000; i += p) {
|
||||
P[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 i = 2; i <= 10000; ++i) {
|
||||
if (P[i]) {
|
||||
primes.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
14
codeforces/903/debug_flags.txt
Normal file
14
codeforces/903/debug_flags.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
-g3
|
||||
-fsanitize=address,undefined
|
||||
-fsanitize=float-divide-by-zero
|
||||
-fsanitize=float-cast-overflow
|
||||
-fno-sanitize-recover=all
|
||||
-fstack-protector-all
|
||||
-fstack-usage
|
||||
-fno-omit-frame-pointer
|
||||
-fno-inline
|
||||
-ffunction-sections
|
||||
-D_GLIBCXX_DEBUG
|
||||
-D_GLIBCXX_DEBUG_PEDANTIC
|
||||
-DLOCAL
|
||||
-std=c++20
|
||||
96
codeforces/903/e.cc
Normal file
96
codeforces/903/e.cc
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
cin >> n;
|
||||
vector<u32> a(n);
|
||||
for (auto& x : a)
|
||||
cin >> x;
|
||||
|
||||
vector<u32> dp(n + 1, n);
|
||||
dp[n] = 0;
|
||||
|
||||
for (i32 i = n - 1; i >= 0; --i) {
|
||||
u32 dont = 1 + dp[i + 1];
|
||||
|
||||
u32 take = i + a[i] + 1;
|
||||
if (take <= n)
|
||||
dont = min(dont, dp[take]);
|
||||
|
||||
dp[i] = dont;
|
||||
}
|
||||
|
||||
cout << dp[0] << '\n';
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
142
codeforces/903/f.cc
Normal file
142
codeforces/903/f.cc
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using f64 = double;
|
||||
using f128 = long double;
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
#define ff first
|
||||
#define ss second
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
const u32 MAXN = 2 * 1e5 + 1;
|
||||
bitset<MAXN> marked, seen;
|
||||
vector<vector<u32>> tree(MAXN);
|
||||
deque<u32> q;
|
||||
|
||||
void solve() {
|
||||
u32 n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
marked.reset();
|
||||
tree.assign(n + 1, vector<u32>());
|
||||
|
||||
u32 node;
|
||||
for (u32 i = 0; i < k; ++i) {
|
||||
cin >> node;
|
||||
marked[node] = true;
|
||||
}
|
||||
|
||||
u32 u, v;
|
||||
for (u32 i = 0; i < n - 1; ++i) {
|
||||
cin >> u >> v;
|
||||
tree[u].emplace_back(v);
|
||||
tree[v].emplace_back(u);
|
||||
}
|
||||
|
||||
q.clear();
|
||||
q.push_back(node);
|
||||
seen.reset();
|
||||
u32 last = node;
|
||||
|
||||
while (!q.empty()) {
|
||||
auto top = q.front();
|
||||
q.pop_front();
|
||||
seen[top] = true;
|
||||
if (marked[top])
|
||||
last = top;
|
||||
|
||||
for (auto& neighbor : tree[top]) {
|
||||
if (!seen[neighbor]) {
|
||||
q.push_back(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
q.clear();
|
||||
q.push_back(last);
|
||||
seen.reset();
|
||||
u32 ans = 0, d = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
++d;
|
||||
u32 size = q.size();
|
||||
for (u32 i = 0; i < size; ++i) {
|
||||
auto top = q.front();
|
||||
q.pop_front();
|
||||
if (marked[top])
|
||||
ans = max(ans, d);
|
||||
seen[top] = true;
|
||||
for (auto& neighbor : tree[top]) {
|
||||
if (!seen[neighbor]) {
|
||||
q.push_back(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans / 2 << endl;
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
u32 tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
37
codeforces/903/io/a.in
Normal file
37
codeforces/903/io/a.in
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
12
|
||||
1 5
|
||||
a
|
||||
aaaaa
|
||||
5 5
|
||||
eforc
|
||||
force
|
||||
2 5
|
||||
ab
|
||||
ababa
|
||||
3 5
|
||||
aba
|
||||
ababa
|
||||
4 3
|
||||
babb
|
||||
bbb
|
||||
5 1
|
||||
aaaaa
|
||||
a
|
||||
4 2
|
||||
aabb
|
||||
ba
|
||||
2 8
|
||||
bk
|
||||
kbkbkbkb
|
||||
12 2
|
||||
fjdgmujlcont
|
||||
tf
|
||||
2 2
|
||||
aa
|
||||
aa
|
||||
3 5
|
||||
abb
|
||||
babba
|
||||
1 19
|
||||
m
|
||||
mmmmmmmmmmmmmmmmmmm
|
||||
15
codeforces/903/io/a.out
Normal file
15
codeforces/903/io/a.out
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
3
|
||||
1
|
||||
2
|
||||
-1
|
||||
1
|
||||
0
|
||||
1
|
||||
3
|
||||
1
|
||||
0
|
||||
2
|
||||
5
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.27103 ms
|
||||
16
codeforces/903/io/b.in
Normal file
16
codeforces/903/io/b.in
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
15
|
||||
1 3 2
|
||||
5 5 5
|
||||
6 36 12
|
||||
7 8 7
|
||||
6 3 3
|
||||
4 4 12
|
||||
12 6 8
|
||||
1000000000 1000000000 1000000000
|
||||
3 7 1
|
||||
9 9 1
|
||||
9 3 6
|
||||
2 8 2
|
||||
5 3 10
|
||||
8 4 8
|
||||
2 8 4
|
||||
18
codeforces/903/io/b.out
Normal file
18
codeforces/903/io/b.out
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
YES
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
|
||||
[code]: 0
|
||||
[time]: 11.1175 ms
|
||||
26
codeforces/903/io/c.in
Normal file
26
codeforces/903/io/c.in
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
5
|
||||
4
|
||||
abba
|
||||
bcbb
|
||||
bccb
|
||||
abba
|
||||
2
|
||||
ab
|
||||
ba
|
||||
6
|
||||
codefo
|
||||
rcesco
|
||||
deforc
|
||||
escode
|
||||
forces
|
||||
codefo
|
||||
4
|
||||
baaa
|
||||
abba
|
||||
baba
|
||||
baab
|
||||
4
|
||||
bbaa
|
||||
abba
|
||||
aaba
|
||||
abba
|
||||
8
codeforces/903/io/c.out
Normal file
8
codeforces/903/io/c.out
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1
|
||||
2
|
||||
181
|
||||
5
|
||||
9
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.35901 ms
|
||||
15
codeforces/903/io/d.in
Normal file
15
codeforces/903/io/d.in
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
7
|
||||
5
|
||||
100 2 50 10 1
|
||||
3
|
||||
1 1 1
|
||||
4
|
||||
8 2 4 2
|
||||
4
|
||||
30 50 27 20
|
||||
2
|
||||
75 40
|
||||
2
|
||||
4 4
|
||||
3
|
||||
2 3 1
|
||||
10
codeforces/903/io/d.out
Normal file
10
codeforces/903/io/d.out
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
YES
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
|
||||
[code]: 0
|
||||
[time]: 11.4608 ms
|
||||
15
codeforces/903/io/e.in
Normal file
15
codeforces/903/io/e.in
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
7
|
||||
7
|
||||
3 3 4 5 2 6 1
|
||||
4
|
||||
5 6 3 2
|
||||
6
|
||||
3 4 1 6 7 7
|
||||
3
|
||||
1 4 3
|
||||
5
|
||||
1 2 3 4 5
|
||||
5
|
||||
1 2 3 1 2
|
||||
5
|
||||
4 5 5 1 5
|
||||
10
codeforces/903/io/e.out
Normal file
10
codeforces/903/io/e.out
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
0
|
||||
4
|
||||
1
|
||||
1
|
||||
2
|
||||
1
|
||||
0
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.28152 ms
|
||||
49
codeforces/903/io/f.in
Normal file
49
codeforces/903/io/f.in
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
6
|
||||
7 3
|
||||
2 6 7
|
||||
1 2
|
||||
1 3
|
||||
2 4
|
||||
2 5
|
||||
3 6
|
||||
3 7
|
||||
4 4
|
||||
1 2 3 4
|
||||
1 2
|
||||
2 3
|
||||
3 4
|
||||
5 1
|
||||
1
|
||||
1 2
|
||||
1 3
|
||||
1 4
|
||||
1 5
|
||||
5 2
|
||||
4 5
|
||||
1 2
|
||||
2 3
|
||||
1 4
|
||||
4 5
|
||||
10 8
|
||||
1 2 3 4 5 8 9 10
|
||||
2 10
|
||||
10 5
|
||||
5 3
|
||||
3 1
|
||||
1 7
|
||||
7 4
|
||||
4 9
|
||||
8 9
|
||||
6 1
|
||||
10 9
|
||||
1 2 4 5 6 7 8 9 10
|
||||
1 3
|
||||
3 9
|
||||
9 4
|
||||
4 10
|
||||
10 6
|
||||
6 7
|
||||
7 2
|
||||
2 5
|
||||
5 8
|
||||
|
||||
9
codeforces/903/io/f.out
Normal file
9
codeforces/903/io/f.out
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
2
|
||||
2
|
||||
0
|
||||
1
|
||||
4
|
||||
5
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.84347 ms
|
||||
30
codeforces/903/makefile
Normal file
30
codeforces/903/makefile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
.PHONY: run debug clean setup init
|
||||
|
||||
VERSION ?= 20
|
||||
|
||||
SRC = $(word 2,$(MAKECMDGOALS))
|
||||
|
||||
.SILENT:
|
||||
|
||||
run:
|
||||
sh scripts/run.sh $(SRC)
|
||||
|
||||
debug:
|
||||
sh scripts/debug.sh $(SRC)
|
||||
|
||||
clean:
|
||||
rm -rf build/*
|
||||
|
||||
setup:
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
test -d scripts || mkdir -p scripts
|
||||
test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format .
|
||||
test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt
|
||||
test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd
|
||||
|
||||
init:
|
||||
make setup
|
||||
|
||||
%:
|
||||
@:
|
||||
29
codeforces/903/scripts/debug.sh
Normal file
29
codeforces/903/scripts/debug.sh
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./scripts/utils.sh
|
||||
|
||||
SRC="$1"
|
||||
BASE=$(basename "$SRC" .cc)
|
||||
INPUT="${BASE}.in"
|
||||
OUTPUT="${BASE}.out"
|
||||
DBG_BIN="${BASE}.debug"
|
||||
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
|
||||
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
|
||||
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
|
||||
|
||||
test -f "io/$INPUT" || touch "io/$INPUT"
|
||||
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
|
||||
|
||||
INPUT="io/$INPUT"
|
||||
OUTPUT="io/$OUTPUT"
|
||||
DBG_BIN="build/$DBG_BIN"
|
||||
|
||||
compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt
|
||||
CODE=$?
|
||||
test $CODE -gt 0 && exit $CODE
|
||||
|
||||
execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT"
|
||||
exit $?
|
||||
29
codeforces/903/scripts/run.sh
Normal file
29
codeforces/903/scripts/run.sh
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./scripts/utils.sh
|
||||
|
||||
SRC="$1"
|
||||
BASE=$(basename "$SRC" .cc)
|
||||
INPUT="${BASE}.in"
|
||||
OUTPUT="${BASE}.out"
|
||||
RUN_BIN="${BASE}.run"
|
||||
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
|
||||
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
|
||||
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
|
||||
|
||||
test -f "io/$INPUT" || touch "io/$INPUT"
|
||||
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
|
||||
|
||||
INPUT="io/$INPUT"
|
||||
OUTPUT="io/$OUTPUT"
|
||||
RUN_BIN="build/$RUN_BIN"
|
||||
|
||||
compile_source "$SRC" "$RUN_BIN" "$OUTPUT" ""
|
||||
CODE=$?
|
||||
test $CODE -gt 0 && exit $CODE
|
||||
|
||||
execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT"
|
||||
exit $?
|
||||
53
codeforces/903/scripts/utils.sh
Normal file
53
codeforces/903/scripts/utils.sh
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#!/bin/sh
|
||||
|
||||
execute_binary() {
|
||||
binary="$1"
|
||||
input="$2"
|
||||
output="$3"
|
||||
|
||||
start=$(date '+%s.%N')
|
||||
timeout 2s ./"$binary" <"$input" >"$output" 2>&1
|
||||
CODE=$?
|
||||
end=$(date '+%s.%N')
|
||||
truncate -s "$(head -n 1000 "$output" | wc -c)" "$output"
|
||||
|
||||
if [ $CODE -ge 124 ]; then
|
||||
MSG=''
|
||||
case $CODE in
|
||||
124) MSG='TIMEOUT' ;;
|
||||
128) MSG='SIGILL' ;;
|
||||
130) MSG='SIGABRT' ;;
|
||||
131) MSG='SIGBUS' ;;
|
||||
136) MSG='SIGFPE' ;;
|
||||
135) MSG='SIGSEGV' ;;
|
||||
137) MSG='SIGPIPE' ;;
|
||||
139) MSG='SIGTERM' ;;
|
||||
esac
|
||||
[ $CODE -ne 124 ] && sed -i '$d' "$output"
|
||||
test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output"
|
||||
else
|
||||
printf '\n[code]: %s' "$CODE" >>"$output"
|
||||
fi
|
||||
|
||||
printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output
|
||||
return $CODE
|
||||
}
|
||||
|
||||
compile_source() {
|
||||
src="$1"
|
||||
bin="$2"
|
||||
output="$3"
|
||||
flags="$4"
|
||||
|
||||
test -f "$bin" && rm "$bin" || true
|
||||
g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output"
|
||||
CODE=$?
|
||||
|
||||
if [ $CODE -gt 0 ]; then
|
||||
printf '\n[code]: %s' "$CODE" >>"$output"
|
||||
return $CODE
|
||||
else
|
||||
echo '' >"$output"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue