本文主要是介绍字节跳动正式批第二场笔试题2019.8.25,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一题
并查集
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 205;
int root[maxn];
int find(int x){if(x == root[x]) return x;int t = find(root[x]);root[x] = t;return t;
}
int main(){int n, x;cin>>n;for(int i = 0; i < maxn; i++){root[i] = i;}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){cin >> x;if(x >= 3){int a = find(i);int b = find(j);if(a != b){root[b] = a;}}}}int cnt = 0;for(int i = 0; i < n; i++){ if(root[i]==i) cnt++;}cout<<cnt<<endl;return 0 ;
}
这篇关于字节跳动正式批第二场笔试题2019.8.25的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!