204922026-01-07 12:16:49szabelrKaktuszgráfcpp17Accepted 50/502ms512 KiB
// Kaktuszgráf.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
using namespace std;
void dfs(vector<vector<int>>& adj, int& maxi, int i, vector<int>& dist)
{
    for (auto next : adj[i])
    {
        if (dist[next] == -1)
        {
            dist[next] = dist[i] + 1;
            dfs(adj, maxi, next, dist);
        }
        else if(next!=i)
        {
            if (dist[i] - dist[next] + 1 > maxi)
                maxi = dist[i] - dist[next]+1;
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    for (int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    int maxi = 0;
    vector<int>dist(n + 1, -1);
    dist[1] = 0;
    dfs(adj, maxi, 1,dist);
    cout << maxi;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms508 KiB
2Accepted0/01ms316 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms316 KiB
9Accepted2/21ms316 KiB
10Accepted2/21ms476 KiB
11Accepted2/21ms480 KiB
12Accepted2/21ms444 KiB
13Accepted2/21ms468 KiB
14Accepted2/21ms316 KiB
15Accepted2/21ms456 KiB
16Accepted2/21ms316 KiB
17Accepted2/21ms316 KiB
18Accepted2/21ms316 KiB
19Accepted3/31ms456 KiB
20Accepted3/31ms316 KiB
21Accepted3/32ms316 KiB
22Accepted3/32ms508 KiB
23Accepted3/32ms508 KiB
24Accepted3/32ms512 KiB