标签:
9 4
//有同学用并查集也可以过的
//六度空间http://blog.csdn.net/u013167299/article/details/42321615
#include<iostream> #include<cstring> #include<string> #include<cmath> #include<map> #include<queue> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int maxn=105; const int inf=210000; vector<int> g[maxn]; bool vis[maxn]; int lev=1; int sum; int ans=1;//ans=0的话第二个样例过不去,能找到这个错误,我也是醉了 void bfs(int s) { queue<int> q; q.push(s); vis[s]=true; int tail; int last=s; int cnt=1; while(!q.empty()) { int k=q.front(); q.pop(); int n=g[k].size(); for(int i=0;i<n;i++) { if(!vis[g[k][i]]) { q.push(g[k][i]); vis[g[k][i]]=true; tail=g[k][i]; sum++; } } if(k==last) { last=tail; cnt++; if(ans<sum) { ans=sum; lev=cnt; } sum=0; } } } int main() { int n,m,i,j,k,t; freopen("in.txt","r",stdin); scanf("%d%d",&n,&m); for(i=0;i<m;i++) { int id; cin>>id>>k; for(j=0;j<k;j++) { cin>>t; g[id].push_back(t); g[t].push_back(id); } } bfs(1); printf("%d %d\n",ans,lev); return 0; }
1094. The Largest Generation (25) 此题和六度空间一个道理,记录BFS的层次
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44263553