結果

問題 No.806 木を道に
ユーザー ate
提出日時 2019-11-22 14:02:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,014 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 204,612 KB
最終ジャッジ日時 2025-01-08 04:28:16
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
using pll = pair<ll,ll>;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
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(a>b){a=b;return 1;}return 0;}
void dump(){cout<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cout<<h<<", ";dump(forward<Ts>(t)...);}
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

void solve1();void solve2();
int main(){
  solve1();
  return 0;
}

using vertex = struct vertex{ll to,cost;vertex(ll to,ll cost):to(to),cost(cost){}};
using edge = struct edge{ll from,to,cost;edge(ll from,ll to,ll cost):from(from),to(to),cost(cost){}};
using weighted_adjlist = vector<vector<vertex>>;
using weighted_edge = vector<edge>;

void solve1(){

  ll n;cin>>n;
  vector<vector<ll>> adj(n);
  vector<ll> deg(n);
  rep(i,n-1){
    ll a,b;
    cin>>a>>b;
    a--;b--;
    adj[a].emplace_back(b);
    adj[b].emplace_back(a);
    deg[a]++;
    deg[b]++;
  }

  queue<pll> que;
  rep(i,n){
    if(deg[i]==1)que.emplace(i,-1);
  }
  vector<ll> dist(n);
  while(!que.empty()){
    auto [now,prv] = que.front();
    que.pop();
    bool update = false;
    if(prv==-1){
      dist[now] = 1;
      update = true;
    }else{
      update = chmax(dist[now],dist[prv]+1);
    }
    if(!update)continue;
    for(auto nxt:adj[now]){
      if(nxt==prv)continue;
      que.emplace(nxt,now);
    }
  }

  ll longest_path = *max_element(dist.begin(),dist.end());
  cout<<(n-longest_path)<<endl;

}
0