Skip to content
Snippets Groups Projects
Commit e9797f72 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add Cadernaveis

parent cfd9de52
Branches
No related tags found
No related merge requests found
#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;
}
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment