并查集 + set 题解 | #第一题#

第一题

https://www.nowcoder.com/practice/7c29cdfa28274c86afc9e88c07448a10

#include <iostream>
#include <set>
using namespace std;
const int N = 1e6+10;
int p[N];
int find(int x){
    if(x != p[x]) p[x] = find(p[x]);
    return p[x];
}
int main(){
    int a, b;
    set<int> s, cnt;
   for(int i = 1; i <= N; i ++) p[i] = i;
    while(cin>>a>>b){
        p[find(a)] = find(b);
        cnt.insert(a);
        cnt.insert(b);
    } 
    for(auto i : cnt){
        s.insert(find(i));
    }
    cout<<s.size();
    return 0;
}

全部评论

相关推荐

也许是天气_:放在双9简历池里也是中上简历,没人指导一是看不懂,二是可能在偷偷抄你简历亮点。如果大厂0面,多半是卡学历了。我觉得你这情况不愁offer,找工作跟投资一样的,90%的时间都是在等待。
点赞 评论 收藏
分享
Hyh_111:像这种hr就不用管了,基本没啥实力,换一个吧
点赞 评论 收藏
分享
感觉初筛都过不去,但是没挂我,我就先等着吧
投递华为技术有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务