CP-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Honam0905/CP-library

:heavy_check_mark: test/yosupo/Tree/tree_diameter_yosupo.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/tree_diameter"
#include "Misc/marco.hpp"
#include "Misc/debug.hpp"
const int INF=1e9;
const ll INFI=1e15;
//----------Author: Nguyen Ho Nam,UIT, Saigon-----------------
#include "tree/Tree_diameter.hpp"
int main() {
  FT;
  int n; cin>>n;
  tree_diameter<ll>tre(n);
  rep(i,n-1){
     ll a,b,c; cin>>a>>b>>c;
     tre.add_edge(a,b,c,true);
  }
  auto [wei,pat]=tre.get_diameter();
  cout<<wei<<" "<<pat.size()<<'\n';
  rep(i,pat.size()){
     cout<<pat[i]<<" ";
  }
  cout<<'\n';
    return 0;
}
#line 1 "test/yosupo/Tree/tree_diameter_yosupo.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/tree_diameter"
#line 2 "Misc/marco.hpp"
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ods;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;

using ld = long double;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
 
 
#define pii pair<int,int>
#define pll pair<ll,ll>
 
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ars(x) (x),(x+n)
 
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)
 
#define For(i,a,b) for (int i=(a); i<(b); i++)
#define rep(i,a) For(i,0,a)
#define rev(i,a,b) for (int i=(a); i>(b); i--)
#define FOR(i,a,b) for (int i=(a); i<=(b); i++)
#define REP(i,a) FOR(i,1,a)
#define REV(i,a,b) for (int i=(a); i>=(b); i--)
 
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define FT ios_base::sync_with_stdio(false); cin.tie(nullptr);
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

using vi=vector<int>;
using vll = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
 
#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
 
//template <class T>
//using ods =
//   tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
 
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#line 1 "Misc/debug.hpp"
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
template<>
void __print(const vector<bool> &x) {int f = 0; cerr << '{'; for (size_t i = 0; i < x.size(); ++i) cerr << (f++ ? ", " : ""), __print(x[i]); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { __print(H); if (sizeof...(T)) cerr << ", "; dbg_out(T...); }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:"; dbg_out(__VA_ARGS__);
#line 4 "test/yosupo/Tree/tree_diameter_yosupo.test.cpp"
const int INF=1e9;
const ll INFI=1e15;
//----------Author: Nguyen Ho Nam,UIT, Saigon-----------------
#line 2 "tree/Tree_diameter.hpp"
/*
   @brief:Tree-Diameter
   author:Kiffaz
*/
template<typename T> struct tree_diameter {
    struct Edge {
        int to;
        T weight;
    };

    int n;
    std::vector<std::vector<Edge>> adj;

    tree_diameter(int size) : n(size), adj(size) {}

    void add_edge(int s, int t, T w, bool use_weight) {
        if (use_weight) {
            adj[s].push_back(Edge{t, w});
            adj[t].push_back(Edge{s, w});
        } else {
            adj[s].push_back(Edge{t, 1}); 
            adj[t].push_back(Edge{s, 1});
        }
    }

    std::pair<T, int> dfs(int v, int parent) {
        T max_dist = 0;
        int farthest_node = v;
        for (auto& edge : adj[v]) {
            if (edge.to != parent) {
                auto sub = dfs(edge.to, v);
                T dist = sub.first + edge.weight;
                if (dist > max_dist) {
                    max_dist = dist;
                    farthest_node = sub.second;
                }
            }
        }
        return {max_dist, farthest_node};
    }

    std::pair<T, std::vector<int>> get_diameter() {
        auto [_, first_farthest] = dfs(0, -1); 
        auto [max_dist, second_farthest] = dfs(first_farthest, -1); 

        std::vector<int> path;
        std::vector<bool> visited(n, false);
        std::function<bool(int, int)> find_path = [&](int current, int target) -> bool {
            visited[current] = true;
            path.push_back(current);
            if (current == target) return true;
            for (auto& edge : adj[current]) {
                if (!visited[edge.to] && find_path(edge.to, target)) {
                    return true;
                }
            }
            path.pop_back();
            return false;
        };

        find_path(second_farthest, first_farthest);

        return {max_dist, path};
    }
};
#line 8 "test/yosupo/Tree/tree_diameter_yosupo.test.cpp"
int main() {
  FT;
  int n; cin>>n;
  tree_diameter<ll>tre(n);
  rep(i,n-1){
     ll a,b,c; cin>>a>>b>>c;
     tre.add_edge(a,b,c,true);
  }
  auto [wei,pat]=tre.get_diameter();
  cout<<wei<<" "<<pat.size()<<'\n';
  rep(i,pat.size()){
     cout<<pat[i]<<" ";
  }
  cout<<'\n';
    return 0;
}
Back to top page