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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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.