602021-01-09 16:08:48mraronSzínes facpp11Elfogadva 50/50104ms45072 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
//#pragma GCC target("avx2")
 
#define deb(x) cout << #x << " = " << x << "\n"
#define all(x) (x).begin(), (x).end()
#define len(x) (int) x.size()
#define lsb(x) (x) & -(x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define v(x) (int)(x - 'a')
 
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
 
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ld, ld> pld;
typedef pair <ll, ll> pll;
 
template <class T1, class T2 = less <T1>> using pb_heap = __gnu_pbds::priority_queue <T1, T2, binary_heap_tag>;
template <class T1, class T2 = null_type, class T3 = less <T1>> using pb_map = tree <T1, T2, T3, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2 = null_type, class T3 = hash <T1>> using pb_umap = gp_hash_table <T1, T2, T3>;
template <class T1, class T2, class T3 = hash <T1>> using umap = unordered_map <T1, T2, T3>;
template <class T> using uset = unordered_set <T>;
template <class T> using vec = vector <T>;
 
const ll infll = numeric_limits <ll>::max() >> 1;
const int inf = numeric_limits <int>::max() >> 1;
const int N = 2e5 + 1;
int n;

struct Graph {
     int dep[N];
     vec <int> adj[N];
} graph;

void input() 
{
     cin >> n;
     for (int i = 2; i <= n; ++i) {
          int p; cin >> p;
          graph.adj[p].pb(i);
     }
}

int dfs(int u, int h = 1)
{
     graph.dep[u] = h;
     int res = inf;
     for (int v: graph.adj[u]) {
          res = min(res, dfs(v, h + 1));
     }
     if (graph.adj[u].empty()) {
          res = min(res, h);
     }
     return res;
}

void solve()
{
     int ans = dfs(1);
     cout << ans << "\n";
     for (int i = 1; i <= n; ++i) {
          cout << min(ans, graph.dep[i]) << " ";
     }
     cout << "\n";
}

int main() 
{
     ios_base::sync_with_stdio(false);
     cin.tie(0), cout.tie(0);
     input();
     solve();
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/07ms11196 KiB
2Elfogadva0/08ms12052 KiB
3Elfogadva1/14ms11452 KiB
4Elfogadva4/46ms11712 KiB
5Elfogadva5/568ms45072 KiB
6Elfogadva2/279ms21784 KiB
7Elfogadva3/379ms21808 KiB
8Elfogadva2/276ms21256 KiB
9Elfogadva2/271ms20452 KiB
10Elfogadva2/274ms21144 KiB
11Elfogadva2/279ms23528 KiB
12Elfogadva2/283ms24896 KiB
13Elfogadva2/286ms25484 KiB
14Elfogadva2/287ms25980 KiB
15Elfogadva2/289ms26484 KiB
16Elfogadva2/289ms26544 KiB
17Elfogadva2/290ms26864 KiB
18Elfogadva2/2101ms27136 KiB
19Elfogadva2/292ms27208 KiB
20Elfogadva2/2104ms27548 KiB
21Elfogadva2/292ms27904 KiB
22Elfogadva2/293ms28364 KiB
23Elfogadva2/297ms32124 KiB
24Elfogadva3/3101ms36888 KiB