結果
| 問題 |
No.1817 Reversed Edges
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-24 04:28:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 145 ms / 2,000 ms |
| コード長 | 5,155 bytes |
| コンパイル時間 | 4,841 ms |
| コンパイル使用メモリ | 269,944 KB |
| 最終ジャッジ日時 | 2025-01-27 15:07:24 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 23 |
ソースコード
#include <atcoder/all>
using namespace atcoder;
#include <bits/stdc++.h>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
using namespace std;
using ll=long long;
using ld=long double;
using pll=pair<ll,ll>;
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;}
istream& operator >> (istream& is, modint1000000007& x) { unsigned int t; is >> t; x=t; return is; }
istream& operator >> (istream& is, modint998244353& x) { unsigned int t; is >> t; x=t; return is; }
istream& operator >> (istream& is, modint& x) { unsigned int t; is >> t; x=t; return is; }
ostream& operator << (ostream& os, const modint1000000007& x) { os << x.val(); return os; }
ostream& operator << (ostream& os, const modint998244353& x) { os << x.val(); return os; }
ostream& operator << (ostream& os, const modint& x) { os << x.val(); return os; }
template<class T1, class T2>istream& operator >> (istream& is, pair<T1,T2>& p) { is >> p.first >> p.second; return is; }
template<class T1, class T2>ostream& operator << (ostream& os, const pair<T1,T2>& p) { os << p.first << " " << p.second; return os;}
template<class T>istream& operator >> (istream& is, vector<T>& v) { for(T& x:v) is >> x; return is; }
template<class T>ostream& operator << (ostream& os, const vector<T>& v) {for(int i=0;i<(int)v.size();i++) {os << v[i] << (i+1 == v.size() ? "":" ");} return os;}
template<class... A> void pt() { std::cout << "\n"; }
template<class... A> void pt_rest() { std::cout << "\n"; }
template<class T, class... A> void pt_rest(const T& first, const A&... rest) { std::cout << " " << first; pt_rest(rest...); }
template<class T, class... A> void pt(const T& first, const A&... rest) { std::cout << first; pt_rest(rest...); }
template<typename V, typename H> void resize(vector<V>& vec, const H head){ vec.resize(head); }
template<typename V, typename H, typename ... T> void resize(vector<V>& vec, const H& head, const T ... tail){ vec.resize(head); for(auto& v: vec) resize(v, tail...); }
template<typename V, typename T> void fill(V& x, const T& val){ x = val; }
template<typename V, typename T> void fill(vector<V>& vec, const T& val){ for(auto& v: vec) fill(v, val); }
template<typename H> void vin(istream& is, const int idx, vector<H>& head){ is >> head[idx]; }
template<typename H, typename ... T> void vin(istream& is, const int idx, vector<H>& head, T& ... tail){ vin(is >> head[idx], idx, tail...); }
template<typename H, typename ... T> void vin(istream& is, vector<H>& head, T& ... tail){ for(int i=0; i<(int)head.size(); i++) vin(is, i, head, tail...); }
template<typename H, typename ... T> void vin(vector<H>& head, T& ... tail){ vin(cin, head, tail...); }
map<ll,ll>divisor(ll n){map<ll,ll>res;for(ll i=1;i*i<=n;i++)if(n%i==0){res[i]++;res[n/i]++;}return res;}
map<ll,ll>factrization(ll n){map<ll,ll>res;for(ll i=2;i*i<=n;i++)while(n%i==0){res[i]++;n/=i;}if(n>1)res[n]++;return res;}
static const ll inf=1e18+7;
static const ld pi=acos(-1);
static const ld eps=1e-10;
using v1=vector<ll>;
using v2=vector<v1>;
using v3=vector<v2>;
using v4=vector<v3>;
using S=ll;
S op(S a,S b){return a+b;}
S e(){return 0;}
struct Rerooting{
v1 A;
struct DP{
ll dp;
ll nv;
DP(ll dp,ll nv):dp(dp),nv(nv){}
};
struct Edge{
ll to;
ll cost;
};
const DP identity=DP(0,0);
DP merge(DP dp_cum,DP d){
return DP({dp_cum.dp+d.dp,dp_cum.nv+d.nv});
};
DP addroot(DP d,ll u,ll p=-1){
return DP({d.dp+A[u]-(p!=-1 and u>p),d.nv+1});
};
DP merge_edge(DP d,Edge e){
return d;
};
using Graph=vector<vector<Edge>>;
vector<vector<DP>>dp;
vector<DP>ans;
Graph g;
Rerooting(ll n){
g.resize(n);
dp.resize(n);
ans.assign(n,identity);
A.resize(n);
}
void addedge(ll a,ll b,ll c=1){
g[a].push_back({b,c});
}
void build(){
dfs(0);
bfs(0,identity);
}
DP dfs(ll u,ll p=-1){
DP dp_cum=identity;
ll deg=g[u].size();
dp[u]=vector<DP>(deg,identity);
for(ll i=0;i<deg;i++){
ll v=g[u][i].to;
if(v==p)continue;
dp[u][i]=dfs(v,u);
dp[u][i]=merge_edge(dp[u][i],g[u][i]);
dp_cum=merge(dp_cum,dp[u][i]);
}
return addroot(dp_cum,u,p);
}
void bfs(ll u,const DP& dpp,ll p=-1){
ll deg=g[u].size();
for(ll i=0;i<deg;i++)if(g[u][i].to==p){
dp[u][i]=dpp;
dp[u][i]=merge_edge(dp[u][i],g[u][i]);
}
vector<DP>dpl(deg+1,identity),dpr(deg+1,identity);
for(ll i=0;i<deg;i++)dpl[i+1]=merge(dpl[i],dp[u][i]);
for(ll i=deg-1;i>=0;i--)dpr[i]=merge(dpr[i+1],dp[u][i]);
ans[u]=addroot(dpl[deg],u);
for(ll i=0;i<deg;i++){
ll v=g[u][i].to;
if(v==p)continue;
bfs(v,addroot(merge(dpl[i],dpr[i+1]),u,v),u);
}
}
};
int main(void) {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;cin>>N;
Rerooting r(N);
for(ll i=0;i<N-1;i++){
ll a,b;cin>>a>>b;a--;b--;
r.addedge(a,b);
r.addedge(b,a);
}
for(ll u=0;u<N;u++)for(auto [v,w]:r.g[u])r.A[u]+=(u>v);
r.build();
for(ll i=0;i<N;i++)pt(r.ans[i].dp);
}