Cod sursa(job #3300711)

Utilizator rapidu36Victor Manz rapidu36 Data 18 iunie 2025 17:27:08
Problema Atac Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.74 kb
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

const int INF = 1e9;
const int LOG = 15;

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)
{
    t_in[x] = ++timp;
    for (auto p: a[x])
    {
        int vf = p.first;
        int c = p.second;
        if (nivel[vf] == -1)
        {
            s[0][vf] = x;
            cost[0][vf] = c;
            nivel[vf] = 1 + nivel[x];
            dfs(vf);
        }
    }
    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 lca(int x, int y)
{
    if (e_stramos(x, y))
    {
        return 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];
        }
    }
    return s[0][x];
}

int cost_lca(int x, int y)
{
    int s_com = lca(x, y);
    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);
    log_2_n = LOG;
    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, -1);
    timp = 0;
    nivel[1] = 0;
    dfs(1);
    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";
        }
        x = (a * x + b * y) % n + 1;
        y = (c * y + d * cost_com) % n + 1;
    }
    in.close();
    out.close();
    return 0;
}