#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;} auto pop_back(auto&a){assert(a.size());auto r=*a.rbegin();a.pop_back();return r;} 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<par_or_size; dsu(ll n):n(n),par_or_size(n,-1){} ll merge(ll a,ll b){ ll x=leader(a),y=leader(b); if(x==y)return x; if(-par_or_size[x]<-par_or_size[y])swap(x,y); par_or_size[x]+=par_or_size[y],par_or_size[y]=x; return x; } 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; } }; templatestruct rooted_tree:graph{ vec>rev_edge; vecdep; vecdist; vectopological; ll r; rooted_tree(ll n,ll r):graph(n),rev_edge(n,{-1,-1,WT(),-1}),dep(n),dist(n),r(r){} ll root()const{return r;} void add_edge(const edge&e){ if(e.from!=-1){ graph::add_edge(e); dep[e.to]=dep[e.from]+1; dist[e.to]=dist[e.from]+e.wt; } rev_edge[e.to]=e; topological.eb(e.to); } }; templateauto dfs_tree(const graph&g,ll s=0){ rooted_treedfs_tree(g.size(),s); vecused(g.size()); vec>st{{-1,s,WT(),-1}}; while(st.size()){ auto pu=st.back();st.pop_back(); auto[p,u,w,_]=pu; if(used[u])continue; used[u]=1; dfs_tree.add_edge(pu); fe(g[u],e)if(!used[e.to])st.eb(e); } return dfs_tree; } templateauto dfs_tree(const rooted_tree&g){return dfs_tree(g,g.root());} templateauto graph_order(const graph&g,const auto&spanning_tree){ vecused(g.size()); vec>r; fo(i,g.size())if(!used[i]){ auto t=spanning_tree(g,i); fe(t.topological,e)used[e]=1; r.eb(t.topological); } return r; } templateauto dfs_order(const graph&g){auto spanning_tree=[](const graph& g,ll s){return dfs_tree(g,s);};return graph_order(g,spanning_tree);} templateauto zip_graph_by_components(const graph&g){ ll n=g.size(); auto V=dfs_order(g); vec>res; vec>id(n); fo(i,V.size())fo(j,V[i].size())id[V[i][j]]={i,j}; fe(V,v){ grapht(v.size()); fo(i,v.size())fe(g[v[i]],e)t.add_edge(i,id[e.to].b,e.wt,e.id); res.eb(t); } 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); } ll ans=0; fe(zip_graph_by_components(G),g){ ll n=g.size(); auto es=g.get_edges(); sort(es,[&](auto&a,auto&b){return a.id>b.id;}); dsu uf(n); vecdp(n); fe(es,e){ if(e.from>e.to)continue; amax(dp[uf.merge(e.from,e.to)],max(dp[uf.leader(e.from)],dp[uf.leader(e.to)])+1); } amax(ans,dp[uf.leader(0)]); } pp(ans); }}