#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
const int MAXN = 3005;
const int INF = 0x3f3f3f3f;
struct edge { int to, nxt; } e[MAXN << 1];
int head[MAXN], e_cnt, fa[MAXN], n, a[MAXN];
bool g[MAXN], dp[MAXN];
inline void add(int x, int y) { e[ ++e_cnt ] = { y, head[ x ] }; head[ x ] = e_cnt; }
inline void addedge(int x, int y) { add( x, y ); add( y, x ); }
inline void dfs(int x)
{
for( int i = head[ x ] ; i ; i = e[ i ].nxt )
if( e[ i ].to ^ fa[ x ] )
fa[ e[ i ].to ] = x, dfs( e[ i ].to ), dp[ x ] |= ( !dp[ e[ i ].to ] && a[ x ] > a[ e[ i ].to ] );
}
inline void dfs2(int x)
{
vector < int > v; int cnt = 0, pre = 0, suf = 0;
for( int i = head[ x ] ; i ; i = e[ i ].nxt )
if( e[ i ].to ^ fa[ x ] ) v.pb( e[ i ].to ), cnt++;
for( int i = 0 ; i < cnt ; i++ ) suf += ( !dp[ v[ i ] ] && a[ x ] > a[ v[ i ] ] );
for( int i = 0 ; i < cnt ; i++ )
{
if( i ) pre += ( !dp[ v[ i - 1 ] ] && a[ x ] > a[ v[ i - 1 ] ] );
suf -= ( !dp[ v[ i ] ] && a[ x ] > a[ v[ i ] ] );
//printf( "%d -> %d pre = %d suf = %d\n", x, v[ i ], pre, suf );
if( ( !g[ x ] && a[ x ] > a[ fa[ x ] ] ) | pre | suf ) g[ v[ i ] ] = 1;
dfs2( v[ i ] );
}
}
int main()
{
#ifdef wxh010910
freopen( "data.in", "r", stdin );
#endif
scanf( "%d", &n );
for( int i = 1 ; i <= n ; i++ ) scanf( "%d", &a[ i ] );
for( int i = 1, x, y ; i < n ; i++ ) scanf( "%d%d", &x, &y ), addedge( x, y );
a[ 0 ] = INF; dfs( 1 ); dfs2( 1 );
//for( int i = 1 ; i <= n ; i++ ) printf( "%d\n", g[ i ] );
for( int i = 1 ; i <= n ; i++ )
if( dp[ i ] || ( !g[ i ] && a[ i ] > a[ fa[ i ] ] ) )
printf( "%d ", i );
}
./Main.cpp: In function ‘int main()’:
./Main.cpp:51:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf( "%d", &n );
^
./Main.cpp:52:56: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for( int i = 1 ; i <= n ; i++ ) scanf( "%d", &a[ i ] );
^
./Main.cpp:53:79: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for( int i = 1, x, y ; i < n ; i++ ) scanf( "%d%d", &x, &y ), addedge( x, y );
^