Codeforces 841D Leha and another game about graph(dfs)
来源:互联网 发布:照相变萌软件 编辑:程序博客网 时间:2024/06/05 07:37
题目Codeforces 841D
题意:给定一n个结点m条边的连通图,第i个结点有值di,di为0、1、-1其中之一。让选定边的子集,使选择后图中每个结点满足:若di≠-1,则di等于结点度数mod2,给出任一种选法。
思路:考虑全部边都不选的情况,此时di=0和di=-1的结点满足条件。为满足di=1的结点,需向其它结点连接一条边,另一结点di取反。将其它结点看作此结点的父亲,则总可以改变儿子与父亲之间边的状态来满足儿子结点的di值,可利用dfs使除根以外的所有结点满足要求。最后判断根节点,若di=1,则需要从任意di=-1的结点中选一个,其与根节点之间的路径全部取反,此时除根节点与这一节点以外,中途任何节点度数mod2均不受影响,可满足要求。若没有di=-1的子结点,则无解。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <cmath>#include <vector>#include <iostream>#include <stack>#include <set>#include <map>using namespace std;const int MAX=4e5+5;int n,m,ans;int d[MAX],vis[MAX],select[MAX]; pair<int,int> f[MAX];vector<pair<int,int>> E[MAX];void dfs(int u,int fa){int i;int v,id;for(i=0;i<E[u].size();i++){v=E[u][i].first;id=E[u][i].second;if(v==fa||vis[v]) continue;vis[v]=1;f[v]=make_pair(u,id);dfs(v,u);if(d[v]==1){d[v]=0;select[id]^=1;if(d[u]!=-1) d[u]^=1;}}}int main(){int i;int u,v,node=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){scanf("%d",&d[i]);if(d[i]==-1) node=i;}for(i=1;i<=m;i++){scanf("%d%d",&u,&v);E[u].push_back(make_pair(v,i));E[v].push_back(make_pair(u,i));}dfs(1,0);if(d[1]==1){if(node==0){printf("-1\n");return 0;}while(node!=1){select[f[node].second]^=1;node=f[node].first;}}for(i=1;i<=m;i++) if(select[i]) ans++;printf("%d\n",ans);for(i=0;i<=m;i++) if(select[i]) printf("%d%c",i,i==m?'\n':' '); return 0;}
阅读全文
0 0
- Codeforces 841D Leha and another game about graph(dfs)
- codeforces 841D Leha and another game about graph
- Codeforces 841D Leha and another game about graph 构造
- Codeforces Round #429 (Div. 1):B. Leha and another game about graph(DFS)
- codeforces 840B. Leha and another game about graph(构造,dfs)
- Codeforces 840B:Leha and another game about graph
- Codeforces Round #429 (Div. 2) D. Leha and another game about graph
- codeforces Div.2 #840D Leha and another game about graph 顶点权值为度数对应奇偶
- Codeforces 366D Dima and Trap Graph【二分+Dfs】
- codeforces 841C Leha and Function
- Leha and Function CodeForces
- Leha and Function CodeForces
- Another Graph Game
- Codeforces 246D Colorful Graph 【DFS】
- Codeforces 263D Cycle in Graph【Dfs】
- CodeForces 841C (C) Leha and Function 贪心
- Codeforces 366D Dima and Trap Graph (DFS剪枝水过...)
- CodeForces 366D Dima and Trap Graph
- NET::ERR_CACHE_MISS的错误解决
- 自定义气泡,适配项目中的所有气泡
- mysql安装出现Errcode: 2
- 作业5.5
- hdu 5919 主席树(区间不同数个数+区间第k个数)
- Codeforces 841D Leha and another game about graph(dfs)
- hdoj 1754 I Hate It
- 获取浏览器地址栏ip
- CentOS安装PHP
- Kotlin的语法糖 thread { } 会运行两次的问题【重大BUG预警!】
- Qt中layout()->setSizeConstraint(QLayout::SetFixedSize);崩溃的问题
- 7.4
- C++类构造函数初始化列表详解
- 算法提高 林丹大战李宗伟