#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; int main() { int n, m; cin >> n >> m; vector a(m), b(m), k(m); REP(i, m) cin >> a[i] >> b[i] >> k[i], --a[i], --b[i]; vector>>> graph(n, vector>>(1)); vector dst(m); REP(i, m) { dst[i] = graph[a[i]].size(); graph[a[i]].emplace_back(); graph[b[i]].back().emplace_back(a[i], dst[i]); } // REP(i, m) cout << dst[i] << " \n"[i + 1 == m]; vector> l(n), r(n); REP(i, n) { l[i].resize(graph[i].size()); r[i].resize(graph[i].size()); } vector children; function dfs = [&](int src1, int src2) { l[src1][src2] = children.size(); children.emplace_back(src1); reverse(ALL(graph[src1][src2])); for (auto [dst1, dst2] : graph[src1][src2]) dfs(dst1, dst2); r[src1][src2] = children.size(); }; REP(i, n) dfs(i, 0); // REP(i, n + m) cout << children[i] << " \n"[i + 1 == n + m]; // REP(i, n) REP(j, graph[i].size()) cout << i << ' ' << j << " : " << l[i][j] << ' ' << r[i][j] << '\n'; vector latest(n); REP(i, n) latest[i] = l[i][graph[i].size() - 1]; sort(ALL(latest)); // REP(i, n) cout << latest[i] << " \n"[i + 1 == n]; function solve = [&](int left, int right, int &k) { // cout << left << ' ' << right << '\n'; int cnt = upper_bound(ALL(latest), right) - lower_bound(ALL(latest), left); if (cnt < k) { k -= cnt; return false; } int idx = lower_bound(ALL(latest), left) - latest.begin(); cout << children[latest[idx + k - 1]] + 1 << '\n'; return true; }; REP(i, m) { int prev = l[a[i]][dst[i] - 1]; assert(solve(0, prev, k[i]) || solve(l[a[i]][dst[i]], r[a[i]][dst[i]] - 1, k[i]) || solve(prev + 1, l[a[i]][dst[i]] - 1, k[i]) || solve(r[a[i]][dst[i]], n + m - 1, k[i])); } return 0; }