Submission #4101570


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct TopologicalSort {
    vector<vector<int>> E; TopologicalSort(int N) { E.resize(N); }
    void add_edge(int a, int b) { E[a].push_back(b); }
    bool visit(int v, vector<int>& order, vector<int>& color) { color[v] = 1;
        for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false;
        if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; }
    bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n);
        for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false;
        reverse(order.begin(), order.end()); return true; } };
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/






int N, M;
vector<int> E[101010];
int dep[101010];
//---------------------------------------------------------------------------------------------------
int ans[101010];
void _main() {
    cin >> N >> M;

    TopologicalSort ts(N);

    rep(i, 0, N + M - 1) {
        int a, b; cin >> a >> b;
        a--; b--;
        E[a].push_back(b);
        ts.add_edge(a, b);
    }
    
    vector<int> ord;
    ts.sort(ord);

    fore(cu, ord) fore(to, E[cu]) chmax(dep[to], dep[cu] + 1);

    int root = ord[0];
    ans[root] = -1;
    rep(cu, 0, N) fore(to, E[cu]) if (dep[cu] + 1 == dep[to]) ans[to] = cu;

    rep(i, 0, N) printf("%d\n", ans[i] + 1);
}

Submission Info

Submission Time
Task D - Restore the Tree
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2584 Byte
Status AC
Exec Time 74 ms
Memory 14708 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 34
Set Name Test Cases
Sample a01, a02
All a01, a02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34
Case Name Status Exec Time Memory
a01 AC 2 ms 2688 KB
a02 AC 2 ms 2688 KB
b03 AC 2 ms 2688 KB
b04 AC 2 ms 2688 KB
b05 AC 2 ms 2688 KB
b06 AC 60 ms 14708 KB
b07 AC 74 ms 14324 KB
b08 AC 57 ms 14708 KB
b09 AC 18 ms 3840 KB
b10 AC 18 ms 3840 KB
b11 AC 18 ms 3840 KB
b12 AC 31 ms 7660 KB
b13 AC 31 ms 7660 KB
b14 AC 30 ms 7916 KB
b15 AC 49 ms 10084 KB
b16 AC 37 ms 6864 KB
b17 AC 48 ms 9356 KB
b18 AC 34 ms 5424 KB
b19 AC 61 ms 11316 KB
b20 AC 35 ms 6404 KB
b21 AC 48 ms 9224 KB
b22 AC 39 ms 7100 KB
b23 AC 36 ms 7804 KB
b24 AC 42 ms 8124 KB
b25 AC 46 ms 8740 KB
b26 AC 55 ms 11568 KB
b27 AC 34 ms 6140 KB
b28 AC 44 ms 8368 KB
b29 AC 54 ms 11560 KB
b30 AC 60 ms 11316 KB
b31 AC 58 ms 10412 KB
b32 AC 52 ms 10336 KB
b33 AC 50 ms 9956 KB
b34 AC 38 ms 6400 KB