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

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:104:12: warning: unused variable 'paths' [-Wunused-variable]
  uint32_t* paths = new uint32_t[count];
            ^~~~~

Code

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

struct Connection;

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

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

uint32_t SumFactorial(uint32_t n)
{
	uint32_t a = 0;

	for (uint32_t i = 1; i < n; i++)
		a += i;

	return a;
}

void FindAllPaths(uint32_t lowestSpeed, const Computer* computer, const Computer* distr, const Computer* computers, const Connection* connection, uint64_t* speed, bool* checked)
{
	for (auto& c : distr->connections)
	{
		if (c != connection)
		{
			uint32_t loopLowestSpeed = lowestSpeed;
			if (loopLowestSpeed > c->speed)
				loopLowestSpeed = c->speed;

			if (&computers[c->a - 1] != distr)
			{
				FindAllPaths(loopLowestSpeed, computer, &computers[c->a - 1], computers, c, speed, checked);
				if (!checked[c->a - 1])
				{
					*speed += loopLowestSpeed;
				}
			}
			else
			{
				FindAllPaths(loopLowestSpeed, computer, &computers[c->b - 1], computers, c, speed, checked);
				if (!checked[c->b - 1])
				{
					*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)
void FindConnetionsSpeed(uint32_t n, const Computer* computers, uint64_t* speed)
{
	bool* checked = new bool[n]();

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

	delete[] checked;
}

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

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

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

		c.a = a;
		c.b = b;

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

		std::cin >> c.speed;
	}

	uint64_t speed = 0;
	uint32_t count = SumFactorial(n);

	uint32_t* paths = new uint32_t[count];
	FindConnetionsSpeed(n, computers, &speed);

	std::cout << speed;

	delete[] connections;
}

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)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc