#include #include using namespace std; namespace my{ #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll ij=n;ij--;) #define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);isync_with_stdio(0);cout<constexpr auto range(bool s,A...a){arrayr{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;} constexpr char nl=10; constexpr char sp=32; bool amax(auto&a,const auto&b){return a>{a...});} templatestruct pair{ A a;B b; pair()=default; pair(A a,B b):a(a),b(b){} pair(const std::pair&p):a(p.first),b(p.second){} auto operator<=>(const pair&)const=default; friend ostream&operator<<(ostream&o,const pair&p){return o<>auto&sort(auto&a,const F&f={}){ranges::sort(a,f);return a;} templateostream&operator<<(ostream&o,const std::pair&p){return o<concept vectorial=is_base_of_v,V>; templatestruct vec_attr{using core_type=T;static constexpr int d=0;}; templatestruct vec_attr{using core_type=typename vec_attr::core_type;static constexpr int d=vec_attr::d+1;}; templateusing core_t=vec_attr::core_type; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateostream&operator<<(ostream&o,const vector&v){fe(v,e)o<?nl:sp);return o;} templatestruct vec:vector{ using vector::vector; vec(const vector&v){vector::operator=(v);} vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;} vec operator^(const vec&u)const{return vec{*this}^=u;} vec&operator++(){fe(*this,e)++e;return*this;} vec&operator--(){fe(*this,e)--e;return*this;} vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;} auto scan(const auto&f)const{pair,bool>r{};fe(*this,e)if constexpr(!vectorial)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;return r;} auto max()const{return scan([](auto&a,const auto&b){a>...>>a);} templatevoid pp(const auto&...a){ll n=sizeof...(a);((cout<0,c)),...);cout<d; dsu(ll n):n(n),d(n,-1){} ll leader(ll a){ assert(0<=a&&astruct edge{ ll from,to; WT wt; ll id; edge()=default; edge(ll from,ll to,WT wt=1,ll id=-1):from(from),to(to),wt(wt),id(id){} auto operator<=>(const edge&e)const{return wt<=>e.wt;} friend ostream&operator<<(ostream&o,const edge&e){return o<<"(to "<struct graph{ vec>>edges; graph()=default; graph(ll n):edges(n){} decltype(auto)operator[](ll i){return edges[i];} decltype(auto)operator[](ll i)const{return edges[i];} ll size()const{return edges.size();} friend ostream&operator<<(ostream&o,const graph&g){ fo(u,g.size()){ o<<"from "<void add_edge(ll a,A&&...b){edges[a].eb(a,std::forward(b)...);} void add_edge(const edge&e){edges[e.from].eb(e);} vec>get_edges()const{ vec>res; fo(u,size())fe(edges[u],e)res.eb(e); return res; } }; single_testcase void solve(){ LL(N,M); graphG(N); fo(i,M){ LL(a,b);dec(a,b); G.add_edge(a,b,1,i); G.add_edge(b,a,1,i); } dsu uf(N); vecdp(N); auto es=G.get_edges(); sort(es,[&](auto&a,auto&b){return a.id>b.id;}); fe(es,e){ if(e.from>e.to)continue; ll nl=uf.merge(e.from,e.to); amax(dp[nl],max(dp[uf.leader(e.from)],dp[uf.leader(e.to)])+1); } pp(dp.max()); }}