QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#95863#6307. Chase Game 2ksunhokimCompile Error//C++201.6kb2023-04-12 09:23:202023-04-12 09:23:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-12 09:23:23]
  • 评测
  • [2023-04-12 09:23:20]
  • 提交

answer

#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;

void solve() {
  int n;
  cin >> n;
  vector adj(n, vector<int>());

  for (int i=0;i<n-1;i++){
    int u,v;
    cin >> u >> v;
    --u,--v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  map<int,int> leafs;
  vector<int> levels(n);

  auto dfs = [&](auto self, int u, int p, int level) -> void {
    int cnt = 0;
    levels[u] = level;
    for (int v : adj[u]) {
      if (v == p) continue;
      self(self, v, u, level + 1);
      cnt++;
    }
    if (cnt == 0) {
      leafs[p]++;
    }
  };
  dfs(dfs, 0, -1, 0);
  
  priority_queue<pair<int,int>> pq;
  if (leafs.size() == 1) {
    if (levels[leafs.begin()->first] <= 1) {
      cout << -1 << "\n";
      return;
    }
	  if (adj[0].size()==1){
		  pair<int, int> k = {-1,-1};
		  for (auto [u, cnt] : leafs){
			  if (levels[u] >= 2) {
				  k = max(k, {cnt, u});		  
			  }
		  }
		  leafs[k.second]--;
		  ans++;
		  if (leafs[k.second] == 0)
			  leafs.erase(k.second);
	  }
	  for (auto [u, cnt] : leafs){
		  pq.push({cnt,u});  
	  }
  int ans = 0;
  while (pq.size() >= 2) {
    auto [cnt, u] = pq.top();
    pq.pop();
    auto [cnt2, v] = pq.top();
    pq.pop();
    cnt--,cnt2--;
    ans++;
    if (cnt != 0) {
      pq.push({cnt, u});
    }
    if (cnt2 != 0) {
      pq.push({cnt2, v});
    }
  }
  if (pq.size())
    ans += pq.top().first;
  cout << ans << "\n";
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int t;
  cin >> t;
  while (t--){
    solve();
  }
}

詳細信息

answer.code: In function ‘void solve()’:
answer.code:50:19: error: ‘ans’ was not declared in this scope; did you mean ‘abs’?
   50 |                   ans++;
      |                   ^~~
      |                   abs
answer.code:77:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   77 | int main() {
      |         ^~
answer.code:77:9: note: remove parentheses to default-initialize a variable
   77 | int main() {
      |         ^~
      |         --
answer.code:77:9: note: or replace parentheses with braces to value-initialize a variable
answer.code:77:12: error: a function-definition is not allowed here before ‘{’ token
   77 | int main() {
      |            ^
answer.code:85:2: error: expected ‘}’ at end of input
   85 | }
      |  ^
answer.code:6:14: note: to match this ‘{’
    6 | void solve() {
      |              ^