Submission #925673

# Submission time Handle Problem Language Result Execution time Memory
925673 2024-02-12T07:20:38 Z daoquanglinh2007 None (JOI16_snowy) C++17
100 / 100
23 ms 2224 KB
#include "Anyalib.h"
#include <bits/stdc++.h>
using namespace std;

#define isz(a) (int)(a).size()

const int NM = 500, D = 10;

int N, A[NM+5], B[NM+5];
bool snow[NM+5];
vector <int> adj[NM+5], arr;
int parent[NM+5], dep[NM+5], f[NM+5], g[NM+5], dp[NM+5];

void dfs(int u, int p){
	parent[u] = p;
	dep[u] = (p == -1 ? 0 : dep[p]+1);
	f[u] = g[u] = dp[u] = 0;
	for (int v : adj[u]){
		if (v == p) continue;
		dfs(v, u);
		dp[u] += dp[v];
		if (f[v]+1 > f[u]){
			g[u] = f[u];
			f[u] = f[v]+1;
		}
		else if (f[v]+1 > g[u]){
			g[u] = f[v]+1;
		}
	}
	if (f[u]+g[u] > D){
		dp[u]++;
		arr.push_back(u);
		f[u] = g[u] = 0;
	}
}

void InitAnya(int _N, int _A[], int _B[]){
	N = _N;
	for (int i = 0; i < N-1; i++){
		A[i] = _A[i], B[i] = _B[i];
		adj[A[i]].push_back(B[i]);
		adj[B[i]].push_back(A[i]);
	}
	arr.clear();
	dfs(0, -1);
	for (int i = 0; i < N-1; i++)
		if (dep[A[i]] > dep[B[i]]) swap(A[i], B[i]);
}

void Anya(int C[]){
	memset(snow, 0, sizeof(snow));
	for (int i = 0; i < N-1; i++)
		if (C[i]){
			snow[B[i]] = 1;
			Save(B[i], 1);
		}
	for (int i = 0; i < isz(arr); i++){
		int v = arr[i], cnt = 0;
		for (int u = v; u > 0; u = parent[u])
			if (snow[u]) cnt++;
		for (int j = 0; j <= 8; j++)
			Save(N+9*i+j, (cnt>>j)&1);
	}
}
#include "Borislib.h"
#include <bits/stdc++.h>
using namespace std;

#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
#define isz(a) (int)(a).size()

const int NM = 500, LOG = 10, D = 10;

int N, A[NM+5], B[NM+5];
vector <int> adj[NM+5], arr;
int parent[NM+5], dep[NM+5], f[NM+5], g[NM+5], dp[NM+5];
int ecnt = 0, ap[NM+5];
pii sp[LOG+5][2*NM+5];

void dfs(int u, int p){
	parent[u] = p;
	dep[u] = (p == -1 ? 0 : dep[p]+1);
	ap[u] = ++ecnt;
	sp[0][ap[u]] = mp(dep[u], u);
	f[u] = g[u] = dp[u] = 0;
	for (int v : adj[u]){
		if (v == p) continue;
		dfs(v, u);
		sp[0][++ecnt] = mp(dep[u], u);
		dp[u] += dp[v];
		if (f[v]+1 > f[u]){
			g[u] = f[u];
			f[u] = f[v]+1;
		}
		else if (f[v]+1 > g[u]){
			g[u] = f[v]+1;
		}
	}
	if (f[u]+g[u] > D || (p == -1 && dp[u] == 0)){
		dp[u]++;
		arr.push_back(u);
		f[u] = g[u] = 0;
	}
}

void build_sparse(){
	for (int i = 1; i <= LOG; i++)
		for (int j = 1; j+(1<<i)-1 <= ecnt; j++)
			sp[i][j] = min(sp[i-1][j], sp[i-1][j+(1<<(i-1))]);
}

void InitBoris(int _N, int _A[], int _B[]){
	N = _N;
	for (int i = 0; i < N-1; i++){
		A[i] = _A[i], B[i] = _B[i];
		adj[A[i]].push_back(B[i]);
		adj[B[i]].push_back(A[i]);
	}
	arr.clear();
	dfs(0, -1);
	for (int i = 0; i < N-1; i++)
		if (dep[A[i]] > dep[B[i]]) swap(A[i], B[i]);
	build_sparse();
}

int lca(int u, int v){
	int l = ap[u], r = ap[v];
	if (l > r) swap(l, r);
	int i = __lg(r-l+1);
	return min(sp[i][l], sp[i][r-(1<<i)+1]).se;
}

int dist(int u, int v){
	return dep[u]+dep[v]-2*dep[lca(u, v)];
}

int Boris(int v){
	int best = 0;
	for (int i = 1; i < isz(arr); i++){
		if (dist(v, arr[i]) < dist(v, arr[best])) best = i;
	}
	int ans = 0;
	for (int i = 0; i <= 8; i++)
		if (Ask(N+9*best+i)) ans += (1<<i);
		
	int u = arr[best], l = lca(u, v);
	while (u != l){
		if (Ask(u)) ans--;
		u = parent[u];
	}
	while (v != l){
		if (Ask(v)) ans++;
		v = parent[v];
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 784 KB Output is correct
2 Correct 2 ms 796 KB Output is correct
3 Correct 2 ms 840 KB Output is correct
4 Correct 3 ms 1132 KB Output is correct
5 Correct 4 ms 1192 KB Output is correct
6 Correct 5 ms 1188 KB Output is correct
7 Correct 5 ms 1184 KB Output is correct
8 Correct 5 ms 1340 KB Output is correct
9 Correct 5 ms 1192 KB Output is correct
10 Correct 5 ms 1212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1200 KB Output is correct
2 Correct 5 ms 1388 KB Output is correct
3 Correct 7 ms 1196 KB Output is correct
4 Correct 5 ms 1244 KB Output is correct
5 Correct 6 ms 1252 KB Output is correct
6 Correct 6 ms 1256 KB Output is correct
7 Correct 4 ms 1256 KB Output is correct
8 Correct 5 ms 1252 KB Output is correct
9 Correct 6 ms 1256 KB Output is correct
10 Correct 5 ms 1380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1216 KB Output is correct
2 Correct 19 ms 1728 KB Output is correct
3 Correct 19 ms 1676 KB Output is correct
4 Correct 19 ms 1736 KB Output is correct
5 Correct 19 ms 1728 KB Output is correct
6 Correct 19 ms 1668 KB Output is correct
7 Correct 19 ms 1992 KB Output is correct
8 Correct 19 ms 1672 KB Output is correct
9 Correct 19 ms 1640 KB Output is correct
10 Correct 22 ms 1852 KB Output is correct
11 Correct 20 ms 1704 KB Output is correct
12 Correct 19 ms 1728 KB Output is correct
13 Correct 19 ms 2176 KB Output is correct
14 Correct 19 ms 1660 KB Output is correct
15 Correct 22 ms 1716 KB Output is correct
16 Correct 20 ms 1660 KB Output is correct
17 Correct 19 ms 1948 KB Output is correct
18 Correct 19 ms 1728 KB Output is correct
19 Correct 19 ms 1736 KB Output is correct
20 Correct 23 ms 1724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1424 KB Output is correct
2 Correct 11 ms 1220 KB Output is correct
3 Correct 9 ms 2224 KB Output is correct
4 Correct 12 ms 1648 KB Output is correct
5 Correct 14 ms 1724 KB Output is correct
6 Correct 9 ms 1732 KB Output is correct
7 Correct 9 ms 1724 KB Output is correct
8 Correct 9 ms 1708 KB Output is correct
9 Correct 14 ms 1732 KB Output is correct
10 Correct 9 ms 1720 KB Output is correct
11 Correct 14 ms 1724 KB Output is correct
12 Correct 9 ms 1724 KB Output is correct
13 Correct 14 ms 1724 KB Output is correct
14 Correct 14 ms 1700 KB Output is correct
15 Correct 15 ms 1984 KB Output is correct
16 Correct 12 ms 1704 KB Output is correct
17 Correct 13 ms 1792 KB Output is correct
18 Correct 14 ms 1724 KB Output is correct
19 Correct 13 ms 1724 KB Output is correct
20 Correct 13 ms 1728 KB Output is correct
21 Correct 13 ms 1700 KB Output is correct
22 Correct 13 ms 1724 KB Output is correct
23 Correct 14 ms 1720 KB Output is correct
24 Correct 13 ms 1732 KB Output is correct
25 Correct 13 ms 1732 KB Output is correct
26 Correct 13 ms 1824 KB Output is correct
27 Correct 13 ms 1724 KB Output is correct
28 Correct 13 ms 1716 KB Output is correct
29 Correct 13 ms 1956 KB Output is correct
30 Correct 14 ms 1668 KB Output is correct
31 Correct 13 ms 1724 KB Output is correct
32 Correct 9 ms 1720 KB Output is correct
33 Correct 14 ms 1728 KB Output is correct
34 Correct 9 ms 1652 KB Output is correct
35 Correct 9 ms 1720 KB Output is correct
36 Correct 14 ms 1724 KB Output is correct
37 Correct 9 ms 1724 KB Output is correct
38 Correct 15 ms 1700 KB Output is correct
39 Correct 10 ms 1720 KB Output is correct
40 Correct 11 ms 1720 KB Output is correct
41 Correct 9 ms 1720 KB Output is correct
42 Correct 9 ms 1664 KB Output is correct
43 Correct 9 ms 1728 KB Output is correct
44 Correct 9 ms 1720 KB Output is correct
45 Correct 9 ms 1720 KB Output is correct
46 Correct 9 ms 1804 KB Output is correct
47 Correct 10 ms 1848 KB Output is correct
48 Correct 9 ms 1892 KB Output is correct
49 Correct 8 ms 1720 KB Output is correct
50 Correct 9 ms 1720 KB Output is correct