标签: 交互题

  • UOJ461 新年的Dog划分

    首先考虑搞出来原图的一棵生成树,直接把这棵生成树二分图染色即可。

    一个比较显然的方法是在边集中二分,把所有的边依次排开,假设当前位于边x,在保留已经确定了的生成树上的边的情况下,删掉x之前的所有边,然后看原图是否联通。

    使得x尽可能靠右即可。

    这样一定可以搞出来n-1条边。

    如何判断奇环?

    把树二分图染色后,枚举一条树边,然后删掉这条树边和所有二分图中连接两个点集的边,如果此时图仍然联通说明存在奇环。

    这样的询问次数大概是$(n-1)log(n^2)=O(2nlogn)$的,过不去。

    然后我们尝试考虑对于点进行二分。

    从1号点开始BFS。

    假设当前点为vtx,没访问过的并且和vtx不在一条树边上的点v纳入二分范围。

    设这个点集为S。

    如果所有的边$(vtx,v)(v\in S)$全部删除后原图仍然联通,那么就直接把这些边纳入删除集合,然后队列弹出点vtx。

    否则二分一个位置x,使得位置在x之前的所有边删掉后都不会使得图不连通,但是删掉x后图就不连通。

    此时把x之前的所有边纳入删除集合,$(vtx,x)$纳入树边。

    然后接着二分x后面的点即可(实际实现就是队列不弹出点vtx)。

    询问次数在$O(nlogn)$级别的。

    #include <assert.h>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <utility>
    #include <vector>
    #include "graph.h"
    using int_t = int;
    using pair_t = std::pair<int_t, int_t>;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 200;
    
    std::vector<pair_t> result;
    std::vector<pair_t> removes;
    bool onTree[LARGE + 1][LARGE + 1];
    bool removed[LARGE + 1][LARGE + 1];
    int_t n;
    //删除已知的待删除边和vtx编号在id之前的所有边是否联通
    bool myCheck(int_t vtx, const std::vector<int_t>& to, int_t id) {
        std::vector<std::pair<int, int>> next = removes;
        for (int_t i = 0; i <= id; i++) {
            if (onTree[vtx][to[i]] == false) next.push_back(pair_t(vtx, to[i]));
        }
        for (auto& p : next) {
            p.first -= 1;
            p.second -= 1;
            assert(p.first != p.second);
        }
        bool result = query(next);
        return result;
    }
    void BFS() {
        static bool inq[LARGE + 1];
        std::queue<int_t> queue;
        queue.push(1);
        inq[1] = true;
        while (queue.empty() == false) {
            int_t front = queue.front();
    
            std::vector<int_t> to;
            for (int_t i = 1; i <= n; i++) {
                if (i != front && inq[i] == false && removed[front][i] == false)
                    to.push_back(i);
            }
            int_t left = 0, right = to.size() - 1;
            int_t result = to.size();
            //所有边保留仍然联通,直接标记成废边
            if (myCheck(front, to, right)) {
                for (int_t x : to) {
                    removed[x][front] = removed[front][x] = true;
                    removes.push_back(pair_t(x, front));
                }
            } else {
                while (left <= right) {
                    int_t mid = (left + right) / 2;
                    if (!myCheck(front, to, mid)) {
                        right = mid - 1;
                        result = std::min(result, mid);
                    } else {
                        left = mid + 1;
                    }
                }
                for (int_t i = 0; i < result; i++) {
                    removed[to[i]][front] = removed[front][to[i]] = true;
                    removes.push_back(pair_t(to[i], front));
                }
            }
            if (result != to.size()) {
                onTree[front][to[result]] = onTree[to[result]][front] = true;
                queue.push(to[result]);
                inq[to[result]] = true;
            } else {
                queue.pop();
            }
        }
    }
    std::vector<int_t> graph[LARGE + 1];
    int_t color[LARGE + 1];
    void DFS(int_t vtx, int_t from = -1) {
        for (int_t to : graph[vtx]) {
            if (to != from) {
                color[to] = color[vtx] ^ 1;
                DFS(to, vtx);
            }
        }
    }
    bool removeCheck(pair_t edge) {
        std::vector<pair_t> next;
        for (int_t i = 1; i <= n; i++) {
            if (color[i] == 0) {
                for (int_t j = 1; j <= n; j++) {
                    if (color[j] == 1 && onTree[i][j] == false) {
                        next.push_back(pair_t(i, j));
                    }
                }
            }
        }
        next.push_back(edge);
        for (auto& p : next) {
            p.first -= 1;
            p.second -= 1;
    
            assert(p.first != p.second);
        }
        bool result = query(next);
        return result;
    }
    std::vector<int> check_bipartite(int vsize) {
        ::n = vsize;
        BFS();
        std::vector<pair_t> treeEdges;
        for (int_t i = 1; i <= n; i++) {
            for (int_t j = i + 1; j <= n; j++) {
                if (onTree[i][j]) {
                    graph[i].push_back(j);
                    graph[j].push_back(i);
                    treeEdges.push_back(pair_t(i, j));
                }
            }
        }
        DFS(1);
        for (const auto& edge : treeEdges) {
            if (removeCheck(edge)) {
                return std::vector<int_t>();
            }
        }
        std::vector<int_t> result;
        for (int_t i = 1; i <= n; i++)
            if (color[i] == 0) result.push_back(i - 1);
        return result;
    }

     

  • 雅礼集训2019-3-18 Day2T1 图

    题意

    本题为交互题。有一个n个点的完全图,n个点顺时针均匀分布在一个圆上,每次可以询问两点间的边的颜色(黑色或白色),要求以不超过2n的询问次数求一棵原图的生成树,满足生成树上的边均为同一种颜色并且任意两条边之间的交点要么不存在,要么在其中一条线段的一个端点上。

    点数不超过1000。

    题解

    首先考虑求出来凸多边形最外层的边的颜色,然后找一个点$i$,满足$(i-1,i)$和$(i,i+1)$的颜色不同,然后就可以划分成另一个子问题:把点i去掉后,连接边(i-1,i+1),然后继续处理去掉i之后的多边形。每次递归返回生成树的颜色,根据生成树的颜色决定点i应该向i-1连边还是i+1连边。

    边界条件是只剩下两个点(一条边),或者剩下所有边的颜色都一样(删掉一条即可)。

    #include <assert.h>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <utility>
    #include <vector>
    #include "gra.hpp"
    using int_t = long long int;
    using pair_t = std::pair<int_t, int_t>;
    
    using std::cin;
    using std::cout;
    using std::endl;
    const int_t LARGE = 1e3;
    int_t color[LARGE + 1][LARGE + 1];
    std::vector<pair_t> result;
    int_t n;
    //#define DEBUG
    bool process(const std::vector<int_t>& vec) {
    
        int_t n = vec.size();
        if (n == 2) {
            report(vec[0], vec[1]);
            return color[vec[0]][vec[1]];
        }
        bool flag = false;
        for (int_t i = 0; i < vec.size(); i++) {
            if (color[vec[(i - 1 + n) % n]][vec[i]] !=
                color[vec[i]][vec[(i + 1) % n]]) {
                std::vector<int_t> next = vec;
                next.erase(next.begin() + i);
                if (n >= 3)
                    color[vec[(i - 1 + n) % n]][vec[(i + 1 + n) % n]] =
                        color[vec[(i + 1 + n) % n]][vec[(i - 1 + n) % n]] =
                            query(vec[(i - 1 + n) % n], vec[(i + 1 + n) % n]);
                int_t color = process(next);
                if (::color[vec[(i - 1 + n) % n]][vec[i]] == color) {
                    report(vec[(i - 1 + n) % n], vec[i]);
                } else {
                    report(vec[i], vec[(i + 1 + n) % n]);
                }
                flag = true;
                return color;
            }
        }
        if (!flag) {
            for (int_t i = 0; i < n - 1; i++) {
                report(vec[i], vec[(i + 1) % n]);
            }
            return color[vec[0]][vec[(1) % n]];
        }
    }
    void tree(int n) {
        ::n = n;
        std::vector<int_t> vec;
        for (int_t i = 1; i <= n; i++) vec.push_back(i);
        for (int_t i = 0; i < n; i++) {
            color[vec[(i + 1) % n]][vec[i]] = color[vec[i]][vec[(i + 1) % n]] =
                query(vec[i], vec[(i + 1) % n]);
        }
        process(vec);
    }