Home > Backend Development > PHP Tutorial > PHP operation MongoDB database summary record_PHP tutorial

PHP operation MongoDB database summary record_PHP tutorial

WBOY
Release: 2016-07-14 10:12:18
Original
921 people have browsed it

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int vis[5000];
vector<int> map[5000];
int ans;
void dfs(int k,int num)
{

    vis[k]=num;     //相当于给每个结点编号
    for(int i=0;i<map[k].size();i++)
    {
        if(!vis[map[k][i]])
            dfs(map[k][i],num+1);
        else
        {
            int tmp=vis[k]-vis[map[k][i]]+1;//找到访问过的结点了,直接编号相减加1就是环的大小
            if(tmp>2&&tmp>ans)
            ans=tmp;
        }
    }
}
int main()
{
    int n,m,a,b,c;
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        ans=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            map[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            map[a].push_back(b);
            map[b].push_back(a);
        }
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]);
            dfs(i,0);
        }
        printf("%d\n",ans);
    }
    return 0;
}
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477210.htmlTechArticle#includecstdio#includecstring#includevectorusing namespace std;int vis[5000];vectorint map[5000];int ans; void dfs(int k,int num){ vis[k]=num; //Equivalent to numbering each node for(i...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template