Untitled
unknown
c_cpp
a year ago
830 B
13
Indexable
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'
vector<int> g[500001];
int vis[500001];
vector<int> path;
void dfs(int v, int p){
vis[v] = 1;
for(int ch : g[v]){
if(vis[ch]) continue;
dfs(ch, v);
}
path.push_back(v);
}
inline void solve(){
int n;
cin>>n;
for(int i=1; i<=n; i++){
int x, y;
cin>>x>>y;
if(x != -1) g[i].push_back(x);
if(y != -1) g[i].push_back(y);
}
for(int i=n; i>0; i--){
if(vis[i]) continue;
dfs(i, 0);
}
reverse(path.begin(), path.end());
int ans[n+1];
for(int i=0; i<n; i++){
ans[path[i]] = i+1;
}
for(int i=1; i<=n; i++) cout<<ans[i]<<" ";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}Editor is loading...
Leave a Comment