結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー kzyKTkzyKT
提出日時 2021-04-19 01:04:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 2,631 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 210,944 KB
実行使用メモリ 58,556 KB
最終ジャッジ日時 2023-09-17 08:07:27
合計ジャッジ時間 8,947 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
27,360 KB
testcase_01 AC 11 ms
27,308 KB
testcase_02 AC 10 ms
27,404 KB
testcase_03 AC 10 ms
27,308 KB
testcase_04 AC 12 ms
27,528 KB
testcase_05 AC 21 ms
28,152 KB
testcase_06 AC 22 ms
28,052 KB
testcase_07 AC 16 ms
27,784 KB
testcase_08 AC 24 ms
28,240 KB
testcase_09 AC 22 ms
28,084 KB
testcase_10 AC 21 ms
28,140 KB
testcase_11 AC 19 ms
27,988 KB
testcase_12 AC 18 ms
28,156 KB
testcase_13 AC 11 ms
27,400 KB
testcase_14 AC 233 ms
34,912 KB
testcase_15 AC 220 ms
34,492 KB
testcase_16 AC 196 ms
34,020 KB
testcase_17 AC 131 ms
32,560 KB
testcase_18 AC 271 ms
35,572 KB
testcase_19 AC 327 ms
36,836 KB
testcase_20 AC 318 ms
36,896 KB
testcase_21 AC 310 ms
37,020 KB
testcase_22 AC 305 ms
36,912 KB
testcase_23 AC 312 ms
36,840 KB
testcase_24 AC 313 ms
36,896 KB
testcase_25 AC 317 ms
37,064 KB
testcase_26 AC 325 ms
36,848 KB
testcase_27 AC 336 ms
36,836 KB
testcase_28 AC 322 ms
36,904 KB
testcase_29 AC 209 ms
58,556 KB
testcase_30 AC 218 ms
43,648 KB
testcase_31 AC 111 ms
37,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

vector<int> v[211111];
int d[211111],n;

P dfs(int x,int t) {
  d[x]=t;P p=P(d[x],x);
  rep(i,v[x].size())if(d[v[x][i]]==MAX)p=max(p,dfs(v[x][i],t+1));
  return p;
}

P calc() {
  fill(d,d+211111,MAX);P p=dfs(0,0);
  fill(d,d+211111,MAX);P q=dfs(p.S,0);
  return P(p.S,q.S);
}

P pp;
set<ll> s;
ll dfs2(int x,int p) {
  if(x==pp.S) {
    s.in(x);
    return 1;
  }
  ll f=0;
  rep(i,v[x].size()) {
    ll y=v[x][i];
    if(y==p) continue;
    f|=dfs2(y,x);
  }
  if(f) s.in(x);
  return f;
}

ll a[222222],k=1;

void dfs3(ll x,ll p) {
  a[x]=k++;
  ll z=-1;
  rep(i,v[x].size()) {
    ll y=v[x][i];
    if(y==p) continue;
    if(s.count(y)) {
      z=y;
      continue;
    }
    dfs3(y,x);
    k++;
  }
  if(z!=-1) {
    dfs3(z,x);
    k++;
  }
}


int dep[200010];
int par[200010][20];
void dfs(int x,int y,int d){
  dep[x]=d;
  for(int i=0; i<v[x].size(); i++){
    int z=v[x][i];if(z==y) continue;
    par[z][0]=x;dfs(z,x,d+1);
  }
}
int lca(int a,int b){
  if(dep[a]<dep[b]) swap(a,b);
  for(int i=0; i<20; i++){
    if((dep[a]-dep[b])&(1<<i)) a=par[a][i];
  }
  if(a==b) return a;
  for(int i=19;i>=0;i--){
    if(par[a][i]!=par[b][i]) a=par[a][i],b=par[b][i];
  }
  return par[a][0];
}
void init() {
  memset(par,0,sizeof(par));
  dfs(0,-1,0);
  for(int i=0; i<19; i++)for(int j=0; j<n; j++) par[j][i+1]=par[par[j][i]][i];
}
int D(int x,int y) {
  return dep[x]+dep[y]-dep[lca(x,y)]*2;
}
void Main() {
  R n;
  rep(i,n-1) {
    ll x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  pp=calc();
  dfs2(pp.F,-1);
  dfs3(pp.F,-1);
  init();
  P b[n];
  rep(i,n) b[i]=P(a[i],i);
  sort(b,b+n);
  ll ans=0;
  rep(i,n-1) ans+=D(b[i].S,b[i+1].S);
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0