# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
424546 | KienTranluvChaeng | Split the sequence (APIO14_sequence) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int O = 1e4 + 1;
int n, f[O][4];
vector <vector <int>> g;
void dfs(int u, int par = 0){
f[u] = 1;
vector <int> d(n + 1);
for (int v : g[u]){
if (v != par){
dfs(v, u);
d[f[v]] = 1;
}
}
int i;
for (i = 1; i <= n; ++ i)
if (!d[i]) break;
f[u] = i;
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n; g.resize(n + 1);
for (int i = 1; i < n; ++ i){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
int ans = 0;
for (int i = 1; i <= n; ++ i) ans += f[i];
cout << ans << "\n";
for (int i = 1; i <= n; ++ i) cout << f[i] << "\n";
}