提交时间:2024-12-08 14:13:52

运行 ID: 35262

#include<bits/stdc++.h> #define vec vector #define jp8 push_back #define ui32 unsigned int using namespace std; const int N=1e6+10; const ui32 INF=1e9+10; int f[N],a[N]; vec<int> E[N]; bool cmp(int a,int b){ return f[a]<f[b]; } void dfs(int u){ f[u]=min(INF,(ui32)(a[u])); for(auto v:E[u]){ dfs(v);f[u]=min(f[u],f[v]); } }void wt(int u){ if(E[u].size()==0) cout<<a[u]<<' '; sort(E[u].begin(),E[u].end(),cmp); for(auto v:E[u]){ wt(v); } }int n,m; int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ int x;cin>>x; while(x--){ int p;cin>>p;E[i].jp8(p); } }dfs(1);wt(1);cout.flush(); return 0; }