QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#766283#9751. 覆盖一棵树ZHYa#WA 26ms3616kbC++201.3kb2024-11-20 16:50:302024-11-20 16:50:34

Judging History

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

  • [2024-11-20 16:50:34]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:3616kb
  • [2024-11-20 16:50:30]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<vector<int>> con;
vector<int> leafs;
vector<int> fa;
void dfs(int u, int f)
{
  bool flag = 0;
  fa[u] = f;
  for (auto v : con[u])
  {
    if (v == f)
      continue;
    flag = 1;
    dfs(v, u);
  }
  if (!flag)
    leafs.push_back(u);
}
bool judge(int mid)
{
  vector<bool> vis(n + 1, 0);
  for (auto lf : leafs)
  {
    int nowpos = lf;
    int cnt = mid;
    while (cnt--)
    {
      vis[nowpos] = 1;
      nowpos = fa[nowpos];
      if (vis[nowpos])
        break;
    }
  }
  for (int i = 2; i <= n; i++)
  {
    if (vis[i])
      continue;
    else
      return 0;
  }
  return 1;
}
void solve()
{
  cin >> n;
  con.assign(n + 1, vector<int>());
  fa.assign(n + 1, 0);
  leafs.clear();
  for (int i = 2; i <= n; i++)
  {
    int f;
    cin >> f;
    fa[i] = f;
    con[f].push_back(i);
    con[i].push_back(f);
  }
  dfs(1, 1);
  int l = 0, r = n;
  while (l + 1 < r)
  {
    int mid = (l + r) / 2;
    if (judge(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
}
signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  cin >> T;
  while (T--)
  {
    solve();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

input:

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

output:

3
7

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 26ms
memory: 3560kb

input:

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

output:

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

result:

wrong answer 1st lines differ - expected: '4', found: '6'