답안 #1066000

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066000 2024-08-19T14:00:11 Z EkinOnal Deblo (COCI18_deblo) C++17
45 / 90
39 ms 8712 KB
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
 
#define MAX 100007
#define pb push_back
#define mp make_pair 
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>> 
const int mod = 1e9+7;
const int INF = 1e18;
// myMap.begin()->first :  key
// myMap.begin()->second : value
 
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;;b>>=1;}return ans;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
int sum(int a,int b){return ((a%mod)+(b%mod))%mod;}
 
//typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
vi dead(MAX),v(MAX);
vi adj[MAX],degree(MAX);
//priority_queue<pii,vector<pii>,greater<pii>> pq;

int ans=0;
int dfs(int node,int par,int curr){
	int now=0;
	for(auto u : adj[node]){
		if(u==par || dead[u]) continue;
		now+=dfs(u,node,curr^v[u]);
	//	now+=curr^v[u];
	}
	return curr+now;
}

int dfs2(int node,int par,int curr){
	int now=0;
	for(auto u : adj[node]){
		if(u==par || dead[u]) continue;
		now+=dfs2(u,node,curr^v[u]);
	//	now+=curr^v[u];
	}
	return curr+now;
}

	
void solve(){
	int n; 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;
		adj[a].pb(b); adj[b].pb(a);
		degree[a]++;
		degree[b]++;
	}

	if(n>1000){
		int curr=v[1],sum=v[1];
		for(int i=2;i<=n;i++){
			curr^=v[i];
			sum+=curr;
		}
		int ans=sum;
		for(int i=2;i<=n;i++){
			sum^=v[i];
			ans+=sum;
		}
		cout<<ans<<endl;

		return;
	}


	multiset<pii> ms;
	for(int i=1;i<=n;i++){
	//	pq.push({degree[i],i});
		ms.insert({degree[i],i});
	}

	int cvp=0;
	while(ms.size()){
	//	int node=pq.top().s;  pq.pop();
		int node=(*ms.begin()).s; ms.erase(ms.begin());
		cvp+=dfs(node,node,v[node]);  dead[node]=true;
		ans=0;

		for(auto u : adj[node]){
			if(dead[u]) continue;
			auto it = lower_bound(ms.begin(),ms.end(),make_pair(degree[u],u));
			ms.erase(it);
			degree[u]--;
			ms.insert({degree[u],u});
		}

	//	cout<<node<<" "<<cvp<<endl;
	}
	cout<<cvp<<endl;
		
	return;
}
 
int32_t main() {
//	freopen("hayfeast.in", "r", stdin);
//	freopen("hayfeast.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(0);   
 
    int t=1;
 //   cin >> t;
    while (t--) solve();
 
    return 0;
}

/*
6
5 4 1 3 3 3
3 1
3 5
4 3
4 2
2 6
*/
/*
5
2 3 4 2 1
1 2
1 3
3 5
3 4
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 10 ms 5300 KB Output is correct
5 Correct 11 ms 5212 KB Output is correct
6 Incorrect 22 ms 8284 KB Output isn't correct
7 Incorrect 22 ms 8280 KB Output isn't correct
8 Incorrect 27 ms 8712 KB Output isn't correct
9 Incorrect 39 ms 8548 KB Output isn't correct
10 Incorrect 30 ms 8648 KB Output isn't correct