京东c++ 2AC代码(并查集+暴力)

第一题用并查集解决

class UnionFind
{
private:
    vector<int> parent;
    vector<int> sz;
    int count;

    int find(int p)
    {
        while (p != parent[p])
            p = parent[p];
        return p;
    }

public:
    UnionFind(int count)
    {
        parent.resize(count);
        sz.resize(count);
        this->count = count;
        for (int i = 0; i < count; i++)
        {
            parent[i] = i;
            sz[i] = 1;
        }
    }

    ~UnionFind()
    {
    }


    bool isConnected(int p, int q)
    {
        return find(p) == find(q);
    }

    void unionElements(int p, int q)
    {
        int pRoot = find(p);
        int qRoot = find(q);

        if (pRoot == qRoot)
            return;

        if (sz[pRoot] < sz[qRoot])
        {
            parent[pRoot] = qRoot;
            sz[qRoot] += sz[pRoot];
        }
        else
        {
            parent[qRoot] = pRoot;
            sz[pRoot] += sz[qRoot];
        }
    }
};


int main()
{
    int t;
    cin >> t;

    for (int i = 0; i < t; i++)
    {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> g(n, vector<int>(n, 0));
        for (int j = 0; j < m; j++)
        {
            int x, y;
            cin >> x >> y;
            g[x - 1][y - 1] = 1;
            g[y - 1][x - 1] = 1;
        }
        UnionFind uf(n + 1);
        bool isBad = false;
        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++)
            {
                if (x != y && x < y)
                {
                    if (g[x][y] == 0)
                    {
                        uf.unionElements(x, y);
                    }
                }
            }
        }
        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++)
            {
                if (x != y && x < y)
                {
                    if (g[x][y] == 1 && uf.isConnected(x, y))
                    {
                        cout << "No" << endl;
                        isBad = true;
                        break;
                    }
                }
            }
            if (isBad) break;
        }
        if (!isBad)
        {
            cout << "Yes" << endl;
        }
    }
    return 0;
}

第二题直接对a排序后遍历

struct Thing
{
    int a;
    int b;
    int c;

    Thing(int x, int y, int z): a(x), b(y), c(z)
    {
    }
};

bool lessc(const Thing& a, const Thing& b)
{
    return a.a < b.a;
}


int main()
{
    int n, count(0);
    cin >> n;
    vector<Thing> things;
    for (int i = 0; i < n; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        things.push_back(Thing(x, y, z));
    }
    sort(things.begin(), things.end(), lessc);

    for (auto it = things.begin(); it != things.end(); ++it)
    {
        for (auto it2 = it + 1; it2 != things.end(); ++it2)
        {
            if (it->a < it2->a && it->b < it2->b && it->c < it2->c)
            {
                count++;
                break;
            }
        }
    }
    cout << count << endl;
}
#京东##笔试题目#
全部评论
求稍微讲讲第一题思路
点赞 回复 分享
发布于 2018-09-09 23:00
大佬求排版稍微好一些。。
点赞 回复 分享
发布于 2018-09-09 21:23

相关推荐

这一集&nbsp;硕士输的很惨
找工作ing10:就是这样不是硕士不愿意脱下长衫,是人家觉得屈才了
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
05-13 02:01
已编辑
惠州学院 前端工程师
安静的少年在求佛:建议把公司名字写到标题。以后有人想搜就能直接搜到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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