Pagini recente » Cod sursa (job #611804) | Cod sursa (job #2090659) | Cod sursa (job #2985412) | Cod sursa (job #2576097) | Cod sursa (job #2615048)
#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], X, Y;
vector < int > G[NMAX], Sons[NMAX];
int Level[NMAX], father[NMAX], W[NMAX];
int M = 1;
int Chain[NMAX], Pos[NMAX], Size[NMAX];
int *AINT[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)
{
f >> X >> Y;
G[X].push_back(Y);
G[Y].push_back(X);
}
return;
}
static inline void Go (int Node)
{
W[Node] = 1;
for(auto it : G[Node])
if(!Level[it])
{
Sons[Node].push_back(it);
father[it] = Node;
Level[it] = Level[Node] + 1;
Go(it);
W[Node] += W[it];
}
return;
}
static inline void DFS (int Node)
{
Chain[Node] = M;
Pos[Node] = (++Size[M]);
int Max_Son = 0, Max = 0;
for(auto it : Sons[Node])
if(W[it] > Max)
{
Max = W[it];
Max_Son = it;
}
if(Max_Son)
DFS(Max_Son);
for(auto it : Sons[Node])
if(it != Max_Son)
{
++M;
DFS(it);
}
return;
}
static inline void Initialize ()
{
for(int i = 1; i <= M; ++i)
{
AINT[i] = new int [(Size[i] << 2) + 1];
}
return;
}
static inline void Precalculation ()
{
Level[ROOT] = 1;
Go(ROOT);
DFS(ROOT);
Initialize();
return;
}
int main()
{
Read();
Precalculation();
return 0;
}