Hi,
I'm having a hard time trying to figure out an efficient solution to problem J — Jimi Hendrix from Petrozavodsk Winter Training Camp 2016.
Do you guys have any tips ?
Thanks
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hi,
I'm having a hard time trying to figure out an efficient solution to problem J — Jimi Hendrix from Petrozavodsk Winter Training Camp 2016.
Do you guys have any tips ?
Thanks
Name |
---|
It's a DP on tree problem.
To solve the problem, you need to calculate maximal number of symbols from prefix of string S you have visited on some path started somewhere in subtree of vertex V and ended in V and similarly for suffix (assume they are dp[v][0] and dp[v][1]).
Than, when you calculated this values for all children of some vertex V, you should check is there exists the path passing through the vertex V that is the answer, you need to check is there exists such two children ch1 and ch2 of vertex V, that dp[ch1][0] + f(edge(ch1, v)) + dp[ch2][1] + f(edge(v, ch2)) >= m, where f is 1 or 0, if this edge contains needed symbol of S or not. (Such two children can be found using prefix maximum of dp values)
And if you maintain vertices, where path for dp[v][0] and dp[v][1] starts, all the problem can be done using one simple DFS.
Thanks for you answer, I tried something very similar, but somehow I tried to decompose the tree with centroids and go up-tree in O(log(n)) but it's pretty easy to see a counter to this idea.