zoukankan      html  css  js  c++  java
  • POJ 3895 Cycles of Lanes(DFS)

    Cycles of Lanes
    Time Limit: 1000MS
    Memory Limit: 65536K
    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 would 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 a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? 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


    题意  求无向图中的最长环

    直接dfs 依次对访问每个点编号   遇到已经访问过的点就是环了  编号相减就是环的长度

    用数组存图会超内存  所以要用vector存

    #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
    ", ans);
            else printf ("0
    ");
        }
        return 0;
    }


  • 相关阅读:
    QT::QString 很全的使用
    QString与TCHAR/wchar_t/LPWSTR之间的类型转换
    Django day24 cbv和APIView的源码分析 和 resful的规范
    Django day17 博客项目(一)
    Django day16 Auth组件
    Git 和 Redis 的基本认识
    Django day15 (二) csrf的 跨站请求伪造 与 局部禁用 , 局部使用
    Django day15 (一) cbv装饰器 , 中间件
    Django day14(二) Session
    Django基础必备三件套: HttpResponse render redirect
  • 原文地址:https://www.cnblogs.com/acvay/p/3947319.html
Copyright © 2011-2022 走看看