Skip to content

Commit

Permalink
HDOJ: 2024“钉耙编程”中国大学生算法设计超级联赛(6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Macesuted committed Aug 5, 2024
1 parent 5b18b89 commit 97fbb64
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 0 deletions.
46 changes: 46 additions & 0 deletions HDOJ/7494.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file 7494.cpp
* @author Macesuted ([email protected])
* @date 2024-08-05
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define maxn 200005

vector<vector<int>> 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;
}
84 changes: 84 additions & 0 deletions HDOJ/7495.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @file 7495.cpp
* @author Macesuted ([email protected])
* @date 2024-08-05
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define maxn 10005

vector<vector<int>> 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;
}
145 changes: 145 additions & 0 deletions HDOJ/7502.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @file 7502.cpp
* @author Macesuted ([email protected])
* @date 2024-08-05
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
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<Func> S;
set<pair<int64_t, Func>> opTim;
Func ans;

int64_t cross(Func a, Func b) { return (b.b - a.b + 1) / 2; }

void insertL(set<Func>::iterator p) {
if (p == S.begin()) return;
auto q = p;
q--;
opTim.emplace(cross(*q, *p), *q);
return;
}
void eraseL(set<Func>::iterator p) {
if (p == S.begin()) return;
auto q = p;
q--;
opTim.erase({cross(*q, *p), *q});
return;
}
void insertR(set<Func>::iterator p) {
auto q = p;
q++;
if (q == S.end()) return;
opTim.emplace(cross(*p, *q), *p);
return;
}
void eraseR(set<Func>::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<pair<int64_t, Func>>::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;
}

0 comments on commit 97fbb64

Please sign in to comment.