QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#484177#9110. Zayin and TreePetroTarnavskyi#AC ✓148ms13416kbC++201.1kb2024-07-19 16:35:132024-07-19 16:35:14

Judging History

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

  • [2024-07-19 16:35:14]
  • 评测
  • 测评结果:AC
  • 用时:148ms
  • 内存:13416kb
  • [2024-07-19 16:35:13]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
 
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 20;

VI g[N];
int a[N];
int ans = 1;

pair<int, int> dfs(int v, int p)
{
	int vR = 1 - a[v];
	int vL = 1 + a[v];
	
	for(int to : g[v])
	{
		if(to == p)
			continue;
		
		auto [L, R] = dfs(to, v);
		
		ans = min(ans, vL + R);
		ans = min(ans, vR + L);
		
		vL = min(vL, L + 1);
		vR = min(vR, R + 1);
	}
	return {vL, vR};
}


void solve()
{
	int n;
	cin >> n;
	FOR(i, 0, n)
		cin >> a[i];
	FOR(i, 0, n - 1)
	{
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].PB(v);
		g[v].PB(u);
	}
	ans = 1;
	dfs(0, 0);
	cout << ans << "\n";
	
	FOR(i, 0, n)
		g[i].clear();
}


int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 148ms
memory: 13416kb

input:

3009
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
10
5 8 1 0 8 7 5 2 0 4
2 4
3 8
3 9
1 2
1 3
3 6
4 5
5 7
6 10
10
6 8 8 4 8 0 6 6 0 2
7 10
1 7
2 9
2 3
3 4
1 5
1 6
6 8
1 2
10
9 0 4 0 4 6 0 2 0 0
1 5
1 3
1 7
2 6
1 2
1 9
1 4
5 8
7 10
10
8 8 1 2 7 4 8 6 0 8
1 6
1 7
1 5
7 9
1 3
1 2
2 10
3 4
1 8...

output:

0
-1
-6
-6
-7
-6
-7
-4
-3
-7
-5
-6
-5
-4
-6
-3
-4
-7
-4
-4
-6
-6
-6
-5
-4
-5
-6
-6
-7
-7
-5
-7
-6
-6
-7
-6
-5
-5
-4
-6
-6
-5
-6
-6
-6
-6
-3
-6
-3
-6
-4
-6
-7
-6
-7
-6
-6
-5
-7
-6
-4
-7
-3
-5
-5
-6
-4
-5
-7
-6
-5
-5
-4
-3
-5
-3
-4
-2
-6
-5
-7
-4
-5
-5
-7
-7
-4
-6
-5
-4
-6
-5
-5
-6
-3
-6
-7
-7
-7
-6
-...

result:

ok 3009 lines