CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:mbezirgan
Submission time:2021-10-17 00:36:04 +0300
Language:C++17
Status:READY
Result:25
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED15
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.93 s2, 3details
#3--3details

Code

#include <iostream>
#include <string>
#include <vector>

struct Connection;

struct Computer
{
	std::vector<Connection*> connections;
};

struct Connection
{
	uint64_t a;
	uint64_t b;
	uint64_t speed;
};

void FindAllPaths(uint64_t lowestSpeed, const Computer* computer, const Computer* distr, const Computer* computers, const Connection* connection, uint64_t* speed, bool* checked)
{
	if (checked[computer - computers])
	{
		if (distr->connections.size() == 1)
		{
			return;
		}
	}

	for (auto& c : distr->connections)
	{
		if (c != connection)
		{

			uint64_t loopLowestSpeed = lowestSpeed;
			if (loopLowestSpeed > c->speed)
				loopLowestSpeed = c->speed;

			if (&computers[c->a] != distr)
			{
				FindAllPaths(loopLowestSpeed, computer, &computers[c->a], computers, c, speed, checked);
				if (!checked[c->a])
				{
					*speed += loopLowestSpeed;
				}
			}
			else
			{
				FindAllPaths(loopLowestSpeed, computer, &computers[c->b], computers, c, speed, checked);
				if (!checked[c->b])
				{
					*speed += loopLowestSpeed;
				}
			}
		}
	}

	if (computer == distr)
	{
		checked[computer - computers] = true;
	}
}

// Generates all paths lengths (can be easily made to also save for what computers they are)
inline void FindConnetionsSpeed(uint64_t n, const Computer* computers, uint64_t* speed)
{
	bool* checked = new bool[n]();

	for (uint64_t i = 0; i < n - 1; i++)
	{
		FindAllPaths(UINT32_MAX, &computers[i], &computers[i], computers, nullptr, speed, checked);
	}

	delete[] checked;
}

int main()
{
	uint64_t n;
	std::cin >> n;

	Computer* computers = new Computer[n]();
	Connection* connections = new Connection[n - 1];

	for (uint64_t i = 0; i < n - 1; i++)
	{
		auto& c = connections[i];
		uint64_t a, b;
		std::cin >> a;
		std::cin >> b;

		c.a = a - 1;
		c.b = b - 1;

		computers[c.a].connections.push_back(&c);
		computers[c.b].connections.push_back(&c);

		std::cin >> c.speed;
	}

	uint64_t speed = 0;
	FindConnetionsSpeed(n, computers, &speed);

	std::cout << speed;

	delete[] connections;
	delete[] computers;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
1 2 74
1 3 100
2 4 50
3 5 40
...

correct output
88687

user output
88687

Test 2

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1103702320243776

user output
1103702320243776

Test 3

Group: 3

Verdict:

input
200000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1080549209850010931

user output
(empty)