标签: 二分图染色

  • noi.ac 165染色

    一场由于忘记清空数组而引发的血案。

    下次我还是开对象处理多组数据吧

    首先判无解,如果存在任意一个点的度数小于2或者为奇数则无解。

    其他情况,让k成为最大的使得$2^k|gcd(degs)$的k,其中degs是全体点度数的集合。

    然后对原图的边进行交错染色,然后这就确定了第k-1位的取值。

    黑色的边的颜色的第k-1位设定为1,白色的边设定为0.

    然后把两种颜色的边拿出来,递归下去按照相同的方法确定第k-2位。

    # 不要忘记在判无解后清空数组!!!

    #include <assert.h>
    #include <algorithm>
    #include <iostream>
    #include <set>
    #include <unordered_set>
    #include <vector>
    using int_t = long long int;
    using set_t = std::unordered_set<int_t>;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 2e5;
    
    struct Edge {
        int_t to, id, from;
    } edges[LARGE + 1];
    
    std::vector<Edge> graph[LARGE + 1];
    int_t color[LARGE + 1];
    int_t degree[LARGE + 1];
    bool visited[LARGE + 1];
    int_t n, m;
    //从点vtx出发,这一位的掩码为mask,上一条边的颜色为lastcolor
    void DFS(int_t vtx, int_t lastcolor, int_t mask) {
        for (int_t i = graph[vtx].size() - 1; i >= 0 && i < graph[vtx].size();
             i--) {
            if (visited[graph[vtx][i].id] == false) {
                color[graph[vtx][i].id] |= (lastcolor ^ mask);
                visited[graph[vtx][i].id] = true;
                int_t to = graph[vtx][i].to;
                graph[vtx].pop_back();
                DFS(to, lastcolor ^ mask, mask);
            } else {
                graph[vtx].pop_back();
            }
        }
    }
    //当前对第bit位进行染色
    void process(int_t* left, int_t* right, int_t bit) {
        if (bit == -1) return;
        if (left > right) return;
        for (auto ptr = left; ptr <= right; ptr++) {
            const auto& edge = ::edges[*ptr];
            graph[edge.from].push_back(Edge{edge.to, edge.id});
            graph[edge.to].push_back(Edge{edge.from, edge.id});
        }
        for (auto ptr = left; ptr <= right; ptr++) {
            const auto& edge = ::edges[*ptr];
            if (visited[edge.id] == false) DFS(edge.from, 0, 1 << bit);
        }
        static int_t zeros[LARGE + 1], ones[LARGE + 1];
        zeros[0] = ones[0] = 0;
        for (auto ptr = left; ptr <= right; ptr++) {
            const auto& edge = ::edges[*ptr];
            graph[edge.from].clear();
            graph[edge.to].clear();
            if (color[edge.id] & (1 << bit))
                ones[++ones[0]] = *ptr;
            else
                zeros[++zeros[0]] = *ptr;
            visited[edge.id] = false;
        }
    
        std::copy(zeros + 1, zeros + 1 + zeros[0], left);
        auto last = left + zeros[0];
        std::copy(ones + 1, ones + 1 + ones[0], last);
        assert(last - left == right - last + 1);
        process(left, last - 1, bit - 1);
        process(last, right, bit - 1);
    }
    int_t gcd(int_t a, int_t b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    int main() {
        while (true) {
            scanf("%lld%lld", &n, &m);
            if (n == 0) break;
            for (int_t i = 1; i <= m; i++) {
                int_t from, to;
                scanf("%lld%lld", &from, &to);
                degree[from]++, degree[to]++;
                edges[i].to = to;
                edges[i].from = from;
                edges[i].id = i;
            }
            int_t ors = 0;
            for (int_t i = 1; i <= n; i++) ors = gcd(ors, degree[i]);
            int_t k = 0;
            while (ors % 2 == 0) {
                k++;
                ors /= 2;
            }
            for (int_t i = 1; i <= n; i++)
                if ((degree[i] & 1) || degree[i] < 2) k = -1;
            if (m & 1) k = -1;
            if (k == -1) {
                cout << -1 << endl;
                std::fill(degree + 1, degree + 1 + n, 0);
                std::fill(color + 1, color + 1 + m, 0);
                continue;
            }
            static int_t edges[LARGE + 1];
            for (int_t i = 1; i <= m; i++) edges[i] = i;
            process(edges + 1, edges + m, k - 1);
            printf("%lld\n", k);
            for (int_t i = 1; i <= m; i++) printf("%lld ", color[i] + 1);
            std::fill(degree + 1, degree + 1 + n, 0);
            std::fill(color + 1, color + 1 + m, 0);
            printf("\n");
        }
        return 0;
    }

     

  • 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;
    }

     

  • NOIP2008 双栈排序

    考虑什么情况下,两个数$a_i$和$a_j$不能放在同一个栈里。

    如果$i<j~,~a_i<a_j~,$并且存在一个$k$,满足$k>j$且$a_k<a_i$,那么$i$和$j$不能放在同一个栈里。

    因为既然要排序,那么$a_k$一定要比$a_i$先出栈,而$a_i$一定要比$

    a_j$先出栈,然而如果$a_k$出栈,那么下一个出栈的只能是$a_j$,显然不能排序。

    所以可以先处理出来哪些数不能放在同一个栈里,然后对于不能放在同一个栈里的数连边,对这张图进行二分图染色。

    染到相同颜色的点放在同一个栈里。

    因为答案要求字典序最小,所以要优先把元素往第一个栈放。

     

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <stack>
    
    using int_t = long long int;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 1000;
    
    int_t seq[LARGE + 1];
    int_t suffix[LARGE + 1];
    int_t n;
    int_t color[LARGE + 1];
    std::vector<int_t> graph[LARGE + 1];
    
    bool DFS(int_t vertex)
    {
        if (color[vertex] == 0)
            color[vertex] = 1;
        for (int_t to : graph[vertex])
        {
            if (color[vertex] + color[to] != 3)
            {
                if (color[to] == 0)
                    color[to] = 3 - color[vertex];
                if (color[to] == color[vertex])
                    return false;
                if (DFS(to) == false)
                    return false;
            }
        }
        return true;
    }
    void pushEdge(int_t from, int_t to)
    {
        graph[from].push_back(to);
        graph[to].push_back(from);
    }
    int main()
    {
        cin >> n;
        for (int_t i = 1; i <= n; i++)
            cin >> seq[i];
        suffix[n] = seq[n];
        for (int_t i = n - 1; i >= 1; i--)
            suffix[i] = std::min(suffix[i + 1], seq[i]);
        for (int_t i = 1; i <= n; i++)
        {
            for (int_t j = i + 1; j < n; j++)
            {
                if (seq[j] > seq[i] && suffix[j + 1] < seq[i])
                {
                    pushEdge(seq[i], seq[j]);
                }
            }
        }
        for (int_t i = 1; i <= n; i++)
        {
            if (color[i] == 0)
            {
                if (DFS(i) == false)
                {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
        std::stack<int_t> stack[3];
        int_t next = 1;
        for (int_t i = 1; i <= n; i++)
        {
            stack[color[seq[i]]].push(seq[i]);
            cout << " ac"[color[seq[i]]] << " ";
            while ((stack[1].empty() == false && stack[1].top() == next) || (stack[2].empty() == false && stack[2].top() == next))
            {
                if (stack[1].empty() == false && stack[1].top() == next)
                {
                    stack[1].pop();
                    cout << "b ";
                }
                else
                {
                    stack[2].pop();
                    cout << "d ";
                }
                next++;
            }
        }
        return 0;
    }