結果
問題 | No.1817 Reversed Edges |
ユーザー |
![]() |
提出日時 | 2022-01-21 21:30:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 265 ms / 2,000 ms |
コード長 | 865 bytes |
コンパイル時間 | 4,640 ms |
コンパイル使用メモリ | 253,556 KB |
最終ジャッジ日時 | 2025-01-27 13:26:18 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
#include <stdio.h> #include <bits/stdc++.h> #include <atcoder/all> using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define Inf 1000000001 int n; vector<vector<int>> E; vector<int> ans; int get(int cur,int p){ int ret = 0; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; ret += get(to,cur); if(cur > to)ret++; } return ret; } int dfs(int cur,int p,int cv){ ans[cur] = cv; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; if(cur > to){ dfs(to,cur,cv - 1); } else{ dfs(to,cur,cv+1); } } return 0; } int main(){ cin>>n; E.resize(n); ans.resize(n,0); rep(i,n-1){ int u,v; cin>>u>>v; u--;v--; E[u].push_back(v); E[v].push_back(u); } int t = get(0,-1); dfs(0,-1,t); rep(i,n){ cout<<ans[i]<<endl; } return 0; }