Cod sursa(job #2614960)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 12 mai 2020 22:35:01
Problema Heavy Path Decomposition Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("heavypath.in");
ofstream g("heavypath.out");

const int NMAX = 1e5 + 5, ROOT = 1;

int N, Q, A[NMAX];

vector < int > G[NMAX], Sons[NMAX];
bool Sel[NMAX];

int W[NMAX], Max_Son[NMAX], Level[NMAX], father[NMAX];

int M = 1;
int Chain[NMAX], Pos[NMAX], First[NMAX], Size[NMAX];

static inline void Read ()
{
    f.tie(nullptr);

    f >> N >> Q;

    for(int i = 1; i <= N; ++i)
        f >> A[i];

    for(int i = 1; i < N; ++i)
    {
        int X = 0, Y = 0;
        f >> X >> Y;

        G[X].push_back(Y);
        G[Y].push_back(X);
    }

    return;
}

static inline void Go (int Node, int Lvl)
{
    Sel[Node] = 1;
    Level[Node] = Lvl;

    W[Node] = 1;
    Max_Son[Node] = -1;

    for(auto it : G[Node])
        if(!Sel[it])
        {
            Sons[Node].push_back(it);
            father[it] = Node;

            Go(it, Lvl + 1);

            W[Node] += W[it];

            if(Max_Son[Node] == -1 || (Max_Son[Node] > 0 && W[it] > W[Max_Son[Node]]))
                Max_Son[Node] = it;
        }

    return;
}

static inline void DFS (int Node)
{
    Chain[Node] = M;
    Pos[Node] = (++Size[M]);

    if(Pos[Node] == 1)
        First[M] = Node;

    if(Max_Son[Node] == -1)
        return;

    DFS(Max_Son[Node]);

    for(auto it : Sons[Node])
        if(it != Max_Son[Node])
        {
            ++M;

            DFS(it);
        }

    return;
}

static inline void Precalculation ()
{
    Go(ROOT, 1);

    /// DFS(ROOT);

    return;
}

int main()
{
    Read();

    Precalculation();

    return 0;
}