#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <climits>
#include <random>
#include <chrono>
#include <typeinfo>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int SIZE = 1 << 10;
int pointer = SIZE;
char buffer[SIZE];
inline char readchar(){if (pointer == SIZE){fread(buffer, 1, SIZE, stdin);pointer = 0;}return buffer[pointer++];}
inline int in(){int x=0;char c=readchar();bool neg=false;while(c!='-'&&('0'>c||c>'9')) c=readchar();if(c=='-') neg=true,c=readchar();while('0'<=c&&c<='9') x=10*x+c-'0',c=readchar();if(neg) x=-x;return x;}
inline void out(long long x){if(x<0) putchar('-'),x=-x;if(x>9) out(x/10);putchar(x%10+'0');}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l,int r){return l+rng()%(r-l+1);}
#define task "deblo"
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define nd second.first
#define rd second.second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vii vector<ii>
#define ii pair<int,int>
#define all(a) a.begin(),a.end()
#define sz(s) int(s.size())
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define _ putchar(' ')
#define __ putchar('\n')
const int N=100010;
int n,cnt,root,pw;
long long kq,re;
int c[N],g[2],f[2];
int a[N][25];
bool dd[N];
vi ad[N],nex[N];
void dfs(int u,int p)
{
c[u]=1;cnt++;
forv(v,ad[u]) if(!dd[v] && v!=p) dfs(v,u),c[u]+=c[v];
}
int ftr(int u,int p)
{
forv(v,ad[u]) if(!dd[v] && v!=p && c[v]>cnt/2) return ftr(v,u);
return u;
}
void ctr(int u,int p)
{
cnt=0;dfs(u,u);
int z=ftr(u,u);
dd[z]=1;
if(u==p) root=z;
if(u!=p) nex[p].pb(z);
forv(v,ad[z]) if(!dd[v]) ctr(v,z);
}
void dfs(int u,int p,int r,int c)
{
forv(v,ad[u]) if(!dd[v] && v!=p)
{
f[a[v][pw]^c]++;
dfs(v,u,r,a[v][pw]^c);
if(u==r)
{
re+=f[0]*g[1-a[u][pw]]+f[1]*g[a[u][pw]]+f[1-a[u][pw]];
g[0]+=f[0],g[1]+=f[1],f[0]=f[1]=0;
}
}
}
void dp(int u)
{
dd[u]=1;
f[0]=f[1]=g[0]=g[1]=0;
dfs(u,u,u,0);re+=a[u][pw];
forv(v,nex[u]) dp(v);
}
int main()
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
n=in();
forinc(i,1,n)
{
int x;x=in();
forinc(j,1,22) a[i][j]=bit(x,j);
}
forinc(i,1,n-1)
{
int u,v;u=in(),v=in();
ad[u].pb(v);
ad[v].pb(u);
}
ctr(1,1);
forinc(i,1,22)
{
reset(dd,0);
re=0,pw=i;
dp(root);
kq+=re*(1<<i-1);
}
out(kq);
}
Compilation message
deblo.cpp: In function 'int main()':
deblo.cpp:117:21: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
kq+=re*(1<<i-1);
~^~
deblo.cpp:97:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(task".inp","r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
deblo.cpp:98:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(task".out","w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
deblo.cpp: In function 'char readchar()':
deblo.cpp:23:50: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
inline char readchar(){if (pointer == SIZE){fread(buffer, 1, SIZE, stdin);pointer = 0;}return buffer[pointer++];}
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1087 ms |
5240 KB |
Time limit exceeded |
2 |
Execution timed out |
1085 ms |
5316 KB |
Time limit exceeded |
3 |
Execution timed out |
1074 ms |
5360 KB |
Time limit exceeded |
4 |
Execution timed out |
1066 ms |
5360 KB |
Time limit exceeded |
5 |
Execution timed out |
1073 ms |
5360 KB |
Time limit exceeded |
6 |
Execution timed out |
1025 ms |
5404 KB |
Time limit exceeded |
7 |
Execution timed out |
1061 ms |
5444 KB |
Time limit exceeded |
8 |
Execution timed out |
1069 ms |
5444 KB |
Time limit exceeded |
9 |
Execution timed out |
1085 ms |
5444 KB |
Time limit exceeded |
10 |
Execution timed out |
1060 ms |
5444 KB |
Time limit exceeded |