QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#484170 | #9107. Zayin and Count | PetroTarnavskyi# | TL | 0ms | 0kb | C++20 | 1.1kb | 2024-07-19 16:31:59 | 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 ...