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,输出任意方案. [算法]数学+搜索 [题解] 最关 ...
随机推荐
- DCNN models
r egion based RNN Fast RCNN Faster RCNN F-RCN Faster RCNN the first five layers is same as the ZF ne ...
- Linux - 日志处理一
Linux 日志处理 history # 历时命令默认1000条 HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " # 让history命令显示具体时间 hi ...
- pythonic语法
b="$".join(str(x) for x in range(10)) a= 2 if 5<2 else 3 print (a)#a是3
- 虚拟树研究-CheckBox初步判断只能在第一列
//虚拟树研究-CheckBox初步判断只能在第一列 procedure TWindowsXPForm.XPTreeInitNode(Sender: TBaseVirtualTree; ParentN ...
- Service的线程、工作线程、权限及系统Service
Service组件和其他组件一样,都是运行于应用的主线程当中,它们都运行于同一个单一的线程中. 可以把Service简单的理解成一个没有界面显示的Activity(这个比喻其实并不准确,因为Servi ...
- android fragment解析
1.fragment加载到Activity (1).添加fragment到Activity的布局文件 (2).动态在activity中添加fragment 例子: // 步骤1:获取FragmentM ...
- c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage
vs 2005 ,vs 2008, vs 2010,安装后有时出现这个错误(我的机器装的x64的win7),很烦人.找了很多地方都不能解决.其实说起来还是开发国家牛,轻易就解决了这个问题.其实出现这个 ...
- Device Tree常用方法解析【转】
转自:https://blog.csdn.net/airk000/article/details/21345159 Device Tree常用方法解析 Device Tree在Linux内核驱动中的使 ...
- linux 查看用户上次修改密码的日期【转】
1.找到以下文件: cat /etc/shadow 第三段字符就是最近一次密码修改的天数,此数字是距离1970年1月1日的天数. 2.用以下命令计算: date -u -d "1970- ...
- Lucas卢卡斯定理
当$p$为素数时 $$C_n^m\equiv C_{n/p}^{m/p}*C_{n\%p}^{m\%p}(mod\ p)$$ 设$n=s*p+q,m\equiv t*p+r(q,r<=p)$ 我 ...