답안 #86289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
86289 2018-11-26T00:54:13 Z kraljlavova1 Deblo (COCI18_deblo) C++11
90 / 90
394 ms 17196 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
const int MAXN=100010,MP=25;
int n;
int u,v;
int pot;
int val[MAXN],pref[MAXN];
int dp[MAXN][2];
vector<int>graf[MAXN];
ll sol,sum;
void calc_pref(int x,int par,int last){
	pref[x]=last^val[x];
	for(auto y:graf[x]){
		if(y==par) continue;
		calc_pref(y,x,pref[x]);
	}
}
void calc_dp(int x,int par){
	dp[x][0]=(((1<<pot)&pref[x])==0);
	dp[x][1]=!dp[x][0];
	for(auto y:graf[x]){
		if(y==par) continue;
		calc_dp(y,x);
		dp[x][0]+=dp[y][0];
		dp[x][1]+=dp[y][1];
	}
}
int PREF(int x){
	if(x==-1) return 0;
	return pref[x];
}
void solve(int x,int par){
	int c=(val[x]&(1<<pot));
	if(c){
		int n0=0,n1=0;
		ll t=0;
		for(auto y:graf[x]){
			if(y==par) continue;
			n0+=dp[y][0];
			n1+=dp[y][1];
		}
		for(auto y:graf[x]){
			if(y==par) continue;
			t+=1ll*dp[y][1]*(n1-dp[y][1]);
			t+=1ll*dp[y][0]*(n0-dp[y][0]);
		}
		sum+=t/2;
	}
	else{
		int n0=0;
		for(auto y:graf[x]){
			if(y==par) continue;
			n0+=dp[y][0];
		}
		for(auto y:graf[x]){
			if(y==par) continue;
			sum+=1ll*dp[y][1]*(n0-dp[y][0]);
		}
	}
	if(PREF(par)&(1<<pot)){
		for(auto y:graf[x]){
			if(y==par) continue;
			sum+=dp[y][0];
		}
	}
	else{
		for(auto y:graf[x]){
			if(y==par) continue;
			sum+=dp[y][1];
		}
	}
	for(auto y:graf[x]){
		if(y==par) continue;
		solve(y,x);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>val[i];
	}
	for(int i=1;i<n;i++){
		cin>>u>>v;u--;v--;
		graf[u].push_back(v);
		graf[v].push_back(u);
	}
	calc_pref(0,-1,0);
	for(pot=0;pot<MP;pot++){
		sum=0;
		calc_dp(0,-1);
		solve(0,-1);
		sol+=sum*(1<<pot);
	}
	for(int i=0;i<n;i++) sol+=val[i];
	cout<<sol<<"\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2684 KB Output is correct
3 Correct 4 ms 2744 KB Output is correct
4 Correct 8 ms 2872 KB Output is correct
5 Correct 6 ms 2932 KB Output is correct
6 Correct 181 ms 15772 KB Output is correct
7 Correct 159 ms 17196 KB Output is correct
8 Correct 227 ms 17196 KB Output is correct
9 Correct 235 ms 17196 KB Output is correct
10 Correct 394 ms 17196 KB Output is correct