0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 16:16:25 +00:00

Compare commits

..

4 Commits

11 changed files with 325 additions and 0 deletions

119
Luogu/T262837/T262837.cpp Normal file
View File

@ -0,0 +1,119 @@
#include <iostream>
#include <queue>
#include <stack>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1005;
int n, m, color[N], ans;
int cnt, id[N];
bool vis[N];
std::vector<int> g[N];
std::unordered_set<int> p[N];
bool dfs1(int u, int c) {
color[u] = c;
for (int v : g[u]) {
if (color[v]) {
if (color[v] == c) return false;
} else if (!dfs1(v, 3 - c)) {
return false;
}
}
return true;
}
bool check() {
for (int i = 1; i <= n; i++) {
if (!color[i]) {
if (!dfs1(i, 1)) return false;
}
}
return true;
}
void dfs2(int u, int x) {
id[u] = x;
p[x].insert(u);
for (int v : g[u]) {
if (!id[v]) dfs2(v, x);
}
}
int bfs(int x) {
std::fill_n(vis, N, false);
int res = 0;
std::queue<std::pair<int, int>> q;
q.emplace(x, 0);
vis[x] = true;
while (!q.empty()) {
auto e = q.front();
q.pop();
int u = e.first,
w = e.second;
res = std::max(res, w);
for (int v : g[u]) {
if (vis[v]) continue;
vis[v] = true;
q.emplace(v, w + 1);
}
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
if (!check()) {
cout << -1 << endl;
exit(0);
}
for (int i = 1; i <= n; i++) {
if (!id[i]) dfs2(i, ++cnt);
}
for (int i = 1; i <= cnt; i++) {
int res = 0;
for (const int &x : p[i]) {
res = std::max(res, bfs(x));
}
ans += res;
}
cout << ans << endl;
return 0;
}

BIN
Luogu/T262837/samples/merge.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/T262837/samples/merge.out (Stored with Git LFS) Normal file

Binary file not shown.

42
Luogu/T263082/T263082.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n, a[N], b[N];
std::vector<std::pair<int, int>> ans;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i] - a[i - 1];
}
b[n + 1] = -a[n];
for (int i = 1, j = 1; i <= n + 1; i++) {
while (b[i] < 0) {
while (j < i && b[j] <= 0) j++;
b[j]--, b[i]++;
ans.emplace_back(j, i - 1);
}
}
cout << ans.size() << endl;
for (const auto &e : ans) {
cout << e.first << ' ' << e.second << endl;
}
return 0;
}

BIN
Luogu/T263082/samples/checker.cpp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/T263082/samples/range.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/T263082/samples/range.out (Stored with Git LFS) Normal file

Binary file not shown.

74
Luogu/T263132/T263132.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 50005;
int n, k;
long long m, sum;
std::pair<long long, long long> a[N];
std::priority_queue<long long, std::vector<long long>, std::greater<long long>> q;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
std::sort(a + 1, a + n + 1, [](auto a, auto b) {
return a.second < b.second;
});
for (int i = 1; i <= k; i++) {
sum += a[i].second;
q.push(a[i].first - a[i].second);
if (sum > m) {
cout << i - 1 << endl;
exit(0);
} else if (i == n) {
cout << n << endl;
exit(0);
}
}
std::sort(a + k + 1, a + n + 1, [](auto a, auto b) {
return a.first < b.first;
});
for (int i = k + 1; i <= n; i++) {
auto x = q.top();
sum += a[i].first;
if (a[i].second + x < a[i].first) {
q.pop();
auto y = a[i].first - a[i].second;
q.push(y);
sum += x - y;
}
if (sum > m) {
cout << i - 1 << endl;
exit(0);
}
}
cout << n << endl;
return 0;
}

69
Luogu/T263133/T263133.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <cmath>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int t, n, k;
std::vector<int> top, g[N];
bool vis[N];
void dfs(int u) {
vis[u] = true;
for (int v : g[u]) {
if (!vis[v]) dfs(v);
}
top.push_back(u);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
while (t--) {
top.clear();
std::fill_n(g, N, std::vector<int>());
std::fill_n(vis, N, false);
int ans = 0;
cin >> n >> k;
for (int i = 2, x; i <= n; i++) {
cin >> x;
g[i].push_back(x);
g[x].push_back(i);
}
dfs(1);
std::fill_n(vis, N, false);
for (int u : top) {
if (k <= 1) break;
if (vis[u]) continue;
for (int v : g[u]) {
if (vis[v]) continue;
vis[u] = vis[v] = true;
ans++;
k -= 2;
}
}
cout << ans + k << endl;
}
return 0;
}

BIN
Luogu/T263133/samples/tree.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/T263133/samples/tree.out (Stored with Git LFS) Normal file

Binary file not shown.