Codeforces Round #429 (Div. 2) - D Leha and another game about graph
Leha and another game about graph
题目大意:给你一个图,每个节点都有一个v( -1 , 0 ,1)值,要求你选一些边,使v值为1
的点度数为奇数,v值为0的度数为偶数,v值为-1的节点没有限制。让你输出边的集合,
如果不存在这样的边集,输出-1。
写的时候没啥思路,dfs瞎搞了一下过不了。
思路:我们先考虑没有解的情况,如果v值为1的点为奇数个,且没有v为-1的点,则不存在
解,因为你添加一条边改变了两个v值非-1点的奇偶性,又有奇数个v值为1的点,不可能满足
全部点,所以不存在解。然后我们把当前节点只和它的父节点联系在一起,如果当前节点
不满足条件,就加上它和它父节点之间的那条边,这里如果有v位-1的点,我们优先用它作为
根节点,因为子节点必定满足了条件,只要看根节点就行了。
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define pb push_back
using namespace std;
const int N=*1e5+;
int v[N],n,m;
vector<pii> e[N];
vector<int> ans;
bool vis[N],e_vis[N];
void dfs(int u,int e_id)
{
vis[u]=true;
bool flag=false;
int len=e[u].size();
for(int i=;i<e[u].size();i++)
{
pii p=e[u][i];
if(vis[p.fi]) continue;
dfs(p.fi,p.se);
if(e_vis[p.se]) flag=!flag;
}
if(v[u]==- || flag==v[u]) return;
if(e_id!=-)
{
e_vis[e_id]=true;
ans.pb(e_id);
}
}
int main()
{
cin>>n>>m;
int st=-,sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&v[i]);
if(st==- && v[i]==-) st=i;
if(v[i]!=-) sum+=v[i];
}
for(int i=;i<=m;i++)
{
int f,t; scanf("%d%d",&f,&t);
e[f].pb(mk(t,i));
e[t].pb(mk(f,i));
}
//cout<<st<<endl;
if(st==- && sum&)
{
puts("-1");
return ;
}
if(st==-) dfs(,-);
else dfs(st,-);
int len=ans.size();
sort(ans.begin(),ans.end());
printf("%d\n",len);
for(int i=;i<len;i++) printf("%d\n",ans[i]);
return ;
}
Codeforces Round #429 (Div. 2) - D Leha and another game about graph的更多相关文章
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]
PROBLEM A/_ - Generous Kefa 题 OvO http://codeforces.com/contest/841/problem/A cf 841a 解 只要不存在某个字母,它的 ...
- Codeforces Round #429 (Div. 2) 补题
A. Generous Kefa 题意:n个气球分给k个人,问每个人能否拿到的气球都不一样 解法:显然当某种气球的个数大于K的话,就GG了. #include <bits/stdc++.h> ...
- 【Codeforces Round #429 (Div. 2) C】Leha and Function
[Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...
- 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))
[题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...
- 【CodeForces】841D. Leha and another game about graph(Codeforces Round #429 (Div. 2))
[题意]给定n个点和m条无向边(有重边无自环),每个点有权值di=-1,0,1,要求仅保留一些边使得所有点i满足:di=-1或degree%2=di,输出任意方案. [算法]数学+搜索 [题解] 最关 ...
随机推荐
- 使用jQuery实现返回顶部功能
<p id="back-to-top"><a href="#top"><span></span>返回顶部< ...
- Java EE之Struts2路径访问小结
一.项目WEB视图结构 注释:struts.xml:最普通配置,任何无特殊配置 二.访问页面 1.访问root.jsp //方式1: http://localhost/demo/root.jsp // ...
- 顺序列表(栈/队列等)ADT[C++]
#include<iostream> using namespace std; //ADT template<class T> class SeqList{ public: / ...
- CF875D High Cry
传送门 题目要求合法的区间个数,这里考虑用总区间个数减去不合法的个数 假设某个数为区间最大值,那么包含这个数的最长区间内,所有数小于他并且所有数没有这个最大值没有的二进制位,可以按位考虑每个数\(i\ ...
- 在虚拟机安装windows xp时所需要的序列号
最新的windows xp sp3序列号 xp序列号 最新的windows xp sp3序列号(绝对可通过正版验证) MRX3F-47B9T-2487J-KWKMF-RPWBY(工行版) 可用(强推 ...
- 解决:[DCC Fatal Error] **.dpk : E2202 Required package '***' not found
//[DCC Fatal Error] **.dpk : E2202 Required package '***' not found 意思是:[DCC致命错误] *:e2202需包***没有发现 D ...
- ReLu、LeakyRelu、PReLu(转载)
转载链接:http://blog.csdn.net/cham_3/article/details/56049205
- Python3学习笔记23-StringIO和BytesIO
StringIO 很多时候数据读取不一定是文件,也可以在内存中 StringIO顾名思义就是在内存中读写str 要把str写入StringIO,我们需要先创建一个StringIO,然后像文件一样写入即 ...
- 编译时bad substitution的解决办法
由于使用的使用的编译器不同导致, 需要使用shell为 #!/bin/bash 即可.
- 修改centos和ubuntu ssh远程连接端口提升系统安全性
#修改centos服务器ssh端口 sed -i 's/#Port 22/Port 38390/' /etc/ssh/sshd_config sed -i 's/^GSSAPIAuthenticati ...