#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll N;
ll val[100100], val_node[100100], a, b;
const ll maxn = 3e6;
vector <int> v[100100];
vector <int> graf[100100];
bool bio[100100];
ll sol = 0;
ll nula[100100], jedan[100100];
ll curr;
void make_tree(int x)
{
bio[x] = 1;
for (int i = 0; i < graf[x].size(); i++)
{
int next = graf[x][i];
if (!bio[next])
{
v[x].push_back(next);
make_tree(next);
}
}
return;
}
void solve(int x)
{
if (val_node[x] == 1) sol += curr;
for (int i = 0; i < v[x].size(); i++)
{
int next = v[x][i];
solve(next);
if (val_node[x] == 1)
{
sol += (nula[next] * curr);
}
else
{
sol += (jedan[next] * curr);
}
}
if (v[x].size() > 1)
{
jedan[x] = jedan[v[x][0]];
nula[x] = nula[v[x][0]];
for (int i = 1; i < v[x].size(); i++)
{
int next = v[x][i];
if (val_node[x] == 1)
{
sol += (jedan[next] * jedan[x] * curr);
sol += (nula[next] * nula[x] * curr);
}
else
{
sol += (jedan[next] * nula[x] * curr);
sol += (nula[next] * jedan[x] * curr);
}
jedan[x] += jedan[next];
nula[x] += nula[next];
}
}
else
{
if (v[x].size() == 1)
{
jedan[x] = jedan[v[x][0]];
nula[x] = nula[v[x][0]];
}
}
if (val_node[x] == 1) swap(jedan[x], nula[x]);
if (val_node[x] == 1) jedan[x]++;
else nula[x]++;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> val[i];
}
for (int i = 1; i < N; i++)
{
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
memset(bio, 0, sizeof bio);
make_tree(1);
for (int i = 1; i <= log2(maxn) + 1; i *= 2)
{
curr = i;
memset(val_node, 0, sizeof val_node);
memset(nula, 0, sizeof nula);
memset(jedan, 0, sizeof jedan);
for (int j = 1; j <= N; j++)
{
if (val[j] & i) val_node[j] = 1;
}
solve(1);
}
cout << sol;
}
Compilation message
deblo.cpp: In function 'void make_tree(int)':
deblo.cpp:19:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < graf[x].size(); i++)
~~^~~~~~~~~~~~~~~~
deblo.cpp: In function 'void solve(int)':
deblo.cpp:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[x].size(); i++)
~~^~~~~~~~~~~~~
deblo.cpp:56:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < v[x].size(); i++)
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7552 KB |
Output is correct |
2 |
Correct |
11 ms |
7552 KB |
Output is correct |
3 |
Incorrect |
9 ms |
7552 KB |
Output isn't correct |
4 |
Incorrect |
10 ms |
7552 KB |
Output isn't correct |
5 |
Incorrect |
11 ms |
7552 KB |
Output isn't correct |
6 |
Incorrect |
97 ms |
22648 KB |
Output isn't correct |
7 |
Incorrect |
88 ms |
22648 KB |
Output isn't correct |
8 |
Incorrect |
133 ms |
16504 KB |
Output isn't correct |
9 |
Incorrect |
108 ms |
15996 KB |
Output isn't correct |
10 |
Incorrect |
187 ms |
14892 KB |
Output isn't correct |