zoukankan      html  css  js  c++  java
  • L2-010 排座位 (并查集)

    这里唯一需要注意的是,各个输出的条件在题目中有点描述模糊。

    是朋友关系,(不管是不是间接朋友关系)

    既不是朋友也不是敌人(这里不用管是不是间接朋友)

    是敌人关系,同时是间接朋友关系

    是单纯的敌人关系,(表示,不是间接朋友关系)

    #include<iostream>
    using namespace std;
    const int maxn = 105;
    int dim[maxn][maxn];
    int fa[maxn];
    int n, m, k, x, y, w;
    
    int find(int x){
        if (fa[x] == x)return x;
        return fa[x] = find(fa[x]);
    }
    
    int main(){
        cin >> n >> m >> k;
        for (int i = 1; i <= n; ++i)fa[i] = i;
        while (m--){
            cin >> x >> y >> w;
            dim[x][y] = dim[y][x] = w;
            if (w == 1){
                int x1 = find(x);
                int y1 = find(y);
                if (x1 != y1){ fa[x1] = y1; }        //链接
            }
        }
        while (k--){
            cin >> x >> y;
            if (dim[x][y] == 1)cout << "No problem" << endl;
            else if (dim[x][y] != 1 && dim[x][y] != -1)cout << "OK" << endl;
            else if (dim[x][y] == -1 && find(x) == find(y))cout << "OK but..." << endl;
            else if (dim[x][y] == -1 && find(x) != find(y))cout << "No way" << endl;
    
        }
    }
  • 相关阅读:
    大神总结的
    更改Xcode的缺省公司名
    iPhone分辨率
    iOS 的 APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸?
    storyBoard(tableViewl)
    storyBoard
    XIB可视化编程
    UITableView(五)
    UITableView(四)
    UITableView(三)
  • 原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/10581197.html
Copyright © 2011-2022 走看看