#include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define ld long double #define pii pair<int,int> #define sz(x) (int)x.size() #define piii pair<pii,pii> #define precise cout<<fixed<<setprecision(10) #define st first #define nd second #define ins insert #define vi vector<int> #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int MAX=305; const int inf=1e18+9; vector<pii> P[MAX]; int cost[MAX]; int dp[MAX]; bitset<MAX> checked; set<int> sasiad[MAX]; vector<pair<pii,int>>e; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int sum=0; void dfs(int u,int fa,int nr){ dp[u]+=cost[u]; if (u>n){ //cout<<"KURWA "<<u<<" "<<n<<"\n"; exit(0); } assert(u<=n); for (auto it:P[u]){ if (it.st==fa)continue; dfs(it.st,u,it.nd); } if (nr==-1 || checked[nr]) sum+=(dp[u]*dp[u]); else if(fa!=-1) dp[fa]+=dp[u]; } int probuj(){ sum=0; fill(dp+1,dp+n+1,0); int start=rand()%n+1; dfs(start,-1,-1); return sum; } int normal[MAX]; int abnormal[MAX]; int query(int k){ shuffle(e.begin(),e.end(),rng); int ans=inf; for (int i=0;i<k;i++){ checked[e[i].nd]=1; } ans=min(ans,probuj()); int kroki=0; while (true){ bool czy=false; for (int i=1;i<=n-1;i++){ if (!checked[i])continue; for (auto it:sasiad[i]){ checked[i].flip(); checked[it].flip(); kroki++; int xd=probuj(); if (xd<ans){ ans=xd; czy=true; break; } checked[i].flip(); checked[it].flip(); } if (czy)break; } int ilea=0,ileb=0; for (int i=1;i<=n;i++){ if (checked[i])normal[++ilea]=i; else abnormal[++ileb]=i; } for (int i=1;i<=n;i++){ if (ilea==0 || ileb==0)break; int a=rand()%(ilea)+1; int b=rand()%(ileb)+1; a=normal[a]; b=abnormal[b]; if (a==b)continue; checked[a].flip(); checked[b].flip(); kroki++; int xd=probuj(); if (xd<ans){ ans=xd; czy=true; break; } checked[a].flip(); checked[b].flip(); } if (!czy)break; //cout<<"TERAZ "<<ans<<"\n"; } //cout<<"XD "<<kroki<<"\n"; return ans; } int query2(int k){ shuffle(e.begin(),e.end(),rng); shuffle(e.begin(),e.end(),rng); int ans=inf; for (int i=0;i<k;i++){ checked[e[i].nd]=1; } ans=min(ans,probuj()); int kroki=0; while (true){ int ilea=0,ileb=0; for (int i=1;i<=n;i++){ if (checked[i])normal[++ilea]=i; else abnormal[++ileb]=i; } bool czy=false; int last; for (int i=1;i<=n*2;i++){ if (ilea==0 || ileb==0)break; int a=rand()%(ilea)+1; int b=rand()%(ileb)+1; a=normal[a]; b=abnormal[b]; if (a==b)continue; checked[a].flip(); checked[b].flip(); kroki++; int xd=probuj(); if (xd<ans){ last=i; ans=xd; czy=true; break; } checked[a].flip(); checked[b].flip(); if (i-last>n/2)break; } if (!czy)break; //cout<<"TERAZ "<<ans<<"\n"; } //cout<<"XD "<<kroki<<"\n"; return ans; } int32_t main(){ BOOST; int t; cin>>t; for (int z=0;z<t;z++){ cin>>n; //cout<<"BEFORE "<<n<<"\n"; e.clear(); for (int i=1;i<=n;i++)P[i].clear(),sasiad[i].clear(); for (int i=1;i<=n;i++)cin>>cost[i]; for (int i=1;i<=n-1;i++){ int a,b; cin>>a>>b; P[a].pb(mp(b,i)); P[b].pb(mp(a,i)); e.pb({{a,b},i}); } for (int i=1;i<=n;i++){ for (auto it1:P[i]){ for (auto it2:P[i]){ if (it1!=it2) sasiad[it1.nd].insert(it2.nd); sasiad[it2.nd].insert(it1.nd); } } } for (int k=0;k<=n-1;k++){ int ans=inf; int operacje=0; if (n<=20)operacje=min(100LL,n*n)+10; else if (n<=50)operacje=50+n; else if (n<=110)operacje=20; else operacje=-1; //cout<<"XD "<<operacje<<"\n"; for (int op=1;op<=operacje;op++){ checked.reset(); ans=min(ans,query(k)); } if (n>110){ operacje=5; for (int op=1;op<=operacje;op++){ checked.reset(); ans=min(ans,query2(k)); } } //cout<<"XD "<<kroki<<"\n"; cout<<ans<<" "; } cout<<"\n"; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define ld long double #define pii pair<int,int> #define sz(x) (int)x.size() #define piii pair<pii,pii> #define precise cout<<fixed<<setprecision(10) #define st first #define nd second #define ins insert #define vi vector<int> #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int MAX=305; const int inf=1e18+9; vector<pii> P[MAX]; int cost[MAX]; int dp[MAX]; bitset<MAX> checked; set<int> sasiad[MAX]; vector<pair<pii,int>>e; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int sum=0; void dfs(int u,int fa,int nr){ dp[u]+=cost[u]; if (u>n){ //cout<<"KURWA "<<u<<" "<<n<<"\n"; exit(0); } assert(u<=n); for (auto it:P[u]){ if (it.st==fa)continue; dfs(it.st,u,it.nd); } if (nr==-1 || checked[nr]) sum+=(dp[u]*dp[u]); else if(fa!=-1) dp[fa]+=dp[u]; } int probuj(){ sum=0; fill(dp+1,dp+n+1,0); int start=rand()%n+1; dfs(start,-1,-1); return sum; } int normal[MAX]; int abnormal[MAX]; int query(int k){ shuffle(e.begin(),e.end(),rng); int ans=inf; for (int i=0;i<k;i++){ checked[e[i].nd]=1; } ans=min(ans,probuj()); int kroki=0; while (true){ bool czy=false; for (int i=1;i<=n-1;i++){ if (!checked[i])continue; for (auto it:sasiad[i]){ checked[i].flip(); checked[it].flip(); kroki++; int xd=probuj(); if (xd<ans){ ans=xd; czy=true; break; } checked[i].flip(); checked[it].flip(); } if (czy)break; } int ilea=0,ileb=0; for (int i=1;i<=n;i++){ if (checked[i])normal[++ilea]=i; else abnormal[++ileb]=i; } for (int i=1;i<=n;i++){ if (ilea==0 || ileb==0)break; int a=rand()%(ilea)+1; int b=rand()%(ileb)+1; a=normal[a]; b=abnormal[b]; if (a==b)continue; checked[a].flip(); checked[b].flip(); kroki++; int xd=probuj(); if (xd<ans){ ans=xd; czy=true; break; } checked[a].flip(); checked[b].flip(); } if (!czy)break; //cout<<"TERAZ "<<ans<<"\n"; } //cout<<"XD "<<kroki<<"\n"; return ans; } int query2(int k){ shuffle(e.begin(),e.end(),rng); shuffle(e.begin(),e.end(),rng); int ans=inf; for (int i=0;i<k;i++){ checked[e[i].nd]=1; } ans=min(ans,probuj()); int kroki=0; while (true){ int ilea=0,ileb=0; for (int i=1;i<=n;i++){ if (checked[i])normal[++ilea]=i; else abnormal[++ileb]=i; } bool czy=false; int last; for (int i=1;i<=n*2;i++){ if (ilea==0 || ileb==0)break; int a=rand()%(ilea)+1; int b=rand()%(ileb)+1; a=normal[a]; b=abnormal[b]; if (a==b)continue; checked[a].flip(); checked[b].flip(); kroki++; int xd=probuj(); if (xd<ans){ last=i; ans=xd; czy=true; break; } checked[a].flip(); checked[b].flip(); if (i-last>n/2)break; } if (!czy)break; //cout<<"TERAZ "<<ans<<"\n"; } //cout<<"XD "<<kroki<<"\n"; return ans; } int32_t main(){ BOOST; int t; cin>>t; for (int z=0;z<t;z++){ cin>>n; //cout<<"BEFORE "<<n<<"\n"; e.clear(); for (int i=1;i<=n;i++)P[i].clear(),sasiad[i].clear(); for (int i=1;i<=n;i++)cin>>cost[i]; for (int i=1;i<=n-1;i++){ int a,b; cin>>a>>b; P[a].pb(mp(b,i)); P[b].pb(mp(a,i)); e.pb({{a,b},i}); } for (int i=1;i<=n;i++){ for (auto it1:P[i]){ for (auto it2:P[i]){ if (it1!=it2) sasiad[it1.nd].insert(it2.nd); sasiad[it2.nd].insert(it1.nd); } } } for (int k=0;k<=n-1;k++){ int ans=inf; int operacje=0; if (n<=20)operacje=min(100LL,n*n)+10; else if (n<=50)operacje=50+n; else if (n<=110)operacje=20; else operacje=-1; //cout<<"XD "<<operacje<<"\n"; for (int op=1;op<=operacje;op++){ checked.reset(); ans=min(ans,query(k)); } if (n>110){ operacje=5; for (int op=1;op<=operacje;op++){ checked.reset(); ans=min(ans,query2(k)); } } //cout<<"XD "<<kroki<<"\n"; cout<<ans<<" "; } cout<<"\n"; } return 0; } |