QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358409#6307. Chase Game 2JCY_WA 1ms5932kbC++141.3kb2024-03-19 19:42:262024-03-19 19:42:27

Judging History

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

  • [2024-03-19 19:42:27]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5932kb
  • [2024-03-19 19:42:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
}
constexpr int MAXN = 1e5 + 10;
int n, ans;
vector<int> g[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int cas;
  cin >> cas;
  for (int turn = 1; turn <= cas; ++turn) {
    cin >> n;
    for (int i = 1; i <= n; ++i) g[i].clear();
    for (int i = 1, u, v; i < n; ++i) {
      cin >> u >> v;
      g[u].emplace_back(v);
      g[v].emplace_back(u);
    }
    if (n == 2) {
      cout << "-1\n";
      continue;
    }
    ans = 0;
    for (int i = 1; i <= n; ++i) ans += (g[i].size() == 1);
    vector<int> vec;
    for (int i = 1; i <= n; ++i) {
      if (g[i].size() != 1) {
        int cnt = 0;
        for (auto j : g[i]) cnt += (g[j].size() == 1);
        vec.emplace_back(cnt);
      }
    }
    int sum = accumulate(vec.begin(), vec.end(), 0), mx = *max_element(vec.begin(), vec.end());
    ans -= min(sum / 2, sum - mx);
    cout << ans << "\n";
  }
  return 0;
}
/*
g++ J.cpp -o J -std=c++14 -O2 -Wall -Wextra -Wshadow -g -fsanitize=address,undefined
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5932kb

input:

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

output:

-1
1
3
2

result:

wrong answer 3rd numbers differ - expected: '-1', found: '3'