/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 512.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 5ms 624.0 KiB
#5 Accepted 64ms 3.039 MiB
#6 Accepted 120ms 3.031 MiB
#7 Accepted 89ms 3.027 MiB
#8 Accepted 49ms 592.0 KiB
#9 Accepted 121ms 804.0 KiB
#10 Accepted 159ms 3.074 MiB
#11 Accepted 109ms 3.391 MiB
#12 Accepted 121ms 3.133 MiB
#13 Accepted 49ms 3.441 MiB
#14 Accepted 167ms 3.207 MiB
#15 Accepted 72ms 988.0 KiB
#16 Accepted 40ms 3.219 MiB
#17 Accepted 39ms 3.203 MiB
#18 Accepted 38ms 3.199 MiB
#19 Accepted 168ms 18.801 MiB
#20 Accepted 169ms 18.766 MiB
#21 Accepted 160ms 18.641 MiB
#22 Accepted 217ms 35.637 MiB
#23 Accepted 285ms 18.535 MiB
#24 Accepted 282ms 18.523 MiB
#25 Accepted 196ms 18.832 MiB
#26 Accepted 11ms 1.809 MiB

Code

#include<bits/stdc++.h>
using namespace std;
using ll=long long int;

void fastio(){ios::sync_with_stdio(false);cin.tie(0);}

vector<ll> take_inputs_0(ll n){vector<ll> v(n);for(int i=0;i<n;i++){cin>>v[i];}return v;}
vector<ll> take_inputs_1(ll n){vector<ll> v(n);for(int i=1;i<n;i++){cin>>v[i];}return v;}

void show(auto x){ cout<<x<<"\n";}
void show1(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){cout<<v[i]<<" ";}cout<<"\n";}
void show2(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){	cout<<v[i]<<"\n";}}

void sort_it(auto &v){ sort(v.begin(),v.end());}
void reverse_it(auto &v){ reverse(v.begin(),v.end());}
void do_both(auto &v){ sort(v.begin(),v.end());reverse(v.begin(),v.end());}

auto find_max(auto v){ sort(v.begin(),v.end());reverse(v.begin(),v.end());return v[0];}
auto find_min(auto v){ sort(v.begin(),v.end());return v[0];}

vector<ll> bitRep64(ll n){vector<ll> v;int i=0;while(i<=63){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<ll> bitRep32(ll n){vector<ll> v;int i=0;while(i<=31){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<string> all_subset(ll n){vector<string> v;for(int i=0;i<(1<<n);i++){string st="";for(int j=0;j<n;j++){if(i&(1<<j)){st+="1";}else{st+="0";}}v.push_back(st);}return v;}

vector<ll> prefix_sum_0(vector<ll> v){ll n=(signed)v.size();for(int i=1;i<n;i++){v[i]=v[i-1]+v[i];}return v;}
vector<ll> prefix_sum_1(vector<ll> v){ll n=(signed)v.size();vector<ll> summ(n+1);for(int i=1;i<=n;i++){summ[i]=summ[i-1]+v[i-1];}return summ;}

///////////////////////////////////

ll n;

void dfs1(ll node,vector<ll> adj[],ll visited[],ll cnt[],ll restricted[])
{
	visited[node]=1;
	
	ll countt=0;
	
	for(auto x:adj[node])
	{
		if(!visited[x])
		{
			dfs1(x,adj,visited,cnt,restricted);
			countt+=cnt[x];
		}
	}
	
	cnt[node]+=countt;
	
	if(cnt[node]==0)
	{
		restricted[node]=1;
	}
}

void dfs2(ll node,vector<ll> adj[],ll visited[],ll cnt[],ll restricted[])
{
	visited[node]=1;

	for(auto x:adj[node])
	{
		if(!visited[x] && restricted[x]==0)
		{
			cnt[x]=cnt[node]+1;
			dfs2(x,adj,visited,cnt,restricted);
		}
	}
}

int main()
{
	int t;cin>>t;
	
	while(t--)
	{
		cin>>n;
		
		vector<ll> v(n+1);
		
		ll visited[n+1];
		ll cnt[n+1];
		
        ll restricted[n+1];
        
		for(int i=1;i<=n;i++)
		{
			cin>>v[i];
			visited[i]=0;
			restricted[i]=0;
			cnt[i]=v[i];
		}
		
		vector<ll> adj[n+1];
		
		for(int i=0;i<n-1;i++)
		{
			ll x,y;cin>>x>>y;
			
			adj[x].push_back(y);
			adj[y].push_back(x);
		}
		
		ll node=-1;
		
		for(int i=1;i<=n;i++)
		{
			if(v[i]==1)
			{
				node=i;
				break;
			}
		}
		
		if(node==-1)
		{
			show(0);
			continue;
		}
		
		
		dfs1(node,adj,visited,cnt,restricted);
		
		
		for(int i=1;i<=n;i++)
		{
			visited[i]=0;
			cnt[i]=0;
		}
		
		dfs2(node,adj,visited,cnt,restricted);
		
		ll last=-1e18;
		ll dem=-1e18;
		for(int i=1;i<=n;i++)
		{
			if(cnt[i]>dem && restricted[i]==0){last=i;dem=cnt[i];}
			visited[i]=0;
			cnt[i]=0;
		}
		
		
		ll tmp=0;
		
		for(int i=1;i<=n;i++){if(restricted[i]){tmp++;}}
		
		dfs2(last,adj,visited,cnt,restricted);
	
		
		ll maxi=0;
		
		for(int i=1;i<=n;i++)
		{
			if(cnt[i]>maxi){maxi=cnt[i];}
		}
	
		n=n-tmp;
		
		show((2*(n-1))-maxi);
	}
	
	return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++17 (G++ 13.2.0)
Submit At
2024-08-17 19:01:13
Judged At
2024-11-11 03:08:02
Judged By
Score
100
Total Time
285ms
Peak Memory
35.637 MiB