A. Qualifiers Ranking Rules
签到。
D. Transitivity
并查集,分类讨论。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
#define range(a) begin(a), end(a)
struct UnionFind {
int n;
vector<int> f, sz;
UnionFind(const int &n = 0) : n(n), f(n), sz(n, 1) {
iota(f.begin(), f.end(), 0);
}
int get(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x != y) {
f[y] = x;
sz[x] += sz[y];
return 1;
}
return 0;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
UnionFind f(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
f.unite(u, v);
}
vector<int> v;
vector<int> vis(n);
for (int i = 0; i < n; i++) {
int j = f.get(i);
if (!vis[j]) {
v.push_back(f.sz[j]);
vis[j] = 1;
}
}
i64 sum = 0;
for (auto &vi : v) {
sum += i64(vi) * (vi - 1) / 2;
}
if (sum > m) {
cout << sum - m << '\n';
} else {
sort(range(v));
cout << i64(v[0]) * v[1] << '\n';
}
return 0;
}G. Spanning Tree
并查集。
记生成树的总方案为 ,若最终可以生成所给的树则答案为 ,否则为 。
复杂度 。
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
struct UnionFind {
int n;
vector<int> f, sz;
UnionFind(const int &n = 0) : n(n), f(n), sz(n, 1) {
iota(f.begin(), f.end(), 0);
}
int get(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x != y) {
f[y] = x;
sz[x] += sz[y];
return 1;
}
return 0;
}
};
constexpr int P = 998244353;
int power(int a, int b, int p = P) {
int res = 1;
for (; b; b >>= 1, a = i64(a) * a % p) {
if (b & 1) {
res = i64(res) * a % p;
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n - 1), b(n - 1), c(n - 1), d(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
}
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
cin >> c[i] >> d[i];
c[i]--, d[i]--;
g[c[i]].push_back(d[i]);
g[d[i]].push_back(c[i]);
}
vector<int> p(n, 0), dep(n);
function<void(int, int)> dfs = [&](int cur, int pre) {
for (auto &nex : g[cur]) {
if (nex != pre) {
p[nex] = cur;
dep[nex] = dep[cur] + 1;
dfs(nex, cur);
}
}
};
dfs(0, -1);
UnionFind f(n);
int ans = 1;
for (int i = 0; i < n - 1; i++) {
int u = f.get(a[i]), v = f.get(b[i]);
if (dep[u] > dep[v]) {
swap(u, v);
}
if (f.get(p[v]) != u) {
cout << "0\n";
return 0;
}
ans = i64(ans) * f.sz[u] % P * f.sz[v] % P;
f.unite(u, v);
}
cout << power(ans, P - 2) << '\n';
return 0;
}I. Pa?sWorD
。
J. Minimum Manhattan Distance
计算几何,记两圆心曼哈顿距离为 ,答案为 。
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using Point = complex<double>;
#define x real
#define y imag
void solve() {
vector<Point> a(4);
for (int i = 0; i < 4; i++) {
int x, y;
cin >> x >> y;
a[i] = Point(x, y);
}
vector<Point> c(2);
for (int i = 0; i < 4; i += 2) {
c[i / 2] = (a[i] + a[i + 1]) / 2.0;
}
cout << abs(x(c[0]) - x(c[1])) + abs(y(c[0]) - y(c[1])) - sqrt(2.0) * abs(a[2] - a[3]) / 2.0 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(12);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}K. Minimum Euclidean Distance
计算几何,在凸包内(包含边缘)找一个点到圆心距离最短,记最短距离为 ,圆的直径为 ,答案即为 。
复杂度 。
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using Point = complex<double>;
#define x real
#define y imag
constexpr double eps = 1E-10;
int sign(const double &x) {
return x < -eps ? -1 : x > eps ? 1 : 0;
}
auto cross(const Point &a, const Point &b) {
return imag(conj(a) * b);
}
auto dot(const Point &a, const Point &b) {
return real(conj(a) * b);
}
struct Line {
Point a, b;
Line() = default;
Line(const Point &a, const Point &b) : a(a), b(b) {}
};
int contains(const Point &p, const vector<Point> &g) {
int n = g.size();
bool in = false;
for (int i = 0; i < n; i++) {
Point a = g[i] - p, b = g[(i + 1) % n] - p;
if (y(a) > y(b)) {
swap(a, b);
}
if (y(a) <= 0 && y(b) > 0 && sign(cross(a, b)) < 0) {
in = !in;
}
if (sign(cross(a, b)) == 0 && sign(dot(a, b)) <= 0) {
return -1;
}
}
return in ? 1 : 0;
}
double get(const Point &p, const vector<Point> &g) {
int n = g.size();
double res = 1E18;
for (int i = 0; i < n; i++) {
Point u = g[i], v = g[(i + 1) % n];
double dis = abs(cross(p - v, u - v) / abs(u - v));
if (sign(dot(p - u, u - v)) * sign(dot(p - v, u - v)) <= 0) {
res = min(res, dis);
} else {
res = min({res, abs(p - u), abs(p - v)});
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(12);
int n, q;
cin >> n >> q;
vector<Point> a(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[i] = Point(x, y);
}
for (int i = 0; i < q; i++) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
Point p0 = Point(x0, y0);
Point p1 = Point(x1, y1);
Point c = (p0 + p1) / 2.0;
double b2 = norm(p1 - p0);
int in = contains(c, a);
double d = 0;
if (!in) {
d = get(c, a);
}
cout << b2 / 8.0 + d * d << '\n';
}
return 0;
}L. KaChang!
签到。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
#define range(a) begin(a), end(a)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int mx = *max_element(range(a));
cout << max(2, (mx + x - 1) / x) << '\n';
return 0;
}