233902026-01-21 15:51:14ubormaciKaktuszgráfcpp17Accepted 50/502ms508 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int32_t ll;

const ll inf = INT32_MAX;

vector<vector<ll>> v;
vector<bool> vis;
vector<ll> maxc;
vector<ll> p;
vector<ll> d;

void dfs(ll curr) {

    vis[curr] = true;

    //cerr << "\ncurr=" << curr;

    for(const auto & ch : v[curr])
    {
        //cerr << "\nch? " << ch;

        if(ch == p[curr]) {
            //cerr << "\nparent >:(";
            continue;
        }
        
        if(vis[ch] == false) {
            vis[ch] = true;
            //cerr << "\nnew node omg";
            p[ch] = curr;
            d[ch] = d[curr]+1;
            dfs(ch);
        }
        else if(vis[ch] == true && maxc[curr] == 0) {

            maxc[curr] = max(maxc[curr], d[curr] - d[ch] + 1);

            /*
            //cerr << "\ncylce ding ding ding";
            //cerr << "\nfrom " << curr << "; back to " << ch;
            // cycle ding ding ding
            // we go backwards until ch, and count
            // then we go through again, 

            ll c = curr;
            ll d = 1;
            while(c != ch) {
                c = p[c];
                d++;
            }

            //cerr << "\nd=" << d;

            c = curr;
            while(c != ch) {
                maxc[c] = max(maxc[c], d);
                c = p[c];
            }
            maxc[ch] = max(maxc[ch], d);
            */
        }   

    }
}   

void solve() {

    // varom hogy mi a trukk
    // de nem kene azert elbizni magam

    ll n, m;
    cin >> n >> m;

    v.resize(n+1);
    vis.assign(n+1, false);
    maxc.assign(n+1, 0);
    p.assign(n+1, 0);
    d.assign(n+1, inf);
    for(ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    // 1e7 bele kene ferjen . . . n^2?
    // minden edge-re egy bfs-t?
    // lassuk, voltak gyengebb tesztek is

    //cerr << "\nv=" << v;
    
    ll ans = 0;

    for(ll i = 1; i <= n; i++) {

        if(vis[i] == false) {
            d[i] = 0;
            dfs(i);
        }

        ans = max(ans, maxc[i]);

    }

    cout << ans << "\n";

}   

int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    solve();

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted2/21ms508 KiB
4Accepted2/21ms492 KiB
5Accepted2/21ms332 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms500 KiB
9Accepted2/21ms508 KiB
10Accepted2/21ms316 KiB
11Accepted2/21ms316 KiB
12Accepted2/21ms484 KiB
13Accepted2/21ms316 KiB
14Accepted2/21ms316 KiB
15Accepted2/21ms316 KiB
16Accepted2/21ms316 KiB
17Accepted2/22ms500 KiB
18Accepted2/21ms476 KiB
19Accepted3/31ms508 KiB
20Accepted3/31ms316 KiB
21Accepted3/32ms316 KiB
22Accepted3/32ms316 KiB
23Accepted3/32ms316 KiB
24Accepted3/32ms316 KiB