import java.util.ArrayList;
import java.util.List;
public static void main
(String[] args
) { new Thread(null,
new Main
(),
"",
256 * (1L
<< 20)).
start(); }
@Override
public void run() {
int n = 500000;
List
<Integer
>[] g
= new List[n
]; for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
int p = i - 1;
g[i].add(p);
g[p].add(i);
}
for (int i = 0; i < 100000; i++) {
dfs(0, -1, g, 0, 0); // JIT please
}
int result
= dfs
(0,
-1, g,
0,
Integer.
MAX_VALUE); System.
out.
println("time = " + (t2
- t1
) / 1.0e9
+ " sec, result = " + result
); }
private int dfs(int x, int p, List<Integer>[] g, int curDepth, int maxDepth) {
if (curDepth > maxDepth) {
return 0;
}
int result = 1;
for (int y : g[x]) {
if (y == p) {
continue;
}
result += dfs(y, x, g, curDepth + 1, maxDepth);
}
return result;
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCnB1YmxpYyBjbGFzcyBNYWluIGltcGxlbWVudHMgUnVubmFibGUgewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIG5ldyBUaHJlYWQobnVsbCwgbmV3IE1haW4oKSwgIiIsIDI1NiAqICgxTCA8PCAyMCkpLnN0YXJ0KCk7CiAgICB9CgogICAgQE92ZXJyaWRlCiAgICBwdWJsaWMgdm9pZCBydW4oKSB7CiAgICAgICAgaW50IG4gPSA1MDAwMDA7CiAgICAgICAgTGlzdDxJbnRlZ2VyPltdIGcgPSBuZXcgTGlzdFtuXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBnW2ldID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGludCBwID0gaSAtIDE7CiAgICAgICAgICAgIGdbaV0uYWRkKHApOwogICAgICAgICAgICBnW3BdLmFkZChpKTsKICAgICAgICB9CgogICAgICAgIGxvbmcgdDEgPSBTeXN0ZW0ubmFub1RpbWUoKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDEwMDAwMDsgaSsrKSB7CiAgICAgICAgICAgIGRmcygwLCAtMSwgZywgMCwgMCk7IC8vIEpJVCBwbGVhc2UKICAgICAgICB9CiAgICAgICAgaW50IHJlc3VsdCA9IGRmcygwLCAtMSwgZywgMCwgSW50ZWdlci5NQVhfVkFMVUUpOwogICAgICAgIGxvbmcgdDIgPSBTeXN0ZW0ubmFub1RpbWUoKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oInRpbWUgPSAiICsgKHQyIC0gdDEpIC8gMS4wZTkgKyAiIHNlYywgcmVzdWx0ID0gIiArIHJlc3VsdCk7CiAgICB9CgogICAgcHJpdmF0ZSBpbnQgZGZzKGludCB4LCBpbnQgcCwgTGlzdDxJbnRlZ2VyPltdIGcsIGludCBjdXJEZXB0aCwgaW50IG1heERlcHRoKSB7CiAgICAgICAgaWYgKGN1ckRlcHRoID4gbWF4RGVwdGgpIHsKICAgICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfQogICAgICAgIGludCByZXN1bHQgPSAxOwogICAgICAgIGZvciAoaW50IHkgOiBnW3hdKSB7CiAgICAgICAgICAgIGlmICh5ID09IHApIHsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJlc3VsdCArPSBkZnMoeSwgeCwgZywgY3VyRGVwdGggKyAxLCBtYXhEZXB0aCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9Cn0=