#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(unused_imports)]
use std::io::Write;
use std::collections::VecDeque;

fn main() {
    input!{
      n: usize,
      m: usize,
      abs: [(usize, usize); n-1+m],
    }
    let mut edges = vec![vec![]; n];
    let mut rev_edges = vec![vec![]; n];
    let mut h = vec![0; n];
    for i in 0..abs.len() {
        let (a, b) = abs[i];
        edges[a-1].push(b-1);
        rev_edges[b-1].push(a-1);
        h[b-1] += 1;
    }
    // debug!(edges);

    let mut st = VecDeque::new();
    for i in 0..edges.len() {
        if h[i] == 0 {
            st.push_back(i);
        }
    }
    // debug!(h);

    let mut sorted = vec![];
    while let Some(i) = st.pop_front() {
        sorted.push(i);
        for &t in edges[i].iter() {
            h[t] -= 1;
            if h[t] == 0 {
                st.push_back(t);
            }
        }
    }

    let mut order = vec![0; n];
    for i in 0..n {
        let v = sorted[i];
        order[v] = i;
    }

    for i in 0..n {
        let parents = &rev_edges[i];
        // debug!(i, parents);
        if parents.len() == 0 {
            println!("{}", 0);
            continue;
        }

        if parents.len() == 1 {
            println!("{}", parents[0]+1);
            continue;
        }

        let mut pindex = 0;
        let mut porder = order[ parents[pindex] ];
        for j in 0..parents.len() {
            let po = order[ parents[j] ];
            if po > porder {
                pindex = j;
                porder = po;
            }
        }
        println!("{}", parents[pindex]+1);
    }
}


Measured memory_bytes: 23292