QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#484170#9107. Zayin and CountPetroTarnavskyi#TL 0ms0kbC++201.1kb2024-07-19 16:31:592024-07-19 16:31:59

Judging History

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

  • [2024-07-19 16:31:59]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-07-19 16:31:59]
  • 提交

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: 0
Time Limit Exceeded

input:

10000
1 0 0 0 1 1 0 0 0 1
0 0 1 0 1 1 1 1 0 0
950595954440050004054505054050
1 0 0 0 1 1 1 1 0 0
1 1 1 0 1 0 0 0 1 1
45467007076660767550460064
1 1 1 1 0 0 0 1 0 0
1 1 0 1 1 0 1 0 0 1
23373171320213300170200722
0 0 0 0 1 1 1 0 1 0
0 0 1 0 0 1 0 1 1 1
558565664666565565558468668484
1 1 0 0 1 0 1 0 1 ...

output:


result: