From 05414bf7f767f10a16af9fe5fdfc1e38d492f0d8 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Fri, 16 Jan 2026 17:50:50 -0300 Subject: [PATCH] Add more problems. --- .../B. Battle of Arrays.cpp | 138 ++++++++++++++ .../C. Divine Tree.cpp | 160 ++++++++++++++++ .../B. Wishing Cards.cpp | 147 +++++++++++++++ .../A. Extract Numbers.cpp | 154 ++++++++++++++++ .... Queries about less or equal elements.cpp | 117 ++++++++++++ .../C. Make Palindrome.cpp | 159 ++++++++++++++++ .../E. Lomsat gelral.cpp | 167 +++++++++++++++++ Good Bye 2025/A. Yes or Yes.cpp | 109 +++++++++++ Good Bye 2025/B. Impost or Sus.cpp | 132 ++++++++++++++ Good Bye 2025/C. First or Second.cpp | 127 +++++++++++++ Good Bye 2025/D. Xmas or Hysteria.cpp | 168 +++++++++++++++++ Good Bye 2025/E. Flatten or Concatenate.cpp | 156 ++++++++++++++++ Hello 2026/A. Binary Array Game.cpp | 112 ++++++++++++ Hello 2026/B. Yet Another MEX Problem.cpp | 121 ++++++++++++ Hello 2026/C. War Strategy.cpp | 137 ++++++++++++++ .../D1. Tree Coloring (Easy Version).cpp | 149 +++++++++++++++ .../C3. Game on Tree (Hard).cpp | 172 ++++++++++++++++++ .../C. Triple Removal.cpp | 169 +++++++++++++++++ 18 files changed, 2594 insertions(+) create mode 100644 2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/B. Battle of Arrays.cpp create mode 100644 Codeforces Round 1033 (Div. 2) and CodeNite 2025/C. Divine Tree.cpp create mode 100644 Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp create mode 100644 Educational Codeforces Round 2/A. Extract Numbers.cpp create mode 100644 Educational Codeforces Round 2/B. Queries about less or equal elements.cpp create mode 100644 Educational Codeforces Round 2/C. Make Palindrome.cpp create mode 100644 Educational Codeforces Round 2/E. Lomsat gelral.cpp create mode 100644 Good Bye 2025/A. Yes or Yes.cpp create mode 100644 Good Bye 2025/B. Impost or Sus.cpp create mode 100644 Good Bye 2025/C. First or Second.cpp create mode 100644 Good Bye 2025/D. Xmas or Hysteria.cpp create mode 100644 Good Bye 2025/E. Flatten or Concatenate.cpp create mode 100644 Hello 2026/A. Binary Array Game.cpp create mode 100644 Hello 2026/B. Yet Another MEX Problem.cpp create mode 100644 Hello 2026/C. War Strategy.cpp create mode 100644 Hello 2026/D1. Tree Coloring (Easy Version).cpp create mode 100644 Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C3. Game on Tree (Hard).cpp create mode 100644 Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/C. Triple Removal.cpp diff --git a/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/B. Battle of Arrays.cpp b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/B. Battle of Arrays.cpp new file mode 100644 index 0000000..e4cbe27 --- /dev/null +++ b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/B. Battle of Arrays.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/contest/2181/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + multiset> a[2]; + + rep(i, n) { + ll b; + cin >> b; + a[0].insert(b); + } + + rep(i, m) { + ll b; + cin >> b; + a[1].insert(b); + } + + int now = 0; + + while (!a[now].empty()) { + ll ca = *a[now].begin(); + ll cb = *a[now^1].begin(); + a[now^1].erase(a[now^1].begin()); + now ^= 1; + + cb -= ca; + if (cb > 0) { + a[now].insert(cb); + } + } + + string win[] = {"Bob", "Alice"}; + cout << win[now] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1033 (Div. 2) and CodeNite 2025/C. Divine Tree.cpp b/Codeforces Round 1033 (Div. 2) and CodeNite 2025/C. Divine Tree.cpp new file mode 100644 index 0000000..a6583e2 --- /dev/null +++ b/Codeforces Round 1033 (Div. 2) and CodeNite 2025/C. Divine Tree.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/contest/2120/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n, m; + cin >> n >> m; + + ll high = n * (n + 1) / 2; + + if (m < n || m > high) { + cout << "-1\n"; + return; + } + + set> all; + nrep(i, 1, n + 1) { + all.insert(i); + } + + V> edges; + int root = -1; + int prev = -1; + + auto itr = all.begin(); + while (n < m) { + while (n - 1 > m - *itr) { + itr++; + } + n--; + m -= *itr; + if (root == -1) { + root = *itr; + prev = root; + itr = all.erase(itr); + continue; + } + + edges.emplace_back(prev, *itr); + prev = *itr; + itr = all.erase(itr); + } + + if (prev == -1) { + root = 1; + } else { + edges.emplace_back(prev, 1); + } + + all.erase(1); + itr = all.begin(); + while (itr != all.end()) { + edges.emplace_back(1, *itr); + itr++; + } + + cout << root << '\n'; + repv(i, edges) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp b/Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp new file mode 100644 index 0000000..3013754 --- /dev/null +++ b/Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/contest/2174/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vi a(n); + cin >> a; + + V> act = {{0, 0}, {1, a[0]}}; + int cur = a[0]; + nrep(i, 1, n) { + if (cur >= a[i]) { + continue; + } + + act.emplace_back(i + 1, a[i]); + cur = a[i]; + } + + act.emplace_back(n + 1, 0); + + vvvl dp(act.size(), vvl(k + 1, vl(k + 1))); + ll ans = 0; + + vvl maximal(act.size(), vl(k + 1)); + + int c = 1; + + nrep(i, 1, act.size() - 1) { + int lim = act[i].second; + int dis = act[i].first - act[i - 1].first; + + rep(j, k + 1) { + rep(l, min(act[i].second, j) + 1) { + dp[i][j][l] = max(maximal[i - 1][j - l] + l, dp[i - 1][j][l] + l * (dp[i - 1][j][l] != 0) * dis); + ll next = 0; + next = act[i + 1].first - act[i].first - 1; + rmax(maximal[i][j], dp[i][j][l] + l * next); + rmax(ans, maximal[i][j]); + } + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 2/A. Extract Numbers.cpp b/Educational Codeforces Round 2/A. Extract Numbers.cpp new file mode 100644 index 0000000..7dfad7e --- /dev/null +++ b/Educational Codeforces Round 2/A. Extract Numbers.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/contest/600/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string a; + cin >> a; + + V words; + V nums; + + string cur; + + bool isnum = true; + repv(i, a) { + if (isalnum(i) || i == '.') { + cur.push_back(i); + isnum = isnum && isdigit(i); + continue; + } + + if (!isnum || cur.empty() || (cur[0] == '0' && cur.size() > 1)) { + words.emplace_back(cur); + cur.clear(); + isnum = true; + continue; + } + + nums.emplace_back(cur); + cur.clear(); + isnum = true; + } + + if (!isnum || cur.empty() || (cur[0] == '0' && cur.size() > 1)) { + words.emplace_back(cur); + } else { + nums.emplace_back(cur); + } + + auto print = [&](V &v) { + if (v.empty()) { + cout << "-\n"; + } else { + cout << '"'; + cout << v[0]; + nrep(i, 1, v.size()) { + cout << ',' << v[i]; + } + cout << "\"\n"; + } + }; + + print(nums); + print(words); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 2/B. Queries about less or equal elements.cpp b/Educational Codeforces Round 2/B. Queries about less or equal elements.cpp new file mode 100644 index 0000000..21336e6 --- /dev/null +++ b/Educational Codeforces Round 2/B. Queries about less or equal elements.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/contest/600/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl a(n); + vl b(m); + cin >> a >> b; + + sortv(a); + + rep(i, m) { + cout << upper_bound(all(a), b[i]) - a.begin() << " \n"[i == m - 1]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 2/C. Make Palindrome.cpp b/Educational Codeforces Round 2/C. Make Palindrome.cpp new file mode 100644 index 0000000..bc65adf --- /dev/null +++ b/Educational Codeforces Round 2/C. Make Palindrome.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/contest/600/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string a; + cin >> a; + + int n = a.size(); + + vi count(26); + + repv(i, a) { + count[i - 'a']++; + } + + vi odds; + rep(i, 26) { + if (count[i] & 1) { + odds.push_back(i); + } + } + + rep(i, odds.size() >> 1) { + count[odds[i]]++; + count[odds[odds.size() - i - 1]]--; + } + + vi now = count; + now[0] >>= 1; + + int cur = 0; + rep(i, n >> 1) { + while (!now[cur]) { + cur++; + now[cur] >>= 1; + } + + a[i] = cur + 'a'; + now[cur]--; + } + + if (n & 1) { + a[n >> 1] = odds[odds.size() >> 1] + 'a'; + } + + count[25] >>= 1; + cur = 25; + nrep(i, (n >> 1) + (n & 1), n) { + while (!count[cur]) { + cur--; + count[cur] >>= 1; + } + + a[i] = cur + 'a'; + count[cur]--; + } + + cout << a << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 2/E. Lomsat gelral.cpp b/Educational Codeforces Round 2/E. Lomsat gelral.cpp new file mode 100644 index 0000000..a28babd --- /dev/null +++ b/Educational Codeforces Round 2/E. Lomsat gelral.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/contest/600/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + vvi graph(n); + + vl ans(n); + vl big(n); + vi color(n); + + cin >> color; + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + V> count(n); + + function dfs = [&](int i, int p) { + ans[i] = color[i]; + big[i] = 1; + count[i][color[i]] = 1; + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + + ll act = ans[j]; + + if (count[j].size() > count[i].size()) { + swap(count[i], count[j]); + swap(big[i], big[j]); + swap(ans[i], ans[j]); + } + + repv(k, count[j]) { + ll &cur = count[i][k.first]; + cur += k.second; + + if (cur > big[i]) { + big[i] = cur; + ans[i] = k.first; + continue; + } + + if (cur == big[i]) { + ans[i] += k.first; + } + } + + ans[j] = act; + } + }; + + dfs(0, 0); + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2025/A. Yes or Yes.cpp b/Good Bye 2025/A. Yes or Yes.cpp new file mode 100644 index 0000000..d89d748 --- /dev/null +++ b/Good Bye 2025/A. Yes or Yes.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/2178/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string s; + cin >> s; + + cout << (count(all(s), 'Y') > 1 ? "NO\n" : "YES\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2025/B. Impost or Sus.cpp b/Good Bye 2025/B. Impost or Sus.cpp new file mode 100644 index 0000000..f72bab6 --- /dev/null +++ b/Good Bye 2025/B. Impost or Sus.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2178/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string s; + cin >> s; + + int ans = 0; + + if (s[0] == 'u') { + s[0] = 's'; + ans++; + } + + if (s.back() == 'u') { + s.back() = 's'; + ans++; + } + + int c = 0; + rep(i, s.size()) { + if (s[i] == 's') { + ans += c >> 1; + c = 0; + continue; + } + + c++; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2025/C. First or Second.cpp b/Good Bye 2025/C. First or Second.cpp new file mode 100644 index 0000000..07eaa83 --- /dev/null +++ b/Good Bye 2025/C. First or Second.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2178/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + vl pref(n + 1); + pref[1] = a[0]; + + nrep(i, 1, n - 1) { + pref[i + 1] = pref[i] + abs(a[i]); + } + + ll ans = pref[n - 1]; + ll cur = a[n - 1]; + + for (int i = n - 2; i >= 0; i--) { + rmax(ans, pref[i] - cur); + cur += a[i]; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2025/D. Xmas or Hysteria.cpp b/Good Bye 2025/D. Xmas or Hysteria.cpp new file mode 100644 index 0000000..bff7d37 --- /dev/null +++ b/Good Bye 2025/D. Xmas or Hysteria.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/contest/2178/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl a(n); + cin >> a; + + if (m > (n >> 1)) { + cout << "-1\n"; + return; + } + + V> ans; + + vi perm(n); + rep(i, n) { + perm[i] = i; + } + + sort(all(perm), [&](int i, int j){ + return a[i] < a[j]; + }); + + auto printans = [&]() { + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } + }; + + if (m == 0) { + int r = perm.back(); + + int cur = 0; + while (cur < n - 2 && a[r] > a[perm[n - 2]]) { + ans.emplace_back(perm[cur] + 1, r + 1); + a[r] -= a[perm[cur]]; + cur++; + } + + if (a[r] > a[perm[n - 2]]) { + cout << "-1\n"; + return; + } + + while (cur < n - 1) { + ans.emplace_back(perm[cur] + 1, perm[cur + 1] + 1); + cur++; + } + + printans(); + return; + } + + int lim = n - m * 2; + rep(i, lim) { + ans.emplace_back(perm[i] + 1, perm[i + 1] + 1); + } + + nrep(i, n - m, n) { + ans.emplace_back(perm[i] + 1, perm[i - m] + 1); + } + + printans(); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Good Bye 2025/E. Flatten or Concatenate.cpp b/Good Bye 2025/E. Flatten or Concatenate.cpp new file mode 100644 index 0000000..5dcf553 --- /dev/null +++ b/Good Bye 2025/E. Flatten or Concatenate.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/2178/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + auto ask = [&](int l, int r) { + cout << "? " << l << ' ' << r << endl; + ll ans; + cin >> ans; + return ans; + }; + + auto answer = [&](ll ans) { + cout << "! " << ans << endl; + fflush(stdout); + }; + + int n; + cin >> n; + + int l = 1; + int r = n; + + while (l != r) { + int low = l; + int high = r; + int ans = l; + + ll total = ask(l, r); + + while (low <= high) { + int mid = (low + high) >> 1; + + ll cur = ask(l, mid); + + if (cur * 2 <= total) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + int sz1 = (ans - l + 1); + int sz2 = (r - ans); + + if (sz1 <= sz2) { + r = ans; + continue; + } + + l = ans + 1; + } + + answer(ask(l, r)); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Hello 2026/A. Binary Array Game.cpp b/Hello 2026/A. Binary Array Game.cpp new file mode 100644 index 0000000..b28281e --- /dev/null +++ b/Hello 2026/A. Binary Array Game.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2183/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi a(n); + cin >> a; + + cout << (a[0] == 1 || a.back() == 1 ? "Alice\n" : "Bob\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Hello 2026/B. Yet Another MEX Problem.cpp b/Hello 2026/B. Yet Another MEX Problem.cpp new file mode 100644 index 0000000..334b69a --- /dev/null +++ b/Hello 2026/B. Yet Another MEX Problem.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2183/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vi va(n + 2); + rep(i, n) { + int n; + cin >> n; + va[n] = 1; + } + + int mex = 0; + while (va[mex]) { + mex++; + } + + cout << min(mex, k - 1) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Hello 2026/C. War Strategy.cpp b/Hello 2026/C. War Strategy.cpp new file mode 100644 index 0000000..6c500bc --- /dev/null +++ b/Hello 2026/C. War Strategy.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/contest/2183/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + ll m; + cin >> n >> m >> k; + + int ans = 1; + int l = 0; + int r = 0; + + bool upd = true; + while (upd) { + upd = false; + + if (k - l != 1) { + ll cost = max(l + 1, r) + l + r; + if (cost <= m) { + l++; + ans++; + upd = true; + } + } + + if (k + r != n) { + ll cost = max(l, r + 1) + l + r; + if (cost <= m) { + r++; + ans++; + upd = true; + } + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Hello 2026/D1. Tree Coloring (Easy Version).cpp b/Hello 2026/D1. Tree Coloring (Easy Version).cpp new file mode 100644 index 0000000..fd25a73 --- /dev/null +++ b/Hello 2026/D1. Tree Coloring (Easy Version).cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/2183/problem/D1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + int ans = 1; + + vi depth(n); + + V> par(n); + + function dfs = [&](int i, int p, int d) { + depth[d]++; + rmax(ans, depth[d]); + + par[d].insert(p); + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i, d + 1); + } + }; + + repv(i, graph[0]) { + dfs(i, 0, 0); + } + + rep(i, n) { + if (par[i].size() == 1) { + rmax(ans, depth[i] + 1); + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C3. Game on Tree (Hard).cpp b/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C3. Game on Tree (Hard).cpp new file mode 100644 index 0000000..be88035 --- /dev/null +++ b/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C3. Game on Tree (Hard).cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/contest/1970/problem/C3 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, t; + cin >> n >> t; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + V win(n); + + function dfs = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + win[i] = !dfs(j, i) || win[i]; + } + + return win[i]; + }; + + dfs(0, 0); + + V dp(n); + + function reroot = [&](int i, int p) { + int c = 0; + repv(j, graph[i]) { + c += !win[j]; + } + + dp[i] = c > 0; + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + bool prev = win[i]; + if (!win[j] && c == 1) { + win[i] = false; + } + + if (!win[i]) { + win[j] = true; + } + + reroot(j, i); + win[i] = prev; + } + }; + + reroot(0, 0); + + while (t--) { + int v; + cin >> v; + v--; + + cout << (dp[v] ? "Ron\n" : "Hermione\n"); + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/C. Triple Removal.cpp b/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/C. Triple Removal.cpp new file mode 100644 index 0000000..a242c4b --- /dev/null +++ b/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/C. Triple Removal.cpp @@ -0,0 +1,169 @@ +/* Problem URL: https://codeforces.com/contest/2152/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + + vi a(n); + cin >> a; + + if (n == 1) { + while (q--) { + int a, b; + cin >> a >> b; + cout << "-1\n"; + } + return; + } + + V val(n); + + nrep(i, 1, n - 1) { + if (a[i] != a[i - 1] && a[i] != a[i + 1]) { + val[i] = true; + } + } + + if (a[0] != a[1]) { + val[0] = true; + } + + if (a[n - 1] != a[n - 2]) { + val[n - 1] = true; + } + + vi count(n + 1); + rep(i, n) { + count[i + 1] = count[i] + a[i]; + } + + vvi sparse(20, vi(n)); + rep(i, n) { + sparse[0][i] = val[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[j][i] = sparse[j - 1][i] && sparse[j - 1][i + (1 << (j - 1))]; + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return sparse[log][l] & sparse[log][r - (1 << log) + 1]; + }; + + while (q--) { + int l, r; + cin >> l >> r; + l--, r--; + + if ((r - l + 1) % 3 || (count[r + 1] - count[l]) % 3) { + cout << "-1\n"; + continue; + } + + cout << (r - l + 1) / 3 + (query(l + 1, r - 1) & (a[l] != a[l + 1]) & (a[r] != a[r - 1])) << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}