Pagini recente » Cod sursa (job #3278039) | Cod sursa (job #929657) | Cod sursa (job #2987179) | Cod sursa (job #2538301) | Cod sursa (job #2651694)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define d liste[top][i]
struct nod
{
int el, s, nivel;
long long int val;
};
int n;
vector<vector<int>> liste(16001, vector<int>());
vector<nod> noduri(16001);
queue<int> c;
int partition(vector<nod>& arr, int low, int high)
{
nod pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
if (arr[j].nivel > pivot.nivel)
{
i++; // increment index of smaller element
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
void quickSort(vector<nod>& arr, int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void bfs(vector<int> ocupat)
{
ocupat[1] = 1;
noduri[1].nivel = 0;
c.push(1);
while (!c.empty())
{
int top = c.front();
for (int i = 0; i < liste[top].size(); i++)
if (ocupat[d] == 0)
{
ocupat[d] = 1;
noduri[d].nivel = noduri[top].nivel + 1;
c.push(d);
}
c.pop();
}
}
int main()
{
fin >> n;
vector<int> ocupat(n + 1, 0);
vector<nod> sortate(n + 1);
for (int i = 1; i <= n; i++)
{
fin >> noduri[i].val;
noduri[i].s += noduri[i].val;
}
for (int i = 0; i < n - 1; i++)
{
int a, b;
fin >> a >> b;
liste[a].push_back(b);
liste[b].push_back(a);
}
bfs(ocupat);
for (int i = 1; i <= n; i++)
{
sortate[i] = noduri[i];
sortate[i].el = i;
}
quickSort(sortate, 1, n);
fout << 0;
}