Submission #22838907


Source Code Expand

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.05.22 23:13:16
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

vector<vector<pair<int,int>>> query; // クエリ
vector<int> now; // 現在の状態
vector<vector<int>> g; // 木
vector<int> ans; // 各クエリの答え

void dfs(int v,int d = 0, int p = -1) {
    vector<int> q(query[v].size()); // 保持する状態
    for (int i = 0; i < query[v].size(); i++) q[i] = now[query[v][i].second];
    now[d]++;
    for(int u : g[v]) {
        if (p == u) continue;
        dfs(u,d+1,v);
    }
    for (int i = 0; i < q.size(); i++) {
        ans[query[v][i].first] = now[query[v][i].second] - q[i];
    }
}

int main() {
    int n;cin >> n;
    g.resize(n);
    for (int i = 0; i < n-1; i++) {
        int p;cin >> p;
        g[i+1].push_back(p-1);
        g[p-1].push_back(i+1);
    }
    int q;cin >> q;
    query.resize(n);
    now.resize(n);
    ans.resize(q);
    for (int i = 0; i < q; i++) {
        int u,d;cin >> u >> d;
        query[u-1].push_back({i,d});
    }
    dfs(0);
    for (int i = 0; i < q; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}

Submission Info

Submission Time
Task E - Count Descendants
User kanpurin
Language C++ (GCC 9.2.1)
Score 500
Code Size 1216 Byte
Status AC
Exec Time 467 ms
Memory 44116 KiB

Compile Error

./Main.cpp: In function ‘void dfs(int, int, int)’:
./Main.cpp:18:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   18 |     for (int i = 0; i < query[v].size(); i++) q[i] = now[query[v][i].second];
      |                     ~~^~~~~~~~~~~~~~~~~
./Main.cpp:24:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   24 |     for (int i = 0; i < q.size(); i++) {
      |                     ~~^~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 30
Set Name Test Cases
Sample sample_00
All binary_00, binary_01, binary_02, binary_03, binary_04, binary_05, binary_06, binary_07, binary_08, binary_09, bound_00, bound_01, bound_02, bound_03, broomlike_00, broomlike_01, broomlike_02, broomlike_03, broomlike_04, random_00, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, sample_00
Case Name Status Exec Time Memory
binary_00 AC 319 ms 18748 KiB
binary_01 AC 324 ms 14320 KiB
binary_02 AC 256 ms 17068 KiB
binary_03 AC 146 ms 16904 KiB
binary_04 AC 329 ms 18028 KiB
binary_05 AC 159 ms 13904 KiB
binary_06 AC 99 ms 15024 KiB
binary_07 AC 77 ms 8816 KiB
binary_08 AC 233 ms 15940 KiB
binary_09 AC 423 ms 23468 KiB
bound_00 AC 462 ms 44116 KiB
bound_01 AC 467 ms 44116 KiB
bound_02 AC 311 ms 6392 KiB
bound_03 AC 313 ms 6400 KiB
broomlike_00 AC 324 ms 28900 KiB
broomlike_01 AC 280 ms 30848 KiB
broomlike_02 AC 400 ms 31304 KiB
broomlike_03 AC 395 ms 24464 KiB
broomlike_04 AC 307 ms 30332 KiB
random_00 AC 83 ms 10104 KiB
random_01 AC 265 ms 20324 KiB
random_02 AC 270 ms 8708 KiB
random_03 AC 360 ms 12132 KiB
random_04 AC 393 ms 22876 KiB
random_05 AC 153 ms 5780 KiB
random_06 AC 386 ms 18052 KiB
random_07 AC 412 ms 16920 KiB
random_08 AC 124 ms 17936 KiB
random_09 AC 300 ms 21916 KiB
sample_00 AC 2 ms 3476 KiB