diff --git a/contests/.gitkeep b/contests/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/contests/Cadernaveis/GINCAN11.cpp b/contests/Cadernaveis/GINCAN11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..accf32ec75457d025e131bc5ede6c76e23b457b9 --- /dev/null +++ b/contests/Cadernaveis/GINCAN11.cpp @@ -0,0 +1,52 @@ +#include <bits/stdc++.h> + +#define MAX 0 +#define MOD 1000000007 +#define inf 0x3f3f3f3f + +#define fi first +#define se second +#define sz size() +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +using namespace std; + +typedef long long ll; +typedef pair<int,int> ii; + +int cont[1010]; +vector<int> graph[1010]; + +void dfs(int x) { + cont[x] = 1; + for (auto i : graph[x]) + if (!cont[i]) + dfs(i); +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n, m, a, b; + cin >> n >> m; + for (int i = 0; i < m; ++i) { + cin >> a >> b; + a--, b--; + graph[a].pb(b); + graph[b].pb(a); + } + + int ans = 0; + for (int i = 0; i < n; ++i) { + if (!cont[i]) { + dfs(i); + ans++; + } + } + + cout << ans << '\n'; + return 0; +} diff --git a/contests/Cadernaveis/NTICKETS.cpp b/contests/Cadernaveis/NTICKETS.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c681d62fb68d4a9a3db63bbfa8a3307fb6fa0d4c --- /dev/null +++ b/contests/Cadernaveis/NTICKETS.cpp @@ -0,0 +1,109 @@ +#include <bits/stdc++.h> + +#define MAX 101010 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define MAXLOG 20 + +#define fi first +#define se second +#define sz size() +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +using namespace std; + +typedef long long ll; +typedef pair<int,ll> ii; + +vector<ii> graph[MAX]; + +ll h[MAX]; +ll par[MAX][MAXLOG], cost[MAX][MAXLOG]; + +void dfs(int v, int p = -1, ll c = 0) { + par[v][0] = p; + cost[v][0] = c; + + if (p + 1) + h[v] = h[p] + 1; + + for (int i = 1; i < MAXLOG; ++i) + if (par[v][i - 1] + 1) { + par[v][i] = par[par[v][i - 1]][i - 1]; + cost[v][i] = max(cost[v][i], max(cost[par[v][i-1]][i-1], cost[v][i-1])); + } + + for (auto u : graph[v]) + if (p != u.fi) + dfs(u.fi, v, u.se); +} + + +void preprocess(int v) { + memset(par, -1, sizeof par); + memset(cost, 0, sizeof cost); + dfs(v); +} + + +ll query(int p, int q) { + ll ans = 0; + + if (h[p] < h[q]) + swap(p, q); + + for (int i = MAXLOG - 1; i >= 0; --i) + if (par[p][i] + 1 && h[par[p][i]] >= h[q]) { + ans = max(ans, cost[p][i]); + p = par[p][i]; + } + + if (p == q) + return ans; + + for (int i = MAXLOG - 1; i >= 0; --i) + if (par[p][i] + 1 && par[p][i] != par[q][i]) { + ans = max(ans, max(cost[p][i], cost[q][i])); + p = par[p][i]; + q = par[q][i]; + } + + if (p == q) return ans; + else return max(ans, max(cost[p][0], cost[q][0])); +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n, a, b; + ll t; + + while (cin >> n && n) { + for (int i = 0; i <= n; ++i) + graph[i].clear(); + + for (int i = 0; i < n - 1; ++i) { + cin >> a >> b >> t; + a--, b--; + graph[a].pb(ii(b, t)); + graph[b].pb(ii(a, t)); + } + + int q; + preprocess(0); + + cin >> q; + for (int i = 0; i < q; ++i) { + cin >> a >> b; + a--, b--; + cout << query(a, b) << '\n'; + } + cout << '\n'; + } + + return 0; +} + diff --git a/contests/Cadernaveis/URI1135.cpp b/contests/Cadernaveis/URI1135.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67499654d7276debb72949481a4d7c4fb2acf970 --- /dev/null +++ b/contests/Cadernaveis/URI1135.cpp @@ -0,0 +1,103 @@ +#include <bits/stdc++.h> + +#define MAX 101010 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define MAXLOG 20 + +#define fi first +#define se second +#define sz size() +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +using namespace std; + +typedef long long ll; +typedef pair<ll,ll> ii; + +vector<ii> graph[MAX]; + +ll h[MAX]; +ll par[MAX][MAXLOG], cost[MAX][MAXLOG]; + +void dfs(int v, int p = -1, ll c = 0) { + par[v][0] = p; + cost[v][0] = c; + + if (p + 1) + h[v] = h[p] + 1; + + for (int i = 1; i < MAXLOG; ++i) + if (par[v][i - 1] + 1) { + par[v][i] = par[par[v][i - 1]][i - 1]; + cost[v][i] += cost[v][i - 1] + cost[par[v][i - 1]][i - 1]; + } + + for (auto u : graph[v]) + if (p != u.fi) + dfs(u.fi, v, u.se); +} + + +void preprocess(int v) { + memset(par, -1, sizeof par); + memset(cost, 0, sizeof cost); + dfs(v); +} + + +ll query(int p, int q) { + ll ans = 0; + + if (h[p] < h[q]) + swap(p, q); + + for (int i = MAXLOG - 1; i >= 0; --i) + if (par[p][i] + 1 && h[par[p][i]] >= h[q]) { + ans += cost[p][i]; + p = par[p][i]; + } + + if (p == q) + return ans; + + for (int i = MAXLOG - 1; i >= 0; --i) + if (par[p][i] + 1 && par[p][i] != par[q][i]) { + ans += cost[p][i] + cost[q][i]; + p = par[p][i]; + q = par[q][i]; + } + + if (p == q) return ans; + else return ans + cost[p][0] + cost[q][0]; +} + +int main() { + ll li; + int ai, n; + int q, s, t; + + while (scanf("%d", &n) && n) { + for (int i = 0; i < n + 1; ++i) + graph[i].clear(); + + for (int i = 1; i <= n - 1; ++i) { + scanf("%d %lld", &ai, &li); + graph[ai].pb(ii(i, li)); + } + + preprocess(0); + + scanf("%d", &q); + for (int i = 0; i < q; ++i) { + scanf("%d %d", &s, &t); + if (i) printf(" "); + printf("%lld", query(s, t)); + } + printf("\n"); + } + + return 0; +} diff --git a/problems/.gitkeep b/problems/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000