Author of the problem: | Andrii Kholod (Connect) |
Prepared by: | Andrii Kholod (Connect) |
Analysis: | Andrii Kholod (Connect) |
Solution
We are given a tree with vertices, and we run a DFS from vertex 1. At each vertex, the order in which we iterate over children is arbitrary, meaning we can influence the order in which vertices are visited — and hence the string that forms when we write down the visited vertices without separators.
Let this string of digits be . The function equals the length of the longest substring of consisting only of digits and . We must maximize by choosing the optimal DFS order.
1. Local characteristics of a vertex
For a vertex , consider its decimal representation s_v.
Define:
pref_v — the length of the longest prefix of s_v consisting only of digits and ;
suff_v — the length of the longest suffix of s_v consisting only of digits and ;
a vertex is fully good if pref_v = |s_v| (i.e., all digits are or ).
A substring of only cannot “jump over’’ a bad digit inside s_v. Thus, when merging traversal fragments from subtrees, the only important characteristics are these three values: whether the whole chunk can be good, or the best good prefix/suffix.
2. DP states
We root the tree at vertex . For each vertex (when processing its subtree during DFS), consider the string S_v formed from its subtree (excluding its parent).
We define:
— the length of S_v if we can choose an order of visiting children so that the entire S_v consists only of ; otherwise ;
— the maximum length of a good prefix of S_v over all possible DFS orders;
— the maximum length of a good suffix of S_v over all possible DFS orders.
We also maintain a global answer ans — the maximum achievable value of .
3. Combining results from children
Suppose a vertex has children to. Split them:
good children — those with \texttt{dp[to][0]} \ne -\infty (their whole subtree can be fully );
bad children — all others.
Let [ sum = pref_v + _to is good dp[to][0]. ] This is the maximum good sequence we can guarantee at the beginning if we visit vertex first (its good prefix) and then all fully good subtrees.
For bad children, define: [ mx = dp[to][1], mx0 = dp[to][2] ] — the best prefix and suffix among bad subtrees.
4. Transition formulas
Fully good segment: It exists iff is fully good and all children are good: [ dp[v][0] = sum, ] else dp[v][0] = -\infty.
Best prefix: If is fully good, we can take and continue into the best prefix of a bad child: [ dp[v][1] = (pref_v,; sum + mx). ] If is not fully good, the prefix cannot continue past its bad digit, so the best is just pref_v.
Best suffix: Let G = sum - pref_v — the total lengths of fully good children. Then: [ dp[v][2] =
G + suff_v, if no bad children,
[4pt] G + mx0, otherwise.
]
5. Updating the global answer
The best substring of may:
consist only of fully good children: ;
start in the suffix of , go through all fully good children, and enter a prefix of one bad child: [ suff_v + G + mx; ]
start in one bad child and end in another, with all good children in between:
We iterate over bad children and maintain two running maxima:
pref2 — best suffix seen so far;
pref1 — best prefix seen so far.
Updating: [ ans = (ans,; G + dp[to][1] + pref2), ] [ ans = (ans,; G + dp[to][2] + pref1). ]
6. Complexity
Each edge is processed times, all merges are linear in total. Thus: [ time O(n), memory O(n). ]
Solution code
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<class T>
using vec = vector<T>;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = 2e5 + 11, inf = 1000111222;
vector <int> edge[max_n];
int dp[max_n][3];
int good (int x) {
return x == 1 || x == 7;
}
int ans = 0;
void dfs (int v, int p = -1) {
dp[v][0] = -inf;
dp[v][1] = -inf;
dp[v][2] = -inf;
int x = v + 1;
int pref = 0, suff = 0;
vector <int> cur;
while (x) {
cur.pb(x % 10);
x /= 10;
}
while (pref < len(cur) && good(cur[len(cur) - 1 - pref])) {
++pref;
}
while (suff < len(cur) && good(cur[suff])) {
++suff;
}
int full = pref == len(cur), sum = pref, mx = 0, mx0 = 0;
bool bad = false;
for (int to : edge[v]) {
if (to == p) {
continue;
}
dfs(to, v);
if (dp[to][0] == -inf) {
full = 0;
bad = true;
umax(mx, dp[to][1]);
umax(mx0, dp[to][2]);
}
else {
sum += dp[to][0];
}
}
if (full) {
dp[v][0] = sum;
}
if (pref == len(cur)) {
dp[v][1] = sum + mx;
}
umax(dp[v][1], pref);
if (!bad) {
dp[v][2] = sum - pref + suff;
}
else {
dp[v][2] = sum - pref + mx0;
}
umax(ans, sum - pref);
umax(ans, suff + sum - pref + mx);
int pref2 = 0, pref1 = 0;
for (int to : edge[v]) {
if (to == p) {
continue;
}
if (dp[to][0] != -inf) {
continue;
}
umax(ans, sum - pref + dp[to][1] + pref2);
umax(ans, sum - pref + dp[to][2] + pref1);
umax(pref2, dp[to][2]);
umax(pref1, dp[to][1]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
--u, --v;
edge[u].pb(v);
edge[v].pb(u);
}
dfs(0);
for (int i = 0; i < n; i++) {
umax(ans, dp[i][0]);
umax(ans, dp[i][1]);
umax(ans, dp[i][2]);
}
cout << ans << '\n';
}