CSES - NOI 2019 - Results
Submission details
Task:Distance Code
Sender:Niklas Sandén
Submission time:2019-03-06 14:39:46 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.03 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#40.02 s1, 2, 3details
#5ACCEPTED0.02 s1, 2, 3details
#60.01 s1, 2, 3details
#70.01 s1, 2, 3details
#80.02 s1, 2, 3details
#9ACCEPTED0.02 s1, 2, 3details
#100.02 s1, 2, 3details
#110.02 s1, 2, 3details
#120.01 s2, 3details
#13ACCEPTED0.02 s2, 3details
#140.02 s2, 3details
#150.01 s2, 3details
#160.07 s3details
#17ACCEPTED0.08 s3details
#180.07 s3details
#190.07 s3details
#200.03 s1, 2, 3details

Code

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define allP(x) x->begin(), x->end()
#define sz(x) (int)(x).size()

// define int ll

typedef long long ll;

const int inf = 0x3f3f3f3f;
const long long mod = 1000000007;

void Encoder()
{
	int n; cin >> n;

	vector<bool> isLeaf(n, true);

	vector<vector<int>> paths(n);
	vector<int> pathCount(n, 0);
	for (int i = 0; i < n - 1; i++)
	{
		int a, b; cin >> a >> b;
		a--; b--;

		paths[a].push_back(b);
		paths[b].push_back(a);

		pathCount[a]++;
		pathCount[b]++;

		if (paths[a].size() > 1)
		{
			isLeaf[a] = false;
		}
		if (paths[b].size() > 1)
		{
			isLeaf[b] = false;
		}
	}

	vector<int> leaves;

	for (int i = 0; i < n; i++)
	{
		if (isLeaf[i])
		{
			leaves.push_back(i);
		}
	}

	while (!leaves.empty())
	{
		int index = -1;
		for (auto tempIndex : paths[leaves.back()])
		{
			if (pathCount[tempIndex] > 0)
			{
				index = tempIndex;
				break;
			}
		}
		// int tempIndex = paths[leaves.back()].front();

		if (index == -1)
		{
			cout << leaves.back() + 1;
			return;
		}

		cout << leaves.back() + 1 << ' ';

		pathCount[leaves.back()]--;

		leaves.pop_back();

		pathCount[index]--;

		if (pathCount[index] == 1)
		{
			leaves.push_back(index);
		}
	}
}

void Decoder()
{
	int n; cin >> n;

	vector<int> distances(n - 1);

	for (int i = n - 2; i >= 0; i--)
	{
		cin >> distances[i];
	}

	vector<int> nodeDistances(n);
	vector<int> nodeConnectedTo(n);
	nodeConnectedTo[0] = 0;
	nodeDistances[0] = 0;

	unordered_map<int, vector<int>> distancesFromRoot;
	distancesFromRoot[0].push_back(0);

	int nextIndex = 1;
	int currentNode = 0;
	int parentNode = 0;
	for (int distance : distances)
	{
		parentNode = currentNode;
		if (distance > 1)
		{
			distance--;

			int currentDistance = nodeDistances[currentNode];
			int currentConnection = nodeConnectedTo[currentNode];

			if (currentDistance - distance > 0)
			{
				vector<int>& tempDistances = distancesFromRoot[currentDistance - distance];

				for (int index : tempDistances)
				{
					if (nodeConnectedTo[index] != currentConnection)
					{
						parentNode = index;
						break;
					}
				}
			}
			else
			{
				vector<int>& tempDistances = distancesFromRoot[currentDistance - distance];

				for (int index : tempDistances)
				{
					if (nodeConnectedTo[index] == currentConnection)
					{
						parentNode = index;
						break;
					}
				}
			}
		}

		nodeDistances[nextIndex] = nodeDistances[parentNode] + 1;

		distancesFromRoot[nodeDistances[nextIndex]].push_back(nextIndex);

		if (parentNode == 0)
		{
			nodeConnectedTo[nextIndex] = nextIndex;
		}
		else
		{
			nodeConnectedTo[nextIndex] = nodeConnectedTo[parentNode];
		}

		cout << currentNode + 1 << ' ' << nextIndex + 1 << '\n';

		currentNode = nextIndex;
		nextIndex++;
	}
}

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	// cin.exceptions(cin.failbit);
	// cout << setprecision(10);
	// cout << fixed;

	int t; cin >> t;

	if (t == 1)
	{
		Encoder();
	}
	else
	{
		Decoder();
	}

	return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
2
2 1

correct output
(empty)

user output
1 2

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
3
3 1
2 1

correct output
(empty)

user output
1 2
2 3

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
4
3 2
2 1
4 1

correct output
(empty)

user output
1 2
2 3
3 4

Test 4

Group: 1, 2, 3

Verdict:

input
1
4
2 3
3 4
1 3

correct output
(empty)

user output
1 2
2 3
3 4

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
5
3 5
4 1
1 3
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5

Test 6

Group: 1, 2, 3

Verdict:

input
1
5
3 2
3 4
5 1
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5

Test 7

Group: 1, 2, 3

Verdict:

input
1
5
4 3
1 4
4 2
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5

Test 8

Group: 1, 2, 3

Verdict:

input
1
10
9 3
8 9
2 9
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 9

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
10
9 2
5 8
7 1
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 10

Group: 1, 2, 3

Verdict:

input
1
10
10 4
9 1
4 7
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 11

Group: 1, 2, 3

Verdict:

input
1
10
2 6
4 3
3 5
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 12

Group: 2, 3

Verdict:

input
1
500
10 6
6 255
6 428
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 13

Group: 2, 3

Verdict: ACCEPTED

input
1
500
152 466
451 313
158 479
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 14

Group: 2, 3

Verdict:

input
1
500
109 440
330 190
443 161
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 15

Group: 2, 3

Verdict:

input
1
500
144 373
257 233
341 318
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 16

Group: 3

Verdict:

input
1
100000
54983 75172
93807 75172
44082 75172
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 17

Group: 3

Verdict: ACCEPTED

input
1
100000
88863 19059
86423 76688
98536 95984
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 18

Group: 3

Verdict:

input
1
100000
59979 6389
19097 24999
27846 82330
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 19

Group: 3

Verdict:

input
1
100000
58761 66001
25102 51081
98625 67861
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6
...

Test 20

Group: 1, 2, 3

Verdict:

input
1
6
2 1
3 2
4 2
...

correct output
(empty)

user output
1 2
2 3
3 4
4 5
5 6