답안 #342086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342086 2021-01-01T10:55:25 Z ruatin Deblo (COCI18_deblo) C++14
45 / 90
333 ms 16260 KB
#include <bits/stdc++.h>
using namespace std;
#define forinc(i, a, b) for(int i=a; i<=b; ++i)
#define fordec(i, a, b) for(int i=a; i>=b; --i)
#define forb(i, BS) for(int i=BS._Find_first(); i<BS.size(); i = BS._Find_next(i))
#define forv(a, b) for(auto &a:b)
#define pb push_back
#define all(a) a.begin(),a.end()
#define bit(x,i) ((x>>i)&1)
#define onbit(x,i) (x|(1<<i))
#define offbit(x,i) (x&~(1<<i))
#define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef long long lli;
typedef unsigned long long llu;
typedef pair<int, int> pii;
#define st first
#define nd second

const int N = 1e5 + 5, mod = 1e9+7;

int n, val[N];
vector<int> ke[N];

void readInput()
{
	cin >> n;
	forinc(i, 1, n) cin >> val[i];
	int u, v;
	forinc(i, 1, n-1)
	{
		cin >> u >> v;
		ke[u].pb(v);
		ke[v].pb(u);
	}
}

int dd[N], s[N], d[N];

void DFS(int u,int p)
{
	s[u] = 1;
	for(int v : ke[u]) if(v != p && !dd[v])
	{
        DFS(v, u);
        s[u] += s[v];
	}
}

int Find(int u,int p,int cnt)
{
    int vm = 0;
    for(int v:ke[u]) if(v!=p && !dd[v])
    {
        if(s[v] > s[vm]) vm = v;
    }
    if(s[vm] <= cnt/2) return u;
    else return Find(vm, u, cnt);
}

vector<int> tmp;
void get(int u, int p)
{
	tmp.pb(u);
	for(int v : ke[u]) if(v != p && !dd[v])
	{
		d[v] = (d[u] ^ val[v]);
		get(v, u);
	}
}

int cnt[24], pre[24][2], lg = 22;

void centroid(int u)
{
	dd[u] = 1;
    DFS(u, 0);

    vector<int> child;
    memset(pre, 0, sizeof pre);

    forinc(b, 0, lg)
    {
    	if(bit(val[u], b)) cnt[b]++;
    	pre[b][bit(val[u], b)]++;
    }

    for(int v : ke[u]) if(!dd[v])
	{
        tmp.clear();
        d[v] = val[v]; get(v, u);

        for(int w : tmp)
		{
			forinc(b, 0, lg)
			{
				if(bit(d[w], b)) cnt[b] += pre[b][0];
				else cnt[b] += pre[b][1];
			}
		}

        for(int w : tmp)
		{
			int value = (d[w] ^ val[u]);
			forinc(b, 0, lg) pre[b][bit(value, b)]++;
		}

		child.pb(Find(v, u, s[v]));
	}
	for(int w : child) centroid(w);
}

int main()
{
    if(fopen("input.txt","r"))
    {
        freopen("input.txt","r",stdin);
//        freopen("output.txt","w",stdout);
    }
    fastio;

	readInput();

	memset(dd, 0, sizeof dd);
	DFS(1, 0);

	centroid(Find(1, 0, n));

	lli ans = 0;
	forinc(i, 0, lg) ans += (1ll << i) * cnt[i];
	cout << ans;
}

Compilation message

deblo.cpp: In function 'int main()':
deblo.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  117 |         freopen("input.txt","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3052 KB Output is correct
2 Correct 3 ms 3052 KB Output is correct
3 Correct 3 ms 3052 KB Output is correct
4 Correct 4 ms 3180 KB Output is correct
5 Correct 5 ms 3180 KB Output is correct
6 Incorrect 333 ms 16260 KB Output isn't correct
7 Incorrect 322 ms 16232 KB Output isn't correct
8 Incorrect 319 ms 9840 KB Output isn't correct
9 Incorrect 323 ms 9196 KB Output isn't correct
10 Incorrect 328 ms 8684 KB Output isn't correct