結果

問題 No.806 木を道に
ユーザー ateate
提出日時 2019-11-22 15:12:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 3,686 bytes
コンパイル時間 3,012 ms
コンパイル使用メモリ 213,996 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 10:15:27
合計ジャッジ時間 3,721 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 44 ms
5,376 KB
testcase_13 AC 33 ms
5,376 KB
testcase_14 AC 44 ms
5,376 KB
testcase_15 AC 45 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 38 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 14 ms
5,376 KB
testcase_20 AC 39 ms
5,376 KB
testcase_21 AC 23 ms
5,376 KB
testcase_22 AC 52 ms
5,376 KB
testcase_23 AC 53 ms
5,376 KB
testcase_24 AC 28 ms
5,376 KB
testcase_25 AC 42 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 50 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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();void solve3();
int main(){
  solve3();
  return 0;
}

void solve3(){
  ll n;
  cin>>n;
  vector<ll>deg(n);
  rep(i,n-1){
    ll a,b;
    cin>>a>>b;
    a--;b--;
    deg[a]++;
    deg[b]++;
  }
  ll ans = 0;
  rep(i,n)ans += max(0ll,deg[i]-2);
  cout<<(ans)<<endl;
}

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 solve2(){
  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;
      if(dist[now]+1<=dist[nxt])continue;
      que.emplace(nxt,now);
    }
  }
  ll mxv=-1,mxi=-1;
  rep(i,n){
    if(chmax(mxv,dist[i]))mxi=i;
  }
  vector<bool> seen(mxv+1),visited(n);
  queue<ll> q;
  q.emplace(mxi);
  ll ans = 0;
  while(!q.empty()){
    ll v = q.front();
    q.pop();
    if(visited[v])continue;
    visited[v]=true;
    if(seen[dist[v]])ans+=max(0ll,deg[v]-2);
    else{
      seen[dist[v]] = true;
      ans += max(0ll,deg[v]-2);
    }
    for(auto nv:adj[v]){
      if(visited[nv])continue;
      q.emplace(nv);
    }
  }
  cout<<(ans)<<endl;

}

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),prvs(n,-1);
  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)prvs[now]=prv;
    }
    if(!update)continue;
    for(auto nxt:adj[now]){
      if(nxt==prv)continue;
      if(dist[now]+1<=dist[nxt])continue;
      que.emplace(nxt,now);
    }
  }

  for(auto x:dist)cout<<x<<" ";cout<<endl;
  for(auto x:prvs)cout<<x<<" ";cout<<endl;

  ll mxv=-1,mxi=-1;
  rep(i,n){
    if(chmax(mxv,dist[i]))mxi=i;
  }
  ll v = prvs[mxi];
  ll tmp = 0;
  while(prvs[v]!=-1){
    tmp += deg[v]-2;
    v = prvs[v];
  }

  cout<<(tmp)<<endl;

}
0