155302025-02-20 10:56:08feheristvanKaktuszgráfcpp17Accepted 50/502ms564 KiB
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct elem {
    int inf;
    elem* kov;
};

void beszur(elem*& first, int x) {
    elem* p = new elem;
    p->inf = x;
    p->kov = first;
    first = p;
}

const int MAXN = 1001;

elem* first[MAXN]; // Láncolt listás tárolás
int parent[MAXN];  // DFS szülő tömb
int depth[MAXN];   // Mélység tömb
bool visited[MAXN];
int max_cycle_length = 0;

void dfs(int node, int par) {
    visited[node] = true;

    for (elem* it = first[node]; it; it = it->kov) {
        int neighbor = it->inf;
        if (neighbor == par) continue;  // Szülő él kihagyása
        if (!visited[neighbor]) {
            parent[neighbor] = node;
            depth[neighbor] = depth[node] + 1;
            dfs(neighbor, node);
        } else if (depth[node] > depth[neighbor]) {
            // Kör megtalálva, hossz számítása
            int cycle_length = depth[node] - depth[neighbor] + 1;
            max_cycle_length = max(max_cycle_length, cycle_length);
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;

    for (int i = 1; i <= N; i++) {
        first[i] = NULL;  // Üres láncolt listák inicializálása
        visited[i] = false;
        parent[i] = -1;
        depth[i] = 0;
    }

    for (int i = 0; i < M; i++) {
        int A, B;
        cin >> A >> B;
        beszur(first[A], B);
        beszur(first[B], A);
    }

    for (int i = 1; i <= N; i++) {
        if (!visited[i]) {
            dfs(i, -1);
        }
    }

    cout << max_cycle_length << endl;

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