1118. Birds in Forest (25)

简介: #include #include #include #include #include using namespace std;int father[10001];int to[10001];int fi...
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
using namespace std;
int father[10001];
int to[10001];

int find(int x){
    while (x != father[x]) {
        x = father[x];
    }
    return x;
}
void Union(int a, int b){
    a = find(a);
    b = find(b);
    if(a < b) father[b] = a;
    else father[a] = b;
}

int main(){
    int n;
    cin >> n;
    map<int, bool> ma;
    vector<int> v;
    for (int i = 0; i < 10001; i++) {
        father[i] = i;
    }
    for (int i = 1; i <= n; i++) {
        int k;
        cin >> k;
        for (int j = 0; j < k; j++) {
            int t;
            cin >> t;
            if(!ma[t]){
                v.push_back(t);
                ma[t] = true;
                to[t] = i;//每一只鸟在那棵树上
            }
            Union(i, find(to[t]));//将两棵树联系在一起
        }
    }
    int cnt = 0;
    map<int, bool> mb;
    for (int i = 0; i < v.size(); i++) {
        if(find(to[v[i]]) && !mb[find(to[v[i]])]){
            mb[find(to[v[i]])] = true;
            cnt++;
        }
    }
    cout << cnt << ' ' << v.size() << endl;

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        if(find(to[a]) == find(to[b])){
            cout << "Yes\n";
        }else{
            cout << "No\n";
        }
    }
    return 0;
}
目录
相关文章
|
5月前
|
机器学习/深度学习 数据采集 算法
随机森林(Random Forest)
随机森林(Random Forest)是一种集成学习方法,它通过构建多个决策树并将它们的预测结果综合起来来提高模型的预测性能。随机森林是一种非常强大的机器学习算法,可以用于分类和回归问题。它具有较高的准确性和鲁棒性,能够处理大规模数据集和缺失数据,并且不容易过拟合。
110 2
|
机器学习/深度学习 数据采集
2D Logistic Regression
2D Logistic Regression 是一种用于解决二分类问题的机器学习模型,它是 Logistic Regression 在多维空间中的扩展。在 2D Logistic Regression 中,我们使用一个二维平面(或多维空间中的超平面)来将不同类别的数据分开。
79 1
|
机器学习/深度学习 数据采集
Logistic Regression
机器学习中的逻辑回归(Logistic Regression)是一种用于解决分类问题的线性模型。它通过拟合一条直线(或平面),将输入变量与输出变量(通常为二值变量,如 0 或 1)之间的关系表示出来。
58 0
|
机器学习/深度学习 算法 Python
MachineLearning---Naive Bayes
MachineLearning---Naive Bayes
75 0
|
机器学习/深度学习 数据采集 运维
Random Forest
首届世界科学智能大赛:生命科学赛道——生物学年龄评价与老年病风险预测
121 0
|
机器学习/深度学习 移动开发 算法
随机森林 Random Forest
随机森林 Random Forest
|
运维 安全 数据挖掘
Outlier and Outlier Analysis|学习笔记
快速学习 Outlier and Outlier Analysis
Outlier and Outlier Analysis|学习笔记
|
机器学习/深度学习 人工智能 移动开发
Logistic Regression with a Neural Network mindset
数据集是两个 .h5 格式的文件,有训练集和测试集,分别有209和50张图片,大小为(64, 64 ,3),reshape 成(12288, 209)和(12288, 50)。
130 0
|
机器学习/深度学习
COVID-19 Cases Prediction (Regression)(一)
COVID-19 Cases Prediction (Regression)
514 0
COVID-19 Cases Prediction (Regression)(一)