标签:code name size max can tor scanf turn int
大水题,dfs判连通块的数量,bfs每个点找朋友圈的最大直径~
#include<bits/stdc++.h> using namespace std; const int maxn=1014; vector<int> g[maxn]; bool visit[maxn]; int N; int maxdepth=0; void dfs (int s) { visit[s]=true; for (int i=0;i<g[s].size();i++) if (visit[g[s][i]]==false) dfs(g[s][i]); } int dfsTrave () { int block=0; for (int i=1;i<=N;i++) if (visit[i]==false) dfs(i),block++; return block; } void bfs (int s) { int depth[maxn]={0}; queue<int> q; fill (visit,visit+maxn,false); q.push(s); visit[s]=true; while (!q.empty()) { int now=q.front(); q.pop(); for (int i=0;i<g[now].size();i++) if (visit[g[now][i]]==false) { q.push(g[now][i]); depth[g[now][i]]=depth[now]+1; visit[g[now][i]]=true; maxdepth=max(maxdepth,depth[g[now][i]]); } } } int main () { scanf ("%d",&N); int k,x; for (int i=1;i<=N;i++) { scanf ("%d",&k); for (int j=1;j<=k;j++) { scanf ("%d",&x); g[i].push_back(x); g[x].push_back(i); } } int block=dfsTrave(); for (int i=1;i<=N;i++) bfs (i); printf ("%d %d",block,max(0,maxdepth-1)); return 0; }
标签:code name size max can tor scanf turn int
原文地址:https://www.cnblogs.com/zhanglichen/p/12302909.html