CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

Posted nicetomeetu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)相关的知识,希望对你有一定的参考价值。

思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- -

/*
CodeForces 840B - Leha and another game about graph [ 增量构造,树上差分 ]  |  Codeforces Round #429(Div 1)
题意:
	选择一个边集合,满足某些点度数的奇偶性
分析:
	将d = 1的点连成一颗树,不在树上的点都不连边。
	可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求
	即整棵树上至多只有1个点不满足自身要求,就是根节点,此时需要在树中任意位置接入 d=-1 的一个节点
	
	然后研究如何在这棵树上选边,考虑增量法
	每次选择两个d=1的点加入树中,并将这棵树上两点间路径上所有的边选择状态反转
	易证对于新加入的节点满足要求,而路径上原有节点仍满足要求
	最后若只剩一个d=1的节点,则和一个d=-1的节点组成一对
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
struct Edge {
    int v, i;
};
vector<Edge> edge[N];
vector<int> c1, c2;
int n, m;
int d[N];
bool vis[N], flag[N];
vector<int> ans;
void dfs(int u)
{
    vis[u] = 1;
    for (auto& e : edge[u])
    {
        if (vis[e.v]) continue;
        dfs(e.v);
        if (flag[e.v]) ans.push_back(e.i);
        flag[u] ^= flag[e.v];
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &d[i]);
        if (d[i] == 1) c1.push_back(i);
        else if (d[i] == -1) c2.push_back(i);
    }
    for (int i = 1; i <= m; i++)
    {
        int u, v; scanf("%d%d", &u, &v);
        edge[u].push_back(Edge{v, i});
        edge[v].push_back(Edge{u, i});
    }
    if (c1.size()%2 && c2.empty())
    {
        puts("-1"); return 0;
    }
    int k = c1.size()/2;
    for (int i = 0; i < k; i++)
    {
        flag[c1[i]] = flag[c1[k+i]] = 1;
    }
    if (c1.size() % 2)
    {
        flag[c1[c1.size()-1]] = flag[c2[0]] = 1;
    }
    dfs(1);
    printf("%d\\n", ans.size());
    for (auto& x : ans) printf("%d ", x);
    puts("");
}

  

以上是关于CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

Codeforces 794F. Leha and security system 线段树

Codeforces 841D Leha and another game about graph - 差分

[Codeforces 841C]Leha and Function

CodeForces841C. Leha and Function(Codeforces Round #429 (Div. 2))

Codeforces 841 D - Leha and another game about graph