#include <iostream>
#include <vector>
#include <list>
#include <map>
using namespace std;
using ll = long long;
ll DFS(int v,
const vector<vector<int>>& E,
const vector<int>& A,
vector<bool>& VIS,
map<int, vector<ll>>& TW) {
ll S = A[v];
TW[v].push_back(A[v]);
VIS[v] = true;
vector<int> VER;
for (auto u : E[v]) {
if (!VIS[u]) {
S += DFS(u, E, A, VIS, TW);
auto& U = TW[u];
auto& V = TW[v];
for (auto tw : U) {
S += A[v]^tw;
V.push_back(A[v]^tw);
}
for (auto e : VER) {
for (auto l : U) {
for (auto r : TW[e]) {
S += A[v]^l^r;
}
}
}
VER.push_back(u);
}
}
for (auto e : VER) {
TW.erase(e);
}
return S;
}
int main(int argc, char* argv[]) {
int n;
cin >> n;
vector<int> A(n + 1);
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
vector<vector<int>> E(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
}
{
ll S = 0;
map<int, vector<ll>> TW;
{
vector<bool> V(n + 1);
S = DFS(1, E, A, V, TW);
}
/*for (int i = 1; i <= n; i++)
cout << "S[" << i << "]=" << S[i] << '\n';*/
cout << S << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Execution timed out |
1072 ms |
38968 KB |
Time limit exceeded |
7 |
Execution timed out |
1073 ms |
38960 KB |
Time limit exceeded |
8 |
Execution timed out |
1087 ms |
14512 KB |
Time limit exceeded |
9 |
Execution timed out |
1090 ms |
11868 KB |
Time limit exceeded |
10 |
Execution timed out |
1082 ms |
7848 KB |
Time limit exceeded |