Home > php教程 > php手册 > body text

PHP操作MongoDB 数据库总结记录

WBOY
Release: 2016-06-13 10:58:17
Original
1101 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

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template