QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865338#5124. TreePoole_teaTL 3ms34844kbC++141.5kb2025-01-21 16:50:412025-01-21 16:50:41

Judging History

你现在查看的是最新测评结果

  • [2025-01-21 16:50:41]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:34844kb
  • [2025-01-21 16:50:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std ;
const int MAXN = 1e6 + 10 ;
vector<int> e[MAXN] ;
int len[MAXN], son[MAXN], siz[MAXN], tot ;
inline bool cmp (int x, int y) {
    return x > y ;
}
inline void dfs (int x) {
    for (auto v : e[x]) {
       dfs(v) ;
       if (siz[son[x]] < siz[v]) son[x] = v ;
    }
    siz[x] = siz[son[x]] + 1 ;
}
inline int read() {
	register int x = 0, f = 0;
	register char ch = getchar();
	while (ch < '0' || ch > '9')
		f |= ch == '-', ch = getchar();
	while (ch >= '0' && ch <= '9')
		x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
inline void redfs (int x, int k) {
    if (!son[x]) len[++tot] = k ;
    else redfs(son[x], k + 1) ;
    for (auto v : e[x]) {
        if (v != son[x]) redfs (v, 1) ;
    }
}
int main () {
    int t ;
    t = read() ;
    while (t--) {
        int n ;
        n = read() ;
        memset(son, 0, sizeof(son)) ;
        for (int i = 1 ; i <= n ; i++) e[i].clear() ;
        int x ;
        for (int i = 2 ; i <= n ; i++) {
            x = read() ;
            e[x].push_back(i) ;
        }
        tot = 0 ;
        dfs(1) ;
        // for (int i = 1 ; i <= n ; i++) cout << son[i] << '\n' ;
        redfs(1, 1) ;
        sort(len + 1, len + tot + 1, cmp) ;
        // for (int i = 1 ; i <= tot ; i++) cout << len[i] << '\n' ;
        int ans = len[1] ;
        len[tot + 1] = 0 ;
        for (int i = 1 ; i <= tot ; i++) ans = min(i + len[i + 1], ans) ;
        cout << ans << '\n' ;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 34844kb

input:

2
7
1 1 2 2 2 3
5
1 2 3 4

output:

3
1

result:

ok 2 number(s): "3 1"

Test #2:

score: -100
Time Limit Exceeded

input:

46234
1

2
1
3
1 1
4
1 1 1
5
1 1 1 1
6
1 1 1 1 1
7
1 1 1 1 1 1
8
1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 2
9
1 1 1 1 1 1 1 3
9
1 1 1 1 1 1 1 4
9
1 1 1 1 1 1 1 5
9
1 1 1 1 1 1 1 6
9
1 1 1 1 1 1 1 7
9
1 1 1 1 1 1 1 8
8
1 1 1 1 1 1 2
9
1 1 1 1 1 1 2 1
9
1 1 1 1 1 1 2 2
9
1 1 1 1 1 1 2 3
9
1 1 1...

output:

1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
2
2
2
3
2
3
3
3
3
2
2
2
3
3
2
3
3
3
2
2
2
3
3
3
2
3
3
2
2
2
3
3
3
3
2
3
2
2
2
3
3
3
3
3
2
2
2
2
2
2
3
3
3
3
2
3
2
2
2
3
3
3
3
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
2
2
2
3
3
3
3
2
2
2
2
2
3
2
3
3
3
2
3
3
3
3
3
3
3
...

result: