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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 200006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n; int A[MAXN] , deg[MAXN]; vi G[MAXN];
struct ed { int u , v , w , id; } E[MAXN] ; int id;
int dfn[MAXN] , clo; void dfs( int u , int fa ) { dfn[u] = clo + 1; if( u != 1 && deg[u] == 1 ) ++ clo; for( int v : G[u] ) if( v != fa ) dfs( v , u ); E[++ id] = { dfn[u] , clo + 1 , A[u] , u }; }
bool cmp( ed u , ed v ) { return u.w < v.w; }
int fa[MAXN]; int find( int x ) { return x == fa[x] ? x : fa[x] = find( fa[x] ); }
void solve() { cin >> n; rep( i , 1 , n ) scanf("%d",A + i); int u , v; rep( i , 2 , n ) scanf("%d%d",&u,&v) , G[u].pb( v ) , G[v].pb( u ) , ++ deg[u] , ++ deg[v]; dfs( 1 , 1 ); sort( E + 1 , E + 1 + n , cmp ); rep( i , 1 , clo + 1 ) fa[i] = i; ll ans = 0; vi re; for( int i = 1 , j ; i <= n ; i = j ) { j = i; while( j != n + 1 && E[j].w == E[i].w ) { u = find( E[j].u ) , v = find( E[j].v ); if( u != v ) re.pb( E[j].id ); ++ j; } rep( k , i , j - 1 ) { u = find( E[k].u ) , v = find( E[k].v ); if( u != v ) fa[u] = v , ans += E[k].w; } } cout << ans << ' ' << re.size() << endl; sort( all( re ) ); for( int i : re ) printf("%d ",i); }
signed main() {
solve(); }
|