#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<long long, long long> par;
vector<par> graf[100009];
vector<int> bum[100009];
long long pole[100009], col[100009], wyn, x;
queue<int> q;
void DFS(int u, long long r, int oj)
{
par p;
bum[x].pb(u);
for(int i=0; i<graf[u].size(); i++)
{
p = graf[u][i];
if(p.fi == oj) continue;
if(r >= p.se) DFS(p.fi, r - p.se, u);
}
return;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
long long n, a, b, c;
cin>>n;
for(int i=1; i<=n; i++) cin>>pole[i];
for(int i=1; i<n; i++)
{
cin>>a>>b>>c;
graf[a].pb(par(b, c));
graf[b].pb(par(a, c));
}
for(int i=1; i<=n; i++)
{
x = i;
DFS(i, pole[i], -1);
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++) col[j] = 0;
wyn = 1;
q.push(i);
col[i] = 1;
while(!q.empty())
{
if(wyn == n) break;
a = q.front();
q.pop();
for(int j=0; j<bum[a].size(); j++)
{
b = bum[a][j];
if(col[b] == 0)
{
wyn++;
col[b] = 1;
q.push(b);
}
}
}
while(!q.empty()) q.pop();
cout<<wyn<<" ";
}
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 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef pair<long long, long long> par; vector<par> graf[100009]; vector<int> bum[100009]; long long pole[100009], col[100009], wyn, x; queue<int> q; void DFS(int u, long long r, int oj) { par p; bum[x].pb(u); for(int i=0; i<graf[u].size(); i++) { p = graf[u][i]; if(p.fi == oj) continue; if(r >= p.se) DFS(p.fi, r - p.se, u); } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, a, b, c; cin>>n; for(int i=1; i<=n; i++) cin>>pole[i]; for(int i=1; i<n; i++) { cin>>a>>b>>c; graf[a].pb(par(b, c)); graf[b].pb(par(a, c)); } for(int i=1; i<=n; i++) { x = i; DFS(i, pole[i], -1); } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) col[j] = 0; wyn = 1; q.push(i); col[i] = 1; while(!q.empty()) { if(wyn == n) break; a = q.front(); q.pop(); for(int j=0; j<bum[a].size(); j++) { b = bum[a][j]; if(col[b] == 0) { wyn++; col[b] = 1; q.push(b); } } } while(!q.empty()) q.pop(); cout<<wyn<<" "; } return 0; } |
English