95062024-02-22 12:41:00ananászÚtadócpp17Wrong answer 25/50192ms16980 KiB
// utado.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <algorithm>

using namespace std;

struct ut {
	long long elotte;
	long long cel;
};
struct varos {
	long long elszam;
	vector <ut>szomszedok;
	bool volt;
};
struct eredmeny {
	long long atmegy;
	long long kezd;
	long long veg;
	long long ar;
};
bool rendez(eredmeny e1, eredmeny e2) {
	return e1.atmegy > e2.atmegy;
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);

	long long N;
	cin >> N;
	vector<varos>varosok(N + 1);
	ut x, y;
	x.elotte = 0;
	y.elotte = 0;
	for (int i = 0; i < N - 1; i++)
	{
		cin >> x.cel >> y.cel;
		varosok[x.cel].szomszedok.push_back(y);
		varosok[y.cel].szomszedok.push_back(x);
		varosok[x.cel].elszam++;
		varosok[y.cel].elszam++;
		if (varosok[y.cel].elszam > 1)
		{
			varosok[y.cel].volt = false;
		}
		else
		{
			varosok[y.cel].volt = true;
		}
		if (varosok[x.cel].elszam > 1)
		{
			varosok[x.cel].volt = false;
		}
		else
		{
			varosok[x.cel].volt = true;
		}
	}
	priority_queue<int>sulyok;
	long long suly;
	for (int i = 0; i < N - 1; i++)
	{
		cin >> suly;
		sulyok.push(suly);
	}
	vector<eredmeny>utak(N - 1);
	int e = 0;
	int s = 0;
	while (e<N-1)
	{
		for (int i = 0; i < N; i++)
		{
			if (!varosok[i].volt)
			{
				for (int j = 0; j < varosok[i].szomszedok.size(); j++)
				{
					int akt = varosok[i].szomszedok[j].cel;
					if (varosok[akt].elszam == 1)
					{
						for (int k = 0; k < varosok[akt].szomszedok.size(); k++)
						{
							if (varosok[akt].szomszedok[k].cel != i)
							{
								varosok[i].szomszedok[j].elotte += varosok[akt].szomszedok[k].elotte;
							}
						}
						varosok[i].szomszedok[j].elotte++;
						varosok[akt].elszam = 0;
						varosok[i].elszam--;
						if (varosok[i].elszam == 1)
						{
							varosok[i].volt = true;
						}
						utak[e].kezd = i;
						utak[e].veg = akt;
						utak[e].atmegy = varosok[i].szomszedok[j].elotte * (N - varosok[i].szomszedok[j].elotte) * 2;
						e++;
					}
				}
			}
			
		}


	}
	sort(utak.begin(), utak.end(), rendez);
	int sum = 0;
	for (int i = 0; i < N - 1; i++)
	{
		utak[i].ar = sulyok.top();
		sum += sulyok.top() * utak[i].atmegy;
		sulyok.pop();
	}
	cout << sum << endl;
	for (int i = 0; i < N - 1; i++)
	{
		cout << utak[i].kezd << " " << utak[i].veg << " " << utak[i].ar << endl;
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base25/50
1Accepted0/03ms1700 KiB
2Wrong answer0/076ms7288 KiB
3Partially correct1/23ms2144 KiB
4Partially correct1/23ms2280 KiB
5Partially correct1/23ms2492 KiB
6Partially correct1/23ms2700 KiB
7Partially correct1/23ms3044 KiB
8Partially correct4/8182ms15180 KiB
9Partially correct1/24ms3200 KiB
10Partially correct1/24ms3276 KiB
11Partially correct1/24ms3400 KiB
12Partially correct1/24ms3468 KiB
13Partially correct1/24ms3556 KiB
14Partially correct1/2107ms16980 KiB
15Partially correct1/2111ms16772 KiB
16Partially correct1/2111ms16748 KiB
17Partially correct1/2108ms16828 KiB
18Partially correct1/2119ms16732 KiB
19Partially correct1/2146ms16808 KiB
20Partially correct1/2180ms16640 KiB
21Partially correct1/2192ms16520 KiB
22Partially correct1/2122ms16840 KiB
23Partially correct1/2159ms16860 KiB
24Partially correct1/2159ms16784 KiB