Submission #870250

#TimeUsernameProblemLanguageResultExecution timeMemory
870250sleepntsheepCat in a tree (BOI17_catinatree)C++17
Compilation error
0 ms0 KiB
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using i64 = long long;
#define N 200005

int n, d, l[N], h[N], alloc, dp[N][2], t[N];
pair<int, int> g[N];

void upd(int p, int k) { for (; p ; p-=p&-p) t[p] += k; }
int qry(int p) { int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }

void add_edge(int u, int v)
{
    int p = alloc++;
    g[p] = {v, h[u]};
    h[u] = p;
}

void dfs(int u, int p = -1, int dep = 1)
{
    l[u] = dep;
    int base = 0;
    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        dfs(v, u, dep+1);
        base += dp[v][0] - 1;
        upd(dp[v][1], 1);
    }
    dp[u][0] = base + 1, dp[u][1] = l[u];

    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        int sel = base + 1 + qry(d - (dp[v][1] - dep) + dep);
        if (dp[u][0] < sel)
        {
            dp[u][0] = sel;
            dp[u][1] = dp[v][1];
        }
        else if (dp[u][0] == sel && dp[v][1] > dp[u][1])
        {
            dp[u][1] = dp[v][1];
        }
    }

    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        upd(dp[v][1], -1);
    }
}

int main()
{
    ShinLena;
    cin >> n >> d;
    for (int i = 1, v; i < n; ++i) cin >> v, add_edge(i, v), add_edge(v, i);
    dfs(1);
    cout << dp[1][1];

    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using i64 = long long;
#define N 200005

int n, d, l[N], h[N], alloc, dp[N][2], t[N];
pair<int, int> g[N];

void upd(int p, int k) { for (; p ; p-=p&-p) t[p] += k; }
int qry(int p) { int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }

void add_edge(int u, int v)
{
    int p = alloc++;
    g[p] = {v, h[u]};
    h[u] = p;
}

void dfs(int u, int p = -1, int dep = 1)
{
    l[u] = dep;
    int base = 0;
    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        dfs(v, u, dep+1);
        base += dp[v][0] - 1;
        upd(dp[v][1], 1);
    }
    dp[u][0] = base + 1, dp[u][1] = l[u];

    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        int sel = base + 1 + qry(d - (dp[v][1] - dep) + dep);
        if (dp[u][0] < sel)
        {
            dp[u][0] = sel;
            dp[u][1] = dp[v][1];
        }
        else if (dp[u][0] == sel && dp[v][1] > dp[u][1])
        {
            dp[u][1] = dp[v][1];
        }
    }

    for (auto j = h[u]; j; j = g[u].second)
    {
        int v = g[u].first;
        if (v == p) continue;
        upd(dp[v][1], -1);
    }
}

int main()
{
    ShinLena;
    cin >> n >> d;
    for (int i = 1, v; i < n; ++i) cin >> v, add_edge(i, v), add_edge(v, i);
    dfs(1);
    cout << dp[1][1];

    return 0;
}


Compilation message (stderr)

catinatree.cpp:94:5: error: redefinition of 'int n'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |     ^
catinatree.cpp:16:5: note: 'int n' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |     ^
catinatree.cpp:94:8: error: redefinition of 'int d'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |        ^
catinatree.cpp:16:8: note: 'int d' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |        ^
catinatree.cpp:94:11: error: redefinition of 'int l [200005]'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |           ^
catinatree.cpp:16:11: note: 'int l [200005]' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |           ^
catinatree.cpp:94:17: error: redefinition of 'int h [200005]'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                 ^
catinatree.cpp:16:17: note: 'int h [200005]' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                 ^
catinatree.cpp:94:23: error: redefinition of 'int alloc'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                       ^~~~~
catinatree.cpp:16:23: note: 'int alloc' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                       ^~~~~
catinatree.cpp:94:30: error: redefinition of 'int dp [200005][2]'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                              ^~
catinatree.cpp:16:30: note: 'int dp [200005][2]' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                              ^~
catinatree.cpp:94:40: error: redefinition of 'int t [200005]'
   94 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                                        ^
catinatree.cpp:16:40: note: 'int t [200005]' previously declared here
   16 | int n, d, l[N], h[N], alloc, dp[N][2], t[N];
      |                                        ^
catinatree.cpp:95:16: error: redefinition of 'std::pair<int, int> g [200005]'
   95 | pair<int, int> g[N];
      |                ^
catinatree.cpp:17:16: note: 'std::pair<int, int> g [200005]' previously defined here
   17 | pair<int, int> g[N];
      |                ^
catinatree.cpp:97:6: error: redefinition of 'void upd(int, int)'
   97 | void upd(int p, int k) { for (; p ; p-=p&-p) t[p] += k; }
      |      ^~~
catinatree.cpp:19:6: note: 'void upd(int, int)' previously defined here
   19 | void upd(int p, int k) { for (; p ; p-=p&-p) t[p] += k; }
      |      ^~~
catinatree.cpp:98:5: error: redefinition of 'int qry(int)'
   98 | int qry(int p) { int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }
      |     ^~~
catinatree.cpp:20:5: note: 'int qry(int)' previously defined here
   20 | int qry(int p) { int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }
      |     ^~~
catinatree.cpp:100:6: error: redefinition of 'void add_edge(int, int)'
  100 | void add_edge(int u, int v)
      |      ^~~~~~~~
catinatree.cpp:22:6: note: 'void add_edge(int, int)' previously defined here
   22 | void add_edge(int u, int v)
      |      ^~~~~~~~
catinatree.cpp:107:6: error: redefinition of 'void dfs(int, int, int)'
  107 | void dfs(int u, int p = -1, int dep = 1)
      |      ^~~
catinatree.cpp:29:6: note: 'void dfs(int, int, int)' previously defined here
   29 | void dfs(int u, int p = -1, int dep = 1)
      |      ^~~
catinatree.cpp:145:5: error: redefinition of 'int main()'
  145 | int main()
      |     ^~~~
catinatree.cpp:67:5: note: 'int main()' previously defined here
   67 | int main()
      |     ^~~~