QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#748744#9565. Birthday GiftacansaidongCompile Error//C++204.7kb2024-11-14 21:17:212024-11-14 21:17:22

Judging History

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

  • [2024-11-14 21:17:22]
  • 评测
  • [2024-11-14 21:17:21]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;

const int N=2e5+10;
const int M=0x3f3f3f3f3f3f3f3f;
vector<int> e[N];
int a[N], b[N], vis[N];
int yu, n;  // 记录重心的全局变量
int size[N];  // 记录每个节点的子树大小

// 计算每个节点的子树大小
void dfs(int node, int parent) {
    size[node] = 1; // 当前节点自身算一个
    for (auto child : e[node]) {
        if (child != parent) {
            dfs(child, node); // 递归计算子树
            size[node] += size[child]; // 当前节点的子树大小是子节点的子树大小加上自身
        }
    }
}

// 查找重心
void find_centroid(int node, int parent, int total_size) {
    bool is_centroid = true;
    int max_subtree = 0;

    // 检查当前节点的子树和父树的大小
    for (auto child : e[node]) {
        if (child != parent) {
            max_subtree = max(max_subtree, size[child]);
            if (size[child] > total_size / 2) {
                is_centroid = false;
            }
        }
    }

    // 检查父树的大小
    if (total_size - size[node] > total_size / 2) {
        is_centroid = false;
    }

    if (is_centroid) {
        yu = node; // 找到重心
        return;
    }

    // 如果当前节点不是重心,继续递归
    for (auto child : e[node]) {
        if (child != parent) {
            find_centroid(child, node, total_size);
            if (yu != -1) break; // 找到重心就停止递归
        }
    }
}

void f() {
    queue<pair<int, int>> q;
    for (int i = 1; i <= n; i++) 
        if (e[i].size() == 1) q.push({i, 1});
    
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        
        if (vis[x] != 0 && vis[x] != y) continue;
        vis[x] = max(vis[x], y);
        
        for (auto i : e[x]) {
            if (vis[i] == 0) q.push({i, y + 1});
        }
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;

    while (T--) {
        cin >> n;
        int ans = 1;

        // 初始化
        for (int i = 1; i <= n; i++) {
            vis[i] = 0;
            e[i].clear();
            size[i] = 0; // 清空每个节点的子树大小
        }

        // 读取树的边信息
        for (int i = 1; i <= n; i++) {
            int x, y;
            cin >> x >> y;
            a[i] = x;
            b[i] = y;
            if (y) {
                e[i].push_back(y);
                e[y].push_back(i);
            }
            if (x) {
                e[i].push_back(x);
                e[x].push_back(i);
            }
        }

        // 1. 从根节点开始计算每个节点的子树大小
        dfs(1, -1);

        // 2. 查找重心节点
        yu = -1;
        find_centroid(1, -1, size[1]);

        // 输出计算出的重心节点
        // cout << "The centroid node is: " << yu << endl;

        // 继续执行原来的操作
        int u, v, t, la = 0, mx = -1;
        int yui = floor(log2(n));
        f();

        for (int i = 1; i <= n; i++) {
            if (vis[i] > mx) mx = vis[i], yu = i;
            else if (vis[i] == mx && e[i].size() > e[yu].size()) yu = i;

            vis[i] = 0;
        }

        while (yui--) {
            int sum = 0;
            for (auto i : e[yu]) if (vis[i] == 0) sum++;

            if (sum == 0) { ans = u; break; }
            else if (sum == 1) {
                u = yu;
                for (auto i : e[yu]) if (vis[i] == 0) v = i;
            } else if (sum == 2) u = e[yu][0], v = e[yu][1];
            else {
                u = v = -1;
                for (auto i : e[yu]) {
                    if (vis[i] == 0 && u == -1) {
                        u = i;
                    } else if (vis[i] == 0 && u != -1) {
                        v = i; break;
                    }
                }
            }

            vis[u] = vis[v] = 1;
            // cout << sum << " ";
            cout << "? " << u << " " << v << endl;
            cin >> t;

            if (sum == 1) {
                if (t == 0) { ans = u; break; }
                else { ans = v; break; }
            } else if (sum == 2) {
                if (t == 1) { ans = yu; break; }
                else if (t == 0) yu = u, ans = u;
                else yu = v, ans = v;
            } else {
                if (t == 0) yu = u, ans = u;
                else if (t == 1) { vis[yu] = 0; for (auto i : e[yu]) if (vis[i] == 0) { yu = i, ans = i; break; } }
                else if (t == 2) yu = v, ans = v;
            }
        }

        cout << "! " << ans << "\n";
    }

    return 0;
}

詳細信息

answer.code: In function ‘void dfs(long long int, long long int)’:
answer.code:15:5: error: reference to ‘size’ is ambiguous
   15 |     size[node] = 1; // 当前节点自身算一个
      |     ^~~~
In file included from /usr/include/c++/13/string:53,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from answer.code:1:
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code:19:13: error: reference to ‘size’ is ambiguous
   19 |             size[node] += size[child]; // 当前节点的子树大小是子节点的子树大小加上自身
      |             ^~~~
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code:19:27: error: reference to ‘size’ is ambiguous
   19 |             size[node] += size[child]; // 当前节点的子树大小是子节点的子树大小加上自身
      |                           ^~~~
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code: In function ‘void find_centroid(long long int, long long int, long long int)’:
answer.code:32:44: error: reference to ‘size’ is ambiguous
   32 |             max_subtree = max(max_subtree, size[child]);
      |                                            ^~~~
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code:33:17: error: reference to ‘size’ is ambiguous
   33 |             if (size[child] > total_size / 2) {
      |                 ^~~~
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code:40:22: error: reference to ‘size’ is ambiguous
   40 |     if (total_size - size[node] > total_size / 2) {
      |                      ^~~~
/usr/include/c++/13/bits/range_access.h:274:5: note: candidates are: ‘template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])’
  274 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/13/bits/range_access.h:264:5: note:                 ‘template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)’
  264 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
answer.code:11:5: note:                 ‘long long int size [200010]’
   11 | int size[N];  // 记录每个节点的子树大小
      |     ^~~~
answer.code: In function ‘int main()’:
answer....