結果

問題 No.1976 Cut then Connect
ユーザー akuaakua
提出日時 2023-04-13 09:32:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 2,000 ms
コード長 6,297 bytes
コンパイル時間 5,933 ms
コンパイル使用メモリ 232,108 KB
実行使用メモリ 50,688 KB
最終ジャッジ日時 2024-05-03 12:25:35
合計ジャッジ時間 14,716 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 156 ms
34,560 KB
testcase_01 AC 157 ms
34,548 KB
testcase_02 AC 319 ms
49,920 KB
testcase_03 AC 238 ms
43,476 KB
testcase_04 AC 182 ms
37,872 KB
testcase_05 AC 224 ms
42,448 KB
testcase_06 AC 275 ms
46,488 KB
testcase_07 AC 225 ms
42,748 KB
testcase_08 AC 327 ms
50,688 KB
testcase_09 AC 297 ms
48,308 KB
testcase_10 AC 184 ms
38,596 KB
testcase_11 AC 323 ms
50,432 KB
testcase_12 AC 160 ms
35,128 KB
testcase_13 AC 269 ms
46,744 KB
testcase_14 AC 263 ms
45,700 KB
testcase_15 AC 261 ms
45,264 KB
testcase_16 AC 312 ms
49,096 KB
testcase_17 AC 201 ms
39,952 KB
testcase_18 AC 164 ms
35,712 KB
testcase_19 AC 274 ms
46,424 KB
testcase_20 AC 329 ms
50,636 KB
testcase_21 AC 246 ms
44,204 KB
testcase_22 AC 157 ms
34,432 KB
testcase_23 AC 157 ms
34,596 KB
testcase_24 AC 156 ms
34,432 KB
testcase_25 AC 158 ms
34,632 KB
testcase_26 AC 156 ms
34,432 KB
testcase_27 AC 157 ms
34,560 KB
testcase_28 AC 159 ms
34,572 KB
testcase_29 AC 159 ms
34,560 KB
testcase_30 AC 170 ms
34,560 KB
testcase_31 AC 169 ms
34,560 KB
testcase_32 AC 157 ms
34,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <atcoder/all>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
#include <functional>
using namespace std;  
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
typedef long long ll;
typedef unsigned long long ull;
const ll inf=1e18;  
using graph = vector<vector<int> > ;
using P= pair<ll,ll>;  
using vi=vector<int>;
using vvi=vector<vi>;
using vll=vector<ll>; 
using vvll=vector<vll>;
using vp=vector<P>;
using vvp=vector<vp>;
using vd=vector<double>;
using vvd =vector<vd>;
//string T="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string S="abcdefghijklmnopqrstuvwxyz";
//g++ main.cpp -std=c++17 -I .  
//cout <<setprecision(20);
//cout << fixed << setprecision(10);
//cin.tie(0); ios::sync_with_stdio(false);
const double PI = acos(-1);
int vx[]={0,1,0,-1,-1,1,1,-1},vy[]={1,0,-1,0,1,1,-1,-1};
void putsYes(bool f){cout << (f?"Yes":"No") << endl;}
void putsYES(bool f){cout << (f?"YES":"NO") << endl;}
void putsFirst(bool f){cout << (f?"First":"Second") << endl;}
void debug(int test){cout << "TEST" << " " << test << endl;}
ll pow_pow(ll x,ll n,ll mod){
    if(n==0) return 1; 
    x%=mod;
    ll res=pow_pow(x*x%mod,n/2,mod);
    if(n&1)res=res*x%mod;
    return res;
}
ll gcd(ll x,ll y){
    if(y==0)return x;
    return gcd(y,x%y);
}
 
ll lcm(ll x,ll y){
    return ll(x/gcd(x,y))*y;
}
template<class T> bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    else return false;
}
template<class T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    else return false;
}
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
ll mod =998244353;
//ll mod =1e9+7;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    //assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    if (n<0) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  } mint p(int n, int k) { return fact[n]*ifact[n-k]; } } c(2000000); 
  using vm=vector<mint> ;
using vvm=vector<vm> ;
struct edge{
  int to; ll cost;
  edge(int to,ll cost) : to(to),cost(cost){}
};
using ve=vector<edge>;
using vve=vector<ve>;
pair<ll,vi> getDiameter(vve g){
  int n=g.size();
  vi pre(n,-1);
  ll L=-1;
  auto f=[&](int s){
    rep(i,n)pre[i]=-1;
    vll dist(n, inf);
    priority_queue<P, vector<P>, greater<P>> q;
    dist[s] = 0;
    q.push(P(dist[s], s));
    while (!q.empty()){
      int v = q.top().second;
      ll d = q.top().first;
      q.pop();
      if(dist[v] < d)continue;
      for (auto u : g[v]){
        if (chmin(dist[u.to], dist[v] + u.cost)){
          pre[u.to]=v;
          q.push(P(dist[u.to], u.to));
        }
      }
    }
    ll maxv=-1;
    int id=-1;
    rep(i,n)if(chmax(maxv,dist[i]))id=i;
    L=maxv;
    return id;
  };
  int st=f(0);
  int gt=f(st); 
  vi vs;
  int now=gt;
  while(now!=-1){
    vs.push_back(now);
    now=pre[now];
  }
  return make_pair(L,vs);
}
const int N=100005;
int dp[N][2][2];
void solve(int test){
  int n; cin >> n;
  vve g(n);
  rep(i,n-1){
    int a,b; cin >> a >> b;a--;b--;
    g[a].push_back(edge(b,1));
    g[b].push_back(edge(a,1));
  }
  auto xx=getDiameter(g);
  int L=xx.first;
  vi vs=xx.second;
//  vvi dp(n,vi(2));
  auto dfs=[&](auto dfs,int v,int p,int i)->void{
    priority_queue<int> q;
    for(auto u:g[v]){
      if(u.to==p)continue;
      dfs(dfs,u.to,v,i);
      q.push(dp[u.to][i][0]+1);
      chmax(dp[v][i][1],dp[u.to][i][1]);
    }
    int now=0;
    rep(j,2)if(q.size()){
      now+=q.top();
      chmax(dp[v][i][j],now);
      q.pop();
    }
    chmax(dp[v][i][1],dp[v][i][0]);
  };
  dfs(dfs,vs[0],-1,0);
  dfs(dfs,vs.back(),-1,1);
 // rep(i,n)cout << i+1 << " " << dp[i][0] << endl;
  int ans=L;
  for(int i=1; i<=L; i++){
    /*
    int l1=i-1;
    int l2=(L+1-i)-1;
    */
    int l1=dp[vs[i-1]][1][1];
    int l2=dp[vs[i]][0][1];
    int now=0;
    chmax(now,l1);
    chmax(now,l2);
    chmax(now,1+(l1+1)/2+(l2+1)/2);
    chmin(ans,now);
  }
  cout << ans << endl;
}
//g++ main.cpp -std=c++17 -I .
int main(){cin.tie(0);ios::sync_with_stdio(false);
  int t=1;// cin >> t;
  rep(test,t)solve(test);
}
0