2 해설
-
4
bfs 即可,总结来说是道图 + bfs。
首先读入关系 , 相连,如果用 表示 , 连通,那么 。
然后对于每个结点() bfs,如果说 (没有访问过),那么对结点 bfs 其所有连通(直接或者间接连通)的点,遍历完后对于答案 的贡献为 。
那么对于答案 ,直接一边 bfs,一边累加家庭人员,每次 bfs 后比较最大值即可。
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,x,y) for(int i=x;i<=y;i++) #define rrep(i,x,y) for(int i=x;i>=y;i--) #define sc scanf #define pr printf #define max(a,b) a>b?a:b #define endline pr("\n") inline int read(){int s=0,w=1;char c=getchar();while(!isdigit(c)){if(c=='-') w=-1;c=getchar();}while(isdigit(c)){s=(s<<1)+(s<<3)+(c^48);c=getchar();}return s*w;} const int N=105; int n,k,tot,ans; bool vis[N]; vector <int> a[N]; queue <int> q; inline void bfs(int i){ q.push(i); vis[i]=1; int family=1;//初始化,一开始结点 i 独自一个家庭。 while(!q.empty()){ int now=q.front(); q.pop(); int len=a[now].size(); rep(j,0,len-1) if(!vis[a[now][j]]){//没有访问过。 ++family;//累加家庭人员数量。 vis[a[now][j]]=1; q.push(a[now][j]);//入队。 } } ans=max(ans,family); } signed main(){ n=read();k=read(); rep(i,1,k){ int u=read(),v=read(); a[u].push_back(v); a[v].push_back(u); } rep(i,1,n) if(!vis[i]){ ++tot; bfs(i); } pr("%lld %lld\n",tot,ans); return 0; }
- 1
정보
- ID
- 72
- 시간
- 1000ms
- 메모리
- 256MiB
- 난이도
- 2
- 태그
- (N/A)
- 제출 기록
- 110
- 맞았습니다.
- 70
- 아이디