제출 #1007759

#제출 시각아이디문제언어결과실행 시간메모리
1007759NamkhingJOI tour (JOI24_joitour)C++17
0 / 100
126 ms34860 KiB
#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 i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cou[j][i] = 0; } dp[i] = 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) { 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]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...