Skip to content

Commit 97fbb64

Browse files
committed
HDOJ: 2024“钉耙编程”中国大学生算法设计超级联赛(6)
1 parent 5b18b89 commit 97fbb64

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed

HDOJ/7494.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file 7494.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-08-05
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 200005
14+
15+
vector<vector<int>> graph;
16+
17+
void solve(void) {
18+
int n;
19+
cin >> n;
20+
graph.clear(), graph.resize(n + 1);
21+
for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x);
22+
for (int i = 1; i <= n; i++) {
23+
if (graph[i].size() != 2) continue;
24+
int p = graph[i][0], q = graph[i][1], target = n - 1;
25+
if (graph[p].size() == 2) {
26+
int p2 = graph[p][graph[p][0] == i];
27+
if (graph[p2].size() != 1) p = p2, target--;
28+
}
29+
if (graph[q].size() == 2) {
30+
int q2 = graph[q][graph[q][0] == i];
31+
if (graph[q2].size() != 1) q = q2, target--;
32+
}
33+
if ((int)graph[p].size() + (int)graph[q].size() == target) return cout << "Yes" << endl, void();
34+
}
35+
return cout << "No" << endl, void();
36+
}
37+
38+
int main() {
39+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
40+
41+
int _;
42+
cin >> _;
43+
while (_--) solve();
44+
45+
return 0;
46+
}

HDOJ/7495.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @file 7495.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-08-05
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 10005
14+
15+
vector<vector<int>> graph;
16+
int cnt[maxn], deg[maxn], tot, tot1;
17+
18+
void update(int p, int delt) {
19+
if (cnt[p] == deg[p]) (deg[p] == 1 ? tot1 : tot) += delt * (cnt[p] + 1);
20+
return;
21+
}
22+
23+
void solve(void) {
24+
int n, m;
25+
cin >> n >> m;
26+
graph.clear(), graph.resize(n + 1), tot = tot1 = 0;
27+
for (int i = 1, x, y; i <= m; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x);
28+
for (int i = 1; i <= n; i++) cnt[i] = 0, deg[i] = graph[i].size();
29+
for (int i = 1; i <= n; i++)
30+
if (graph[i].size() == 1) cnt[graph[i][0]]++;
31+
for (int i = 1; i <= n; i++) update(i, +1);
32+
bool vis = false;
33+
for (int i = 1; i <= n; i++) {
34+
update(i, -1);
35+
if (graph[i].empty()) {
36+
if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true;
37+
} else if (graph[i].size() == 1) {
38+
int j = graph[i][0];
39+
update(graph[i][0], -1), cnt[graph[i][0]]--, deg[graph[i][0]]--, update(graph[i][0], +1);
40+
if (graph[j].size() == 2) {
41+
int x = graph[j][graph[j][0] == i];
42+
update(x, -1), cnt[x]++, update(x, +1);
43+
}
44+
if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true;
45+
if (graph[j].size() == 2) {
46+
int x = graph[j][graph[j][0] == i];
47+
update(x, -1), cnt[x]--, update(x, +1);
48+
}
49+
update(graph[i][0], -1), cnt[graph[i][0]]++, deg[graph[i][0]]++, update(graph[i][0], +1);
50+
} else {
51+
for (auto j : graph[i]) {
52+
update(j, -1), deg[j]--, update(j, +1);
53+
if (graph[j].size() == 2) {
54+
int x = graph[j][graph[j][0] == i];
55+
update(x, -1), cnt[x]++, update(x, +1);
56+
}
57+
}
58+
59+
if (tot + tot1 / 2 == n - 1) cout << i << ' ', vis = true;
60+
61+
for (auto j : graph[i]) {
62+
update(j, -1), deg[j]++, update(j, +1);
63+
if (graph[j].size() == 2) {
64+
int x = graph[j][graph[j][0] == i];
65+
update(x, -1), cnt[x]--, update(x, +1);
66+
}
67+
}
68+
}
69+
update(i, +1);
70+
}
71+
if (!vis) cout << -1;
72+
cout << endl;
73+
return;
74+
}
75+
76+
int main() {
77+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
78+
79+
int _;
80+
cin >> _;
81+
while (_--) solve();
82+
83+
return 0;
84+
}

HDOJ/7502.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* @file 7502.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-08-05
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 500005
14+
15+
int64_t TIME;
16+
17+
struct Func {
18+
mutable int64_t k, b;
19+
20+
Func(void) : k(0), b(0) {}
21+
Func(int64_t _k, int64_t _b) : k(_k), b(_b) {}
22+
23+
int64_t getVal(void) const { return k * TIME + b; }
24+
25+
bool operator<(const Func& o) const { return getVal() < o.getVal() || (getVal() == o.getVal() && k < o.k); }
26+
Func operator+(const Func& o) const { return Func(k + o.k, b + o.b); }
27+
Func operator-(const Func& o) const { return Func(k - o.k, b - o.b); }
28+
Func& operator+=(const Func& o) { return *this = *this + o; }
29+
Func& operator-=(const Func& o) { return *this = *this - o; }
30+
};
31+
32+
int64_t t[maxn], l[maxn], r[maxn];
33+
34+
multiset<Func> S;
35+
set<pair<int64_t, Func>> opTim;
36+
Func ans;
37+
38+
int64_t cross(Func a, Func b) { return (b.b - a.b + 1) / 2; }
39+
40+
void insertL(set<Func>::iterator p) {
41+
if (p == S.begin()) return;
42+
auto q = p;
43+
q--;
44+
opTim.emplace(cross(*q, *p), *q);
45+
return;
46+
}
47+
void eraseL(set<Func>::iterator p) {
48+
if (p == S.begin()) return;
49+
auto q = p;
50+
q--;
51+
opTim.erase({cross(*q, *p), *q});
52+
return;
53+
}
54+
void insertR(set<Func>::iterator p) {
55+
auto q = p;
56+
q++;
57+
if (q == S.end()) return;
58+
opTim.emplace(cross(*p, *q), *p);
59+
return;
60+
}
61+
void eraseR(set<Func>::iterator p) {
62+
auto q = p;
63+
q++;
64+
if (q == S.end()) return;
65+
opTim.erase({cross(*p, *q), *p});
66+
return;
67+
}
68+
69+
void print(void) {
70+
cerr << "PRINT " << TIME << endl;
71+
for (auto [k, b] : S) cerr << '(' << k << ',' << b << ',' << k * TIME + b << ')';
72+
cerr << endl;
73+
for (auto [tim, Func] : opTim) cerr << "(" << tim << ' ' << Func.k << ' ' << Func.b << ')';
74+
cerr << endl;
75+
cerr << ans.k << ' ' << ans.b << ' ' << ans.getVal() + S.size() / 2 << endl;
76+
return;
77+
}
78+
79+
void insert(int l, int r) {
80+
auto pl = S.lower_bound({-1, l + TIME});
81+
if (pl == S.end()) return;
82+
if (pl->getVal() > r) {
83+
if (pl->k == -1) return;
84+
auto it = S.emplace(+1, l - 1 - TIME);
85+
it = S.emplace(-1, r + 1 + TIME);
86+
insertL(it);
87+
ans += Func{+1, l - 1 - TIME};
88+
ans -= Func{-1, r + 1 + TIME};
89+
return;
90+
}
91+
auto pr = --S.lower_bound({-1, r + 1 + TIME});
92+
if (pl->k == +1) eraseR(pl), ans -= *pl, pl->b = l - 1 - TIME, ans += *pl, insertR(pl), pl++;
93+
if (pr->k == -1)
94+
eraseL(pr), ans += *pr, pr->b = r + 1 + TIME, ans -= *pr, insertL(pr);
95+
else
96+
pr++;
97+
for (auto i = pl; i != pr;) {
98+
eraseL(i);
99+
ans += *i, i = S.erase(i);
100+
eraseR(i);
101+
ans -= *i, i = S.erase(i);
102+
if (i != S.end()) insertL(i);
103+
}
104+
return;
105+
}
106+
107+
void update(set<pair<int64_t, Func>>::iterator it) {
108+
auto p = S.find(it->second);
109+
ans -= *p, p = S.erase(p);
110+
ans += *p, p = S.erase(p);
111+
opTim.erase(it);
112+
return;
113+
}
114+
115+
void solve(void) {
116+
int n, q;
117+
cin >> n >> q;
118+
for (int i = 1; i <= n; i++) cin >> t[i] >> l[i] >> r[i];
119+
TIME = 0, S.clear(), S.insert({-1, 0}), S.insert({+1, 0}), opTim.clear(), opTim.emplace(t[n + 1] = INT64_MAX, Func{0, 0});
120+
ans = Func(2, 0);
121+
for (int i = 1, j = 1; i <= q; i++) {
122+
int64_t qt;
123+
cin >> qt;
124+
while (min(t[j], opTim.begin()->first) <= qt) {
125+
if (j <= n && t[j] < opTim.begin()->first)
126+
TIME = t[j], insert(l[j], r[j]), j++;
127+
else
128+
TIME = opTim.begin()->first - 1, update(opTim.begin());
129+
}
130+
TIME = qt;
131+
cout << ans.getVal() + S.size() / 2 << ' ';
132+
}
133+
cout << endl;
134+
return;
135+
}
136+
137+
int main() {
138+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
139+
140+
int _;
141+
cin >> _;
142+
while (_--) solve();
143+
144+
return 0;
145+
}

0 commit comments

Comments
 (0)