QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#627444 | #9110. Zayin and Tree | huangzihao123 | AC ✓ | 476ms | 14880kb | C++20 | 2.6kb | 2024-10-10 15:56:34 | 2024-10-10 15:56:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define all(a) a.begin(),a.end()
typedef pair<int,int> PII;
typedef long long LL;
const int N = 2e5 + 10;
const int mod = 998244353;
inline int ls(int x) { return x << 1; }
inline int rs(int x) { return x << 1 | 1; }
int dx[8] = { 1,0,0,-1,-1,-1,1,1 };
int dy[8] = { 0,-1,1,0,-1,1,-1,1};
inline int gcd(int a, int b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
void solve()
{
int n;
cin >> n;
vector<int>a(n + 1);
vector<vector<int>>g(n + 1);
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i < n;i++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int minpart = 0,root = 0;
vector<int>sz(n + 1),buc(n + 1);
vector<bool>vis(n + 1);
int tol = 0;
auto getroot = [&](auto getroot,int sum,int u,int fa) -> void {
sz[u] = 1;
int maxpart = 0;
for(int v : g[u]){
if(v == fa || vis[v]) continue;
getroot(getroot,sum,v,u);
sz[u] += sz[v];
maxpart = max(maxpart,sz[v]);
}
maxpart = max(maxpart,sum - sz[u]);
if(maxpart < minpart){
minpart = maxpart;
root = u;
}
};
const int inf = 1e18;
int lstmx = inf,lstmi = inf,maxn = inf,minn = inf;
int ans = 1;
auto dfs = [&](auto dfs,int u,int fa,int mi,int mx,int d) -> void {
ans = min(ans,d + 1 - mx + mi);
maxn = min(maxn,d - mx);
minn = min(minn,d + mi);
for(int v : g[u]){
if(v == fa || vis[v]) continue;
dfs(dfs,v,u,min(mi,a[v]),max(mx,a[v]),d + 1);
}
};
auto sol = [&](auto sol,int sum,int u,int fa) -> void {
minpart = sum;
getroot(getroot,sum,u,fa);
vis[root] = 1;
lstmx = lstmi = inf;
for(int v : g[root]){
if(v == fa || vis[v]) continue;
minn = inf,maxn = inf;
dfs(dfs,v,root,min(a[root],a[v]),max(a[v],a[root]),1);
ans = min(ans,lstmx + minn + 1);
ans = min(ans,lstmi + maxn + 1);
lstmx = min(maxn,lstmx);
lstmi = min(minn,lstmi);
}
for(auto v : g[root]){
if(v == fa || vis[v]) continue;
sol(sol,sz[v],v,root);
}
};
sol(sol,n,1,0);
cout << ans << endl;
}
signed main()
{
io;
int tt;
cin >> tt;
while(tt--){
solve();
}
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 476ms
memory: 14880kb
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