82042024-01-12 19:00:20CWMSzínes facpp17Wrong answer 0/50175ms30864 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <climits>

using namespace std;

//#define int long long

signed main()
{
	int n;
	cin >> n;
	vector<vector<int>> g(n);
	for (size_t i = 0; i < n-1; i++)
	{
		int a;
		cin >> a;
		a--;
		g[a].push_back(i);
		g[i].push_back(a);
	}
	vector<int> dist(n);
	queue<pair<int,int>> BFS;
	BFS.push({ 0,0 });
	while (!BFS.empty()) {
		pair<int, int> cur = BFS.front();
		BFS.pop();
		for (size_t i = 0; i < g[cur.first].size(); i++)
		{
			int val = g[cur.first][i];
			if (dist[val] == 0) {
				dist[val] = cur.second;
				BFS.push({ val,cur.second + 1 });
			}
		}
	}
	int minRes = INT_MAX;
	for (size_t i = 1; i < dist.size(); i++) {
		if (g[i].size() == 1) {
			minRes = min(dist[i], minRes);
		}
	}
	cout << minRes+1 << "\n";
	for (size_t i = 0; i < dist.size(); i++)
	{
		cout << min(minRes + 1, dist[i] + 1) << " ";
	}
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/03ms1812 KiB
2Wrong answer0/08ms2928 KiB
3Wrong answer0/13ms2232 KiB
4Wrong answer0/43ms2280 KiB
5Wrong answer0/5123ms27052 KiB
6Wrong answer0/2175ms28648 KiB
7Wrong answer0/3163ms28856 KiB
8Wrong answer0/2150ms29096 KiB
9Wrong answer0/2152ms29440 KiB
10Wrong answer0/2150ms29392 KiB
11Wrong answer0/2160ms29392 KiB
12Wrong answer0/2172ms29736 KiB
13Wrong answer0/2150ms29924 KiB
14Wrong answer0/2150ms29840 KiB
15Wrong answer0/2171ms29840 KiB
16Wrong answer0/2164ms29788 KiB
17Wrong answer0/2174ms30040 KiB
18Wrong answer0/2152ms30304 KiB
19Wrong answer0/2159ms30568 KiB
20Wrong answer0/2163ms30568 KiB
21Wrong answer0/2159ms30320 KiB
22Wrong answer0/2174ms30284 KiB
23Wrong answer0/2173ms30524 KiB
24Wrong answer0/3172ms30864 KiB