# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
87814 | Koschei | Deblo (COCI18_deblo) | C++17 | 236 ms | 45408 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long llint;
const int MAXN = 1e5 + 7;
const int LOG = 23;
int n;
llint s;
bool visited[MAXN];
int v[MAXN];
int parent[MAXN];
llint dp[MAXN][LOG][2];
stack<int> leaves;
vector<int> graph[MAXN];
inline bool get(int i, llint b)
{
return ((1 << b) && ((1 << b) & v[i]));
}
void prep()
{
stack<int> st;
st.push(1);
while (st.size())
{
int start = st.top();
st.pop();
int c = 0;
visited[start] = 1;
for (int next : graph[start])
{
if (!visited[next])
{
parent[next] = start;
st.push(next);
c++;
}
}
if (!c) leaves.push(start);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
prep();
for (int i = 0; i < MAXN; i++) visited[i] = 0;
while (leaves.size())
{
int vert = leaves.top();
leaves.pop();
visited[vert] = 1;
for (llint b = 0; b < LOG; b++)
{
if (get(vert, b))
{
dp[vert][b][0] = 1;
for (int v : graph[vert]) if (vert == parent[v])
{
dp[vert][b][0] += dp[v][b][1];
dp[vert][b][1] += dp[v][b][0];
}
}
else
{
dp[vert][b][1] = 1;
for (int v : graph[vert]) if (vert == parent[v])
{
dp[vert][b][0] += dp[v][b][0];
dp[vert][b][1] += dp[v][b][1];
}
}
}
int par = parent[vert];
if (par)
{
bool t = 1;
for (int nex : graph[par]) if (par == parent[nex]) t &= visited[nex];
if (t) leaves.push(par);
}
}
for (int v = 1; v <= n; v++)
{
for (llint b = 0; b < LOG; b++)
{
llint clc0 = get(v, b);
llint clc1 = !get(v, b);
if (get(v, b)) s += (1 << b);
for (int nx : graph[v])
{
if (v == parent[nx])
{
if (get(v, b))
{
s += (1 << b) * (dp[nx][b][0] * clc1);
s += (1 << b) * (dp[nx][b][1] * clc0);
clc1 += dp[nx][b][0];
clc0 += dp[nx][b][1];
}
else
{
s += (1 << b) * (dp[nx][b][0] * clc1);
s += (1 << b) * (dp[nx][b][1] * clc0);
clc1 += dp[nx][b][1];
clc0 += dp[nx][b][0];
}
}
}
}
}
cout << s;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |