Poj 3895 cycles of lanes (DFS)

Source: Internet
Author: User

Cycles of lanes
Time limit:1000 ms
Memory limit:65536 K
Total submissions:1062
Accepted:392

Description

Each of the M lanes of the park of Polytechnic University of Bucharest connects two of the N crossroads of the Park (labeled from 1 to n ). there is no pair of Crossroads connected by more than one lane and it is possible to pass from each crossroad to each other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once.
The administration of the University wocould like to put on the lanes pictures of the Winners of regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of same simple cycle. that is why the administration wowould like to assign the longest simple cycles of lanes to most successful universities. the problem is to find the longest cy Cles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the figure ).

Input

On the first line of the input file the number t of the test cases will be given. each test case starts with a line with the positive integers n and M, separated by interval (4 <= n <= 4444 ). each of the next M lines of the test case contains the labels of one of the pairs of Crossroads connected by a lane.

Output

For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.

Sample Input

1 7 8 3 4 1 4 1 3 7 1 2 7 7 5 5 6 6 2

Sample output

4

Source

Southeastern European regional programming contest 2009


The longest ring in an undirected graph

Direct DFS sequentially accesses each vertex number. If the accessed vertex is already accessed, the Link number is subtracted, And the link length is

Storing images with arrays will exceed the memory, so we need to store them with vectors.

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int N = 4500;vector<int> ma[N];int vis[N], a, b, cas, n, m, ans;int dfs (int i, int no){    vis[i] = no;    for (int j = 0; j < ma[i].size(); ++j)    {        if (!vis[ma[i][j]]) dfs (ma[i][j], no + 1);        else ans = max (ans, no - vis[ma[i][j]] + 1);    }}int main(){    scanf ("%d", &cas);    while (cas--)    {        scanf ("%d%d", &n, &m);        for (int i = 1; i <= n; ++i) ma[i].clear();        for (int i = 1; i <= m; ++i)        {            scanf ("%d%d", &a, &b);            ma[a].push_back (b);            ma[b].push_back (a);        }        ans = 0;        memset (vis, 0, sizeof (vis));        for (int i = 1; i <= n; ++i)            if (!vis[i]) dfs (i, 1);        if (ans >= 3) printf ("%d\n", ans);        else printf ("0\n");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.