#include <iostream> #include <vector> using namespace std; int main() { int t, i, j, a, b, r, x, n; cin>>t; for (i=0; i<t; i++) { cin>>n; int t[n]; for (j=0; j<n; j++) { cin>>t[j]; } int k[n-1]; for (j=0; j<n-1; j++) { cin>>a>>b; k[j]=t[a]+t[b]; } int l[n]; for (j=0; j<n; j++) { l[j]=0; for (r=0; r<n; r++) { x=t[r]; if (r<j) { x=k[r]; } l[j]=(l[j]+x*x); } } for (j=n-1; j>=0; j--) { cout<<l[j]<<" "; } } 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 | #include <iostream> #include <vector> using namespace std; int main() { int t, i, j, a, b, r, x, n; cin>>t; for (i=0; i<t; i++) { cin>>n; int t[n]; for (j=0; j<n; j++) { cin>>t[j]; } int k[n-1]; for (j=0; j<n-1; j++) { cin>>a>>b; k[j]=t[a]+t[b]; } int l[n]; for (j=0; j<n; j++) { l[j]=0; for (r=0; r<n; r++) { x=t[r]; if (r<j) { x=k[r]; } l[j]=(l[j]+x*x); } } for (j=n-1; j>=0; j--) { cout<<l[j]<<" "; } } return 0; } |