From 97fbb64a0212799b88dd9ff92441bec533d97110 Mon Sep 17 00:00:00 2001 From: Macesuted Date: Mon, 5 Aug 2024 19:50:33 +0800 Subject: [PATCH] =?UTF-8?q?HDOJ:=202024=E2=80=9C=E9=92=89=E8=80=99?= =?UTF-8?q?=E7=BC=96=E7=A8=8B=E2=80=9D=E4=B8=AD=E5=9B=BD=E5=A4=A7=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E7=AE=97=E6=B3=95=E8=AE=BE=E8=AE=A1=E8=B6=85=E7=BA=A7?= =?UTF-8?q?=E8=81=94=E8=B5=9B=EF=BC=886=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HDOJ/7494.cpp | 46 ++++++++++++++++ HDOJ/7495.cpp | 84 +++++++++++++++++++++++++++++ HDOJ/7502.cpp | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 HDOJ/7494.cpp create mode 100644 HDOJ/7495.cpp create mode 100644 HDOJ/7502.cpp diff --git a/HDOJ/7494.cpp b/HDOJ/7494.cpp new file mode 100644 index 0000000..38b9ee0 --- /dev/null +++ b/HDOJ/7494.cpp @@ -0,0 +1,46 @@ +/** + * @file 7494.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-08-05 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define maxn 200005 + +vector> graph; + +void solve(void) { + int n; + cin >> n; + graph.clear(), graph.resize(n + 1); + for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x); + for (int i = 1; i <= n; i++) { + if (graph[i].size() != 2) continue; + int p = graph[i][0], q = graph[i][1], target = n - 1; + if (graph[p].size() == 2) { + int p2 = graph[p][graph[p][0] == i]; + if (graph[p2].size() != 1) p = p2, target--; + } + if (graph[q].size() == 2) { + int q2 = graph[q][graph[q][0] == i]; + if (graph[q2].size() != 1) q = q2, target--; + } + if ((int)graph[p].size() + (int)graph[q].size() == target) return cout << "Yes" << endl, void(); + } + return cout << "No" << endl, void(); +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/HDOJ/7495.cpp b/HDOJ/7495.cpp new file mode 100644 index 0000000..e3c23c4 --- /dev/null +++ b/HDOJ/7495.cpp @@ -0,0 +1,84 @@ +/** + * @file 7495.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-08-05 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define maxn 10005 + +vector> graph; +int cnt[maxn], deg[maxn], tot, tot1; + +void update(int p, int delt) { + if (cnt[p] == deg[p]) (deg[p] == 1 ? tot1 : tot) += delt * (cnt[p] + 1); + return; +} + +void solve(void) { + int n, m; + cin >> n >> m; + graph.clear(), graph.resize(n + 1), tot = tot1 = 0; + for (int i = 1, x, y; i <= m; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x); + for (int i = 1; i <= n; i++) cnt[i] = 0, deg[i] = graph[i].size(); + for (int i = 1; i <= n; i++) + if (graph[i].size() == 1) cnt[graph[i][0]]++; + for (int i = 1; i <= n; i++) update(i, +1); + bool vis = false; + for (int i = 1; i <= n; i++) { + update(i, -1); + if (graph[i].empty()) { + if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true; + } else if (graph[i].size() == 1) { + int j = graph[i][0]; + update(graph[i][0], -1), cnt[graph[i][0]]--, deg[graph[i][0]]--, update(graph[i][0], +1); + if (graph[j].size() == 2) { + int x = graph[j][graph[j][0] == i]; + update(x, -1), cnt[x]++, update(x, +1); + } + if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true; + if (graph[j].size() == 2) { + int x = graph[j][graph[j][0] == i]; + update(x, -1), cnt[x]--, update(x, +1); + } + update(graph[i][0], -1), cnt[graph[i][0]]++, deg[graph[i][0]]++, update(graph[i][0], +1); + } else { + for (auto j : graph[i]) { + update(j, -1), deg[j]--, update(j, +1); + if (graph[j].size() == 2) { + int x = graph[j][graph[j][0] == i]; + update(x, -1), cnt[x]++, update(x, +1); + } + } + + if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true; + + for (auto j : graph[i]) { + update(j, -1), deg[j]++, update(j, +1); + if (graph[j].size() == 2) { + int x = graph[j][graph[j][0] == i]; + update(x, -1), cnt[x]--, update(x, +1); + } + } + } + update(i, +1); + } + if (!vis) cout << -1; + cout << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/HDOJ/7502.cpp b/HDOJ/7502.cpp new file mode 100644 index 0000000..c9d3130 --- /dev/null +++ b/HDOJ/7502.cpp @@ -0,0 +1,145 @@ +/** + * @file 7502.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-08-05 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define maxn 500005 + +int64_t TIME; + +struct Func { + mutable int64_t k, b; + + Func(void) : k(0), b(0) {} + Func(int64_t _k, int64_t _b) : k(_k), b(_b) {} + + int64_t getVal(void) const { return k * TIME + b; } + + bool operator<(const Func& o) const { return getVal() < o.getVal() || (getVal() == o.getVal() && k < o.k); } + Func operator+(const Func& o) const { return Func(k + o.k, b + o.b); } + Func operator-(const Func& o) const { return Func(k - o.k, b - o.b); } + Func& operator+=(const Func& o) { return *this = *this + o; } + Func& operator-=(const Func& o) { return *this = *this - o; } +}; + +int64_t t[maxn], l[maxn], r[maxn]; + +multiset S; +set> opTim; +Func ans; + +int64_t cross(Func a, Func b) { return (b.b - a.b + 1) / 2; } + +void insertL(set::iterator p) { + if (p == S.begin()) return; + auto q = p; + q--; + opTim.emplace(cross(*q, *p), *q); + return; +} +void eraseL(set::iterator p) { + if (p == S.begin()) return; + auto q = p; + q--; + opTim.erase({cross(*q, *p), *q}); + return; +} +void insertR(set::iterator p) { + auto q = p; + q++; + if (q == S.end()) return; + opTim.emplace(cross(*p, *q), *p); + return; +} +void eraseR(set::iterator p) { + auto q = p; + q++; + if (q == S.end()) return; + opTim.erase({cross(*p, *q), *p}); + return; +} + +void print(void) { + cerr << "PRINT " << TIME << endl; + for (auto [k, b] : S) cerr << '(' << k << ',' << b << ',' << k * TIME + b << ')'; + cerr << endl; + for (auto [tim, Func] : opTim) cerr << "(" << tim << ' ' << Func.k << ' ' << Func.b << ')'; + cerr << endl; + cerr << ans.k << ' ' << ans.b << ' ' << ans.getVal() + S.size() / 2 << endl; + return; +} + +void insert(int l, int r) { + auto pl = S.lower_bound({-1, l + TIME}); + if (pl == S.end()) return; + if (pl->getVal() > r) { + if (pl->k == -1) return; + auto it = S.emplace(+1, l - 1 - TIME); + it = S.emplace(-1, r + 1 + TIME); + insertL(it); + ans += Func{+1, l - 1 - TIME}; + ans -= Func{-1, r + 1 + TIME}; + return; + } + auto pr = --S.lower_bound({-1, r + 1 + TIME}); + if (pl->k == +1) eraseR(pl), ans -= *pl, pl->b = l - 1 - TIME, ans += *pl, insertR(pl), pl++; + if (pr->k == -1) + eraseL(pr), ans += *pr, pr->b = r + 1 + TIME, ans -= *pr, insertL(pr); + else + pr++; + for (auto i = pl; i != pr;) { + eraseL(i); + ans += *i, i = S.erase(i); + eraseR(i); + ans -= *i, i = S.erase(i); + if (i != S.end()) insertL(i); + } + return; +} + +void update(set>::iterator it) { + auto p = S.find(it->second); + ans -= *p, p = S.erase(p); + ans += *p, p = S.erase(p); + opTim.erase(it); + return; +} + +void solve(void) { + int n, q; + cin >> n >> q; + for (int i = 1; i <= n; i++) cin >> t[i] >> l[i] >> r[i]; + TIME = 0, S.clear(), S.insert({-1, 0}), S.insert({+1, 0}), opTim.clear(), opTim.emplace(t[n + 1] = INT64_MAX, Func{0, 0}); + ans = Func(2, 0); + for (int i = 1, j = 1; i <= q; i++) { + int64_t qt; + cin >> qt; + while (min(t[j], opTim.begin()->first) <= qt) { + if (j <= n && t[j] < opTim.begin()->first) + TIME = t[j], insert(l[j], r[j]), j++; + else + TIME = opTim.begin()->first - 1, update(opTim.begin()); + } + TIME = qt; + cout << ans.getVal() + S.size() / 2 << ' '; + } + cout << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file