Pagini recente » Cod sursa (job #2636754) | Cod sursa (job #2375408) | Cod sursa (job #3257137) | Cod sursa (job #2602748) | Cod sursa (job #3300700)
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
const int INF = 2e9;
vector <vector <pair <int, int>>> a;
vector <vector <int>> s;
vector <vector <int>> cost;
vector <int> t_in;
vector <int> t_out;
vector <int> nivel;
int timp;
int log_2_n;
void dfs(int x, int t)
{
t_in[x] = ++timp;
if (t == 0)
{
nivel[x] = 0;
}
else
{
nivel[x] = 1 + nivel[t];
}
for (auto p: a[x])
{
int vf = p.first;
int c = p.second;
if (vf != t)
{
s[0][vf] = x;
cost[0][vf] = c;
dfs(vf, x);
}
}
t_out[x] = ++timp;
}
bool e_stramos(int x, int y)
{
return (t_in[x] <= t_in[y] && t_out[y] <= t_out[x]);
}
int cost_lant(int x, int lung)
{
int cost_min = INF, i = 0;
while (lung != 0)
{
if (lung % 2 != 0)
{
cost_min = min(cost_min, cost[i][x]);
x = s[i][x];
}
lung /= 2;
i++;
}
return cost_min;
}
int cost_lca(int x, int y)
{
int s_com;
if (e_stramos(x, y))
{
s_com = x;
}
else
{
int x_ = x;
for (int i = log_2_n; i >= 0; i--)
{
if (s[i][x] != 0 && !e_stramos(s[i][x], y))
{
x = s[i][x];
}
}
s_com = s[0][x];
x = x_;
}
int c_com = INF;
c_com = min(c_com, cost_lant(x, nivel[x] - nivel[s_com]));
c_com = min(c_com, cost_lant(y, nivel[y] - nivel[s_com]));
return c_com;
}
int main()
{
ifstream in("atac.in");
ofstream out("atac.out");
int n, m, p;
in >> n >> m >> p;
log_2_n = (int)log2(n);
s.resize(log_2_n + 1);
cost.resize(log_2_n + 1);
for (int i = 0; i <= log_2_n; i++)
{
s[i].resize(n + 1, 0);
cost[i].resize(n + 1, INF);
}
a.resize(n + 1);
for (int i = 2; i <= n; i++)
{
int j, c;
in >> j >> c;
a[i].push_back({j, c});
a[j].push_back({i, c});
}
t_in.resize(n + 1);
t_out.resize(n + 1);
nivel.resize(n + 1);
timp = 0;
dfs(1, 0);
for (int i = 1; i <= log_2_n; i++)
{
for (int j = 1; j <= n; j++)
{
s[i][j] = s[i-1][s[i-1][j]];
cost[i][j] = min(cost[i-1][j], cost[i-1][s[i-1][j]]);
}
}
int x, y, a, b, c, d;
in >> x >> y >> a >> b >> c >> d;
for (int i = 0; i < m; i++)
{
int cost_com;
cost_com = cost_lca(x, y);
if (i >= m - p)
{
out << cost_com << "\n";
}
int x_ = (a * x + b * y) % n + 1;
int y_ = (c * y + d * cost_com) % n + 1;
x = x_;
y = y_;
}
in.close();
out.close();
return 0;
}