/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Wrong Answer 1ms 532.0 KiB
#4 Wrong Answer 53ms 788.0 KiB
#5 Time Exceeded ≥1097ms ≥8.051 MiB
#6 Time Exceeded ≥1098ms ≥7.578 MiB
#7 Time Exceeded ≥1098ms ≥9.051 MiB
#8 Wrong Answer 147ms 688.0 KiB
#9 Time Exceeded ≥1093ms ≥1.383 MiB
#10 Time Exceeded ≥1096ms ≥5.293 MiB
#11 Time Exceeded ≥1098ms ≥9.234 MiB
#12 Time Exceeded ≥1098ms ≥4.641 MiB
#13 Time Exceeded ≥1099ms ≥10.434 MiB
#14 Time Exceeded ≥1097ms ≥7.969 MiB
#15 Time Exceeded ≥1092ms ≥1.836 MiB
#16 Time Exceeded ≥1092ms ≥9.684 MiB
#17 Time Exceeded ≥1098ms ≥10.84 MiB
#18 Time Exceeded ≥1099ms ≥10.84 MiB
#19 Time Exceeded ≥1103ms ≥62.191 MiB
#20 Time Exceeded ≥1104ms ≥62.203 MiB
#21 Time Exceeded ≥1103ms ≥70.301 MiB
#22 Time Exceeded ≥1105ms ≥77.598 MiB
#23 Time Exceeded ≥1005ms ≥69.961 MiB
#24 Time Exceeded ≥1103ms ≥61.836 MiB
#25 Time Exceeded ≥1103ms ≥62.184 MiB
#26 Time Exceeded ≥1099ms ≥3.875 MiB

Code

//#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

#ifdef Mhmd_Bakr
#include "mhmd_bakr.h"
#endif

//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;

// Author: Mohamed Bakr

#define ll long long int
#define INT int32_t
#define int ll
#define vct vector
//#define int128 number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void>>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sum(v) accumulate(all(v), 0LL)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define ln "\n"
#define lln cout<<ln
#define umap unordered_map
#define yes cout<<"YES"<<ln
#define no cout<<"NO"<<ln
#define precision(x,y) fixed<<setprecision(y)<<x
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define PI 3.141592653589793238462643383279502884197
#define toDeg(theta) theta*(180.0/PI)
#define toRad(theta) theta*(PI/180.0)

template <typename T, typename C>
istream& operator>>(istream& istream, pair<T, C>& pi) { cin >> pi.first >> pi.second; return istream; }
template <typename T, typename C>
ostream& operator<<(ostream& ostream, pair<T, C>& pi) { cout << pi.first << " " << pi.second;  return ostream; }
template <typename T>
istream& operator>>(istream& istream, vector<T>& v) { for (T& it : v) cin >> it; return istream; }
template <typename T>
ostream& operator<<(ostream& ostream, vector<T>& v) { for (T it : v) cout << it << " "; return ostream; }
template <typename T>
ostream& operator<<(ostream& ostream, set<T>& v) { for (T it : v) cout << it << " "; return ostream; }


bool prime(int num) { if (num <= 1) return false; int ch = 2; while (ch * ch <= num) { if (!(num % ch)) return false; ch++; }return true; }
int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); }
int nPr(int n, int r) { int val = 1; for (int i = n - r + 1; i <= n; i++) val *= i; return val; }
int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); }
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
int biPow(int x, int y) { int val = 1; while (y) { if (y % 2) { val *= x; y--; } else { x *= x; y /= 2; } }return val; }
int modPow(int x, int y, int m) { int val = 1; x %= m; while (y) { if (y % 2) { val *= x; val %= m; y--; } else { x *= x; x %= m; y /= 2; } }return val % m; }
int sumRng(int l, int r) { return (r - l + 1) * (l + r) / 2; }

string mv[8] = { "U", "R", "D", "L", "UR","UL","DR","DL" };
int xd[9] = { -1, 0, 1,  0, -1, -1, 1,  1, 0 };
int yd[9] = { 0, 1, 0, -1,  1, -1, 1, -1, 0 };
int xk[9] = { -1, 1, 2, -2, -1, 1, 2, -2, 0 };
int yk[9] = { 2, -2, 1, -1, -2, 2, -1, 1, 0 };

struct LCA {
	vct<vct<int>>par;
	vct<int>depth, dis;
	int N = 0, LOG = 20, Root = 1;
	LCA(const vct<vct<int>>& gra, int root = 1) {
		N = gra.size();
		par = vct<vct<int>>(N, vct<int>(LOG + 1));
		depth = dis = vct<int>(N);
		ini(root, 0, gra);
	}
	void ini(int u, int p, const vct<vct<int>>& gra) {
		par[u][0] = p;
		depth[u] = depth[p] + 1;
		dis[u] = dis[p] + 1;
		for (int i = 1; i <= LOG; i++) {
			par[u][i] = par[par[u][i - 1]][i - 1];
		}
		for (auto v : gra[u]) {
			if (v != p) {
				ini(v, u, gra);
			}
		}
	}
	int lca(int u, int v) {
		if (depth[u] < depth[v]) swap(u, v);
		for (int k = LOG; k >= 0; k--)
			if (depth[par[u][k]] >= depth[v]) u = par[u][k];
		if (u == v) return u;
		for (int k = LOG; k >= 0; k--)
			if (par[u][k] != par[v][k]) u = par[u][k], v = par[v][k];
		return par[u][0];
	}
	int dist(int u, int v) {
		return dis[u] + dis[v] - (dis[lca(u, v)] << 1);
	}
};

void answer(int test)
{
	int n; cin >> n;
	set<int>mark;
	for (int i = 1; i <= n; i++) {
		int x; cin >> x;
		if (x) mark.insert(i);
	}
	vct<vct<int>>gra(n + 1);
	for (int i = 1; i < n; i++) {
		int u, v; cin >> u >> v;
		gra[u].push_back(v);
		gra[v].push_back(u);
	}
	LCA lc(gra);
	int u = *mark.begin();
	mark.erase(u);
	int ans = 0;
	while (mark.size()) {
		priority_queue<pair<int, int>>q;
		for (int v : mark) {
			if (u != v) {
				q.push({ -lc.dist(u,v),v });
			}
		}
		ans += -q.top().first;
		u = q.top().second;
		mark.erase(u);
	}
	cout << ans << ln;
}
bool multiTests = true;
INT main()
{
#ifdef Mhmd_Bakr
	//Contest : -
	//Problem : -
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	fastio;
	int tests = 1;
	if (multiTests) cin >> tests;
	for (int test = 1; test <= tests; test++)
	{
		//cout << "Case #" << test << ": ";
		answer(test);
	}
#ifdef Mhmd_Bakr
	End();
#endif
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-05 15:14:10
Judged At
2024-09-05 15:14:10
Judged By
Score
4
Total Time
≥1105ms
Peak Memory
≥77.598 MiB