QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125104#5418. Color the TreesavacskaRE 1ms18792kbC++233.8kb2023-07-16 01:37:012023-07-16 01:37:02

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 01:37:02]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:18792kb
  • [2023-07-16 01:37:01]
  • 提交

answer

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;
typedef long double ld;

const int H = 17;

struct Update
{
	int pos, lef, rig;
};

vector <int> g[100005];
int cost[100005], sz[100005], metka[100005];
vector <ll> dp[100005];
vector <Update> updates[100005];
int sp[H + 3][100005], logs[100005];

int get_min(int lef, int rig)
{
 	int step = logs[rig - lef + 1];
 	return min(sp[step][lef], sp[step][rig - (1 << step) + 1]);
}

void actualize(int v, int len)
{
 	len = min(len, (int) dp[v].size());
 	//cout << "Len: " << len << '\n';
 	while (!updates[v].empty() && (int) dp[v].size() - updates[v].back().pos <= len)
 	{
 	 	Update upd = updates[v].back();
 	 	//cout << "AAAA: " << v << ' ' << upd.pos << ' ' << upd.lef << ' ' << upd.rig << '\n';
 	 	updates[v].pop_back();

 	 	int L = upd.lef - upd.pos, R = upd.rig - upd.pos;
 	 	dp[v][upd.pos] = min(dp[v][upd.pos], (ll) get_min(L, R));
 	 	upd.pos--;
 	 	if (upd.pos >= 0)
 	 	{
 	 	 	if (!updates[v].empty() && updates[v].back().pos == upd.pos) updates[v].back().rig = upd.rig;
 	 	 	else updates[v].pb(upd);
 	 	}
 	}
 	return;
}

void dfs(int v, int pr)
{
	sz[v] = 1;
	for (int u : g[v])
		if (u != pr)
		{
		 	dfs(u, v);
		 	sz[v] += sz[u];
		}
	return;
}

void write(int v)
{
 	cout << "Write: " << v << '\n';
 	cout << "Metka = " << metka[v] << '\n';
 	cout << "DP: ";
 	for (ll x : dp[metka[v]]) cout << x << ' ';
 	cout << '\n';
 	for (const auto &[pos, lef, rig] : updates[metka[v]]) cout << pos << ' ' << lef << ' ' << rig << '\n';
 	cout << "-----------\n";
 	return;
}

void dfsik(int v, int pr)
{
 	metka[v] = v;
 	int mx1 = 0, mx2 = 0;
 	for (int u : g[v])
 	{
 	 	if (u == pr) continue;
 	 	dfsik(u, v);
 	 	if (mx1 == 0 || sz[mx1] <= sz[u]) mx2 = mx1, mx1 = u;
 	 	else if (mx2 == 0 || sz[mx2] <= sz[u]) mx2 = u;
 	}
 	
 	if (mx1 == 0)
 	{
 	 	dp[metka[v]].pb(cost[0]);
 	 	//write(v);
 	 	return;
 	}
 	
 	metka[v] = metka[mx1];
 	if (mx2 == 0)
 	{
 		dp[metka[v]].pb(cost[0]);
 		updates[metka[v]].pb({(int) dp[metka[v]].size() - 2, (int) dp[metka[v]].size() - 1, (int) dp[metka[v]].size() - 1});
 		//write(v);
 		return;	 	
 	}

 	int len = (int) dp[metka[mx2]].size();
 	for (int u : g[v])
 	{
 	 	if (u == pr) continue;
 	 	actualize(metka[u], len);
 	}

 	for (int u : g[v])
 	{
 	 	if (u == pr || u == mx1) continue;
 	 	for (int i = 0; i < (int) dp[metka[u]].size(); i++)
 	 		dp[metka[v]][i] += dp[metka[u]][i];
 	}
 	dp[metka[v]].pb(cost[0]);
 	updates[metka[v]].pb({(int) dp[metka[v]].size() - 2, (int) dp[metka[v]].size() - 1, (int) dp[metka[v]].size() - 1});
 	//write(v);
 	return;
}	

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);

 	int test;
 	cin >> test;
 	for (int rep = 1; rep <= test; rep++)
 	{
 	 	int n;
 	 	cin >> n;
 	 	for (int i = 0; i < n; i++) cin >> cost[i];

 	 	logs[1] = 0;
 	 	for (int i = 2; i <= n; i++) logs[i] = logs[i / 2] + 1;
 	 	for (int i = 1; i <= n; i++) sp[0][i] = cost[i];
 	 	for (int step = 1; step <= H; step++)
 	 		for (int i = 1; i <= n; i++)
 	 		{
 	 		 	if (i + (1 << (step - 1)) <= n) sp[step][i] = min(sp[step - 1][i], sp[step - 1][i + (1 << (step - 1))]);
 	 		 	else sp[step][i] = sp[step - 1][i];
 	 		}

 	 	for (int i = 1; i <= n; i++) g[i].clear(), dp[i].clear(), updates[i].clear();

 	 	for (int i = 1; i < n; i++)
 	 	{
 	 	 	int u, v;
 	 	 	cin >> u >> v;
 	 	 	g[u].pb(v), g[v].pb(u);
 	 	}
 	 	dfs(1, -1);
 	 	dfsik(1, -1);
 	 	actualize(metka[1], n);

 	 	ll ans = 0;
 	 	for (ll x : dp[metka[1]]) ans += x;
 	 	cout << ans << '\n';
 	}

 	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 18792kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Dangerous Syscalls

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:


result: