Submission #1007767

# Submission time Handle Problem Language Result Execution time Memory
1007767 2024-06-25T12:52:40 Z Namkhing JOI tour (JOI24_joitour) C++17
20 / 100
3000 ms 39988 KB
#include "joitour.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll n, q;
vector<ll> f, dp, order;
vector<vector<ll>> adj, cou;

void dfs(int u, int p) {
    for (ll& v : adj[u]) {
        if (v == p) {
            swap(v, adj[u].back());
            adj[u].pop_back();
            break;
        }
    }
    for (int v : adj[u]) {
        dfs(v, u);
    }   
    order.push_back(u);
}

void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V, int Q) {
    n = N, q = Q;
    f.resize(N);
    dp.resize(N);
    cou.resize(3, vector<ll>(N));
    adj.resize(N);

    for (int i = 0; i < N; i++) {
        f[i] = F[i];
    }

    for (int j = 0; j < N - 1; j++) {
        adj[U[j]].push_back(V[j]);
        adj[V[j]].push_back(U[j]);
    }

    dfs(0, -1);
}

void change(int X, int Y) {
    f[X] = Y;
}

long long num_tours() {
    for (int u = 0; u < n; u++) {
        dp[u] = cou[0][u] = cou[1][u] = cou[2][u] = 0;
    }
    
    for (int u : order) {
        cou[f[u]][u] = 1;
        
        for (int v : adj[u]) {
            for (int i = 0; i < 3; i++) {
                cou[i][u] += cou[i][v];
            }
        }
    }

    ll sum = 0;
    
    for (int u : order) {
        if (f[u] == 1) {
            ll sum = 0;
            for (int v : adj[u]) {
                sum += cou[0][v] * cou[2][v];
            }
            
            dp[u] += cou[0][u] * cou[2][u] - sum;
            dp[u] += cou[0][u] * (cou[2][0] - cou[2][u]);
            dp[u] += cou[2][u] * (cou[0][0] - cou[0][u]);
        }

        for (int v : adj[u]) {
            dp[u] += dp[v];
        }
    }
    
    return dp[0];
}

Compilation message

joitour.cpp: In function 'long long int num_tours()':
joitour.cpp:62:8: warning: unused variable 'sum' [-Wunused-variable]
   62 |     ll sum = 0;
      |        ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 65 ms 1104 KB Output is correct
26 Correct 50 ms 972 KB Output is correct
27 Correct 42 ms 856 KB Output is correct
28 Correct 38 ms 1112 KB Output is correct
29 Correct 57 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 88 ms 34868 KB Output is correct
3 Correct 113 ms 34492 KB Output is correct
4 Correct 78 ms 32580 KB Output is correct
5 Correct 81 ms 35380 KB Output is correct
6 Correct 57 ms 28724 KB Output is correct
7 Correct 59 ms 28808 KB Output is correct
8 Correct 73 ms 28980 KB Output is correct
9 Correct 79 ms 29236 KB Output is correct
10 Correct 78 ms 29628 KB Output is correct
11 Correct 78 ms 30260 KB Output is correct
12 Correct 82 ms 32440 KB Output is correct
13 Correct 98 ms 32796 KB Output is correct
14 Correct 97 ms 32440 KB Output is correct
15 Correct 84 ms 31904 KB Output is correct
16 Correct 86 ms 37172 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 80 ms 28604 KB Output is correct
22 Correct 79 ms 28600 KB Output is correct
23 Correct 78 ms 28468 KB Output is correct
24 Correct 87 ms 28608 KB Output is correct
25 Correct 65 ms 29116 KB Output is correct
26 Correct 67 ms 28872 KB Output is correct
27 Correct 63 ms 28980 KB Output is correct
28 Correct 68 ms 28980 KB Output is correct
29 Correct 88 ms 39988 KB Output is correct
30 Correct 60 ms 39884 KB Output is correct
31 Correct 67 ms 39988 KB Output is correct
32 Correct 64 ms 39884 KB Output is correct
33 Correct 62 ms 29132 KB Output is correct
34 Correct 89 ms 28872 KB Output is correct
35 Correct 83 ms 28864 KB Output is correct
36 Correct 85 ms 28980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 39 ms 1184 KB Output is correct
9 Correct 65 ms 39876 KB Output is correct
10 Correct 59 ms 39988 KB Output is correct
11 Correct 64 ms 39988 KB Output is correct
12 Correct 59 ms 39984 KB Output is correct
13 Execution timed out 3075 ms 20032 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 43 ms 856 KB Output is correct
8 Correct 57 ms 29108 KB Output is correct
9 Correct 69 ms 28868 KB Output is correct
10 Correct 76 ms 28868 KB Output is correct
11 Correct 59 ms 28980 KB Output is correct
12 Execution timed out 3020 ms 14660 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 65 ms 1104 KB Output is correct
26 Correct 50 ms 972 KB Output is correct
27 Correct 42 ms 856 KB Output is correct
28 Correct 38 ms 1112 KB Output is correct
29 Correct 57 ms 856 KB Output is correct
30 Execution timed out 3009 ms 15936 KB Time limit exceeded
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 65 ms 1104 KB Output is correct
26 Correct 50 ms 972 KB Output is correct
27 Correct 42 ms 856 KB Output is correct
28 Correct 38 ms 1112 KB Output is correct
29 Correct 57 ms 856 KB Output is correct
30 Correct 0 ms 344 KB Output is correct
31 Correct 88 ms 34868 KB Output is correct
32 Correct 113 ms 34492 KB Output is correct
33 Correct 78 ms 32580 KB Output is correct
34 Correct 81 ms 35380 KB Output is correct
35 Correct 57 ms 28724 KB Output is correct
36 Correct 59 ms 28808 KB Output is correct
37 Correct 73 ms 28980 KB Output is correct
38 Correct 79 ms 29236 KB Output is correct
39 Correct 78 ms 29628 KB Output is correct
40 Correct 78 ms 30260 KB Output is correct
41 Correct 82 ms 32440 KB Output is correct
42 Correct 98 ms 32796 KB Output is correct
43 Correct 97 ms 32440 KB Output is correct
44 Correct 84 ms 31904 KB Output is correct
45 Correct 86 ms 37172 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 0 ms 344 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 1 ms 344 KB Output is correct
50 Correct 80 ms 28604 KB Output is correct
51 Correct 79 ms 28600 KB Output is correct
52 Correct 78 ms 28468 KB Output is correct
53 Correct 87 ms 28608 KB Output is correct
54 Correct 65 ms 29116 KB Output is correct
55 Correct 67 ms 28872 KB Output is correct
56 Correct 63 ms 28980 KB Output is correct
57 Correct 68 ms 28980 KB Output is correct
58 Correct 88 ms 39988 KB Output is correct
59 Correct 60 ms 39884 KB Output is correct
60 Correct 67 ms 39988 KB Output is correct
61 Correct 64 ms 39884 KB Output is correct
62 Correct 62 ms 29132 KB Output is correct
63 Correct 89 ms 28872 KB Output is correct
64 Correct 83 ms 28864 KB Output is correct
65 Correct 85 ms 28980 KB Output is correct
66 Correct 0 ms 344 KB Output is correct
67 Correct 0 ms 344 KB Output is correct
68 Correct 1 ms 344 KB Output is correct
69 Correct 1 ms 344 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 1 ms 344 KB Output is correct
72 Correct 2 ms 344 KB Output is correct
73 Correct 39 ms 1184 KB Output is correct
74 Correct 65 ms 39876 KB Output is correct
75 Correct 59 ms 39988 KB Output is correct
76 Correct 64 ms 39988 KB Output is correct
77 Correct 59 ms 39984 KB Output is correct
78 Execution timed out 3075 ms 20032 KB Time limit exceeded
79 Halted 0 ms 0 KB -