feat: new graph problem partially formatted.

This commit is contained in:
2026-05-28 21:27:41 -03:00
parent 4d583f8d8c
commit d931bb6422
21 changed files with 6761 additions and 0 deletions

96
pique-pega/src/ac.cpp Normal file
View File

@@ -0,0 +1,96 @@
// https://codeforces.com/contest/1405/problem/D
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
const int MAXN = 1e5 + 10;
int n, a, b, da, db, distAtoB;
int dist[MAXN];
void dfs(int x, int p, vector<vi> &g)
{
for (auto n : g[x])
{
if (n != p)
{
dist[n] = dist[x] + 1;
dfs(n, x, g);
}
}
}
pair<int, int> farthest(int x)
{
int maxD = INT_MIN, v = -1;
for (int i = 1; i <= n; i++)
{
if (dist[i] > maxD)
{
maxD = dist[i];
v = i;
}
}
return {v, maxD};
}
int diameter(vector<vi> &g)
{
dist[a] = 0;
dfs(a, -1, g);
auto [v, d] = farthest(1);
distAtoB = dist[b];
dist[v] = 0;
dfs(v, -1, g);
auto [_, diam] = farthest(v);
return diam;
}
void solve()
{
cin >> n >> a >> b >> da >> db;
vector<vi> g(n + 1);
for (int i = 1; i <= n - 1; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int diam = diameter(g);
if (2 * da >= diam || da >= distAtoB)
{
cout << "Delegado" << endl;
return;
}
db = min(db, diam);
if (db > 2 * da)
{
cout << "Noivo" << endl;
return;
}
cout << "Delegado" << endl;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int testCases = 1;
cin >> testCases;
for (int t = 1; t <= testCases; t++)
{
// cout << "Case #" << t << ": ";
solve();
}
return 0;
}

View File

@@ -0,0 +1,49 @@
from collections import deque
t = int(input())
for _ in range(t):
n, a, b, da, db = map(int, input().split())
adj = [[] for _ in range(n + 1)]
for _ in range(n - 1):
u, v = map(int, input().split())
adj[u].append(v)
adj[v].append(u)
if 2 * da >= db:
print("Alice")
continue
def bfs(start):
dist = [-1] * (n + 1)
dist[start] = 0
q = deque([start])
furthest_node = start
max_d = 0
while q:
u = q.popleft()
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
q.append(v)
if dist[v] > max_d:
max_d = dist[v]
furthest_node = v
return dist, furthest_node, max_d
dist_from_a, furthest_from_a, _ = bfs(a)
if dist_from_a[b] <= da:
print("Alice")
continue
_, _, diameter = bfs(furthest_from_a)
if diameter <= 2 * da:
print("Alice")
else:
print("Bob")

View File

@@ -0,0 +1,26 @@
#include "testlib.h"
#include <string>
using namespace std;
const string YES = "Delegado";
const string NO = "Noivo";
int main(int argc, char *argv[]) {
setName("%s", (YES + " or " + NO + " (case insensitive)").c_str());
registerTestlibCmd(argc, argv);
std::string ja = upperCase(ans.readWord());
std::string pa = upperCase(ouf.readWord());
if (ja != YES && ja != NO)
quitf(_fail, "%s or %s expected in answer, but %s found", YES.c_str(), NO.c_str(), compress(ja).c_str());
if (pa != YES && pa != NO)
quitf(_pe, "%s or %s expected, but %s found", YES.c_str(), NO.c_str(), compress(pa).c_str());
if (ja != pa)
quitf(_wa, "expected %s, found %s", compress(ja).c_str(), compress(pa).c_str());
quitf(_ok, "answer is %s", ja.c_str());
}

View File

@@ -0,0 +1,83 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
const int MIN_N = 0;
const int MAX_N = 100;
const int rnd_test_n = 100;
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
dest.insert(dest.end(), orig.begin(), orig.end());
}
string output_tc(int x, int y) {
ostringstream oss;
oss << x << " " << y << endl;
return oss.str();
}
vector<string> generate_sample_tests() {
vector<string> tests;
tests.push_back(output_tc(1, 1));
tests.push_back(output_tc(2, 2));
tests.push_back(output_tc(0, 0));
return tests;
}
vector<string> generate_manual_tests() {
vector<string> tests;
tests.push_back(output_tc(100, 0));
tests.push_back(output_tc(0, 100));
return tests;
}
string rnd_test(int i){
int min_n = MIN_N;
int max_n = MAX_N;
if(i<rnd_test_n / 3){
max_n = 5;
}
else if(i<rnd_test_n / 2){
max_n = 20;
}
int x = rnd.next(min_n, max_n);
int y = rnd.next(min_n, max_n);
return(output_tc(x, y));
}
vector<string> generate_random_tests() {
vector<string> tests;
for (int i = 0; i < rnd_test_n; i++){
tests.push_back(rnd_test(i));
}
return tests;
}
string extreme_test_1(){
return(output_tc(100, 100));
}
vector<string> generate_extreme_tests(){
vector<string> tests;
tests.push_back(extreme_test_1());
return tests;
}
int main(int argc, char *argv[]) {
registerGen(argc, argv, 1);
vector<string> tests;
size_t test = 0;
append(tests, generate_sample_tests());
append(tests, generate_manual_tests());
append(tests, generate_random_tests());
append(tests, generate_extreme_tests());
for (const auto &t : tests) {
startTest(++test);
cout << t;
}
return 0;
}

1
pique-pega/src/script.sh Normal file
View File

@@ -0,0 +1 @@
generator

5963
pique-pega/src/testlib.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,70 @@
#include "testlib.h"
#include <vector>
using namespace std;
struct DSU {
vector<int> parent;
DSU(int n) {
parent.resize(n + 1);
for (int i = 1; i <= n; i++) parent[i] = i;
}
int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
bool unite(int i, int j) {
int root_i = find(i);
int root_j = find(j);
if (root_i != root_j) {
parent[root_i] = root_j;
return true;
}
return false;
}
};
int main(int argc, char* argv[]) {
registerValidation(argc, argv);
int t = inf.readInt(1, 1e4, "t");
inf.readEoln();
int sum_n = 0;
for (int tc = 1; tc <= t; tc++) {
int n = inf.readInt(2, 1e5, "n");
inf.readSpace();
int a = inf.readInt(1, n, "a");
inf.readSpace();
int b = inf.readInt(1, n, "b");
ensuref(a != b, "a and b must be different");
inf.readSpace();
int da = inf.readInt(1, n - 1, "da");
inf.readSpace();
int db = inf.readInt(1, n - 1, "db");
inf.readEoln();
sum_n += n;
ensuref(sum_n <= 1e5, "Sum of n over all test cases must not exceed 10^5");
DSU dsu(n);
for (int i = 0; i < n - 1; i++) {
int u = inf.readInt(1, n, "u");
inf.readSpace();
int v = inf.readInt(1, n, "v");
inf.readEoln();
ensuref(u != v, "Edge must connect two different vertices");
ensuref(dsu.unite(u, v), "Graph contains a cycle or is disconnected (not a tree)");
}
}
inf.readEof();
return 0;
}