【Codeforces Round #429 (Div. 1) B】Leha and another game about graph
【链接】点击打开链接
【题意】
【题解】
如果d[cur]==0,则跳过cur和parent[cur]之间的边.不做处理,则度数之和不变还是偶数.
如果d[cur]==1,则把cur和parent[cur]之间的边加到ans里面去,同时把d[parent[cur]]取反,也即0变成1,1变成0,然后d[cur]变成0.这样做,我们把d[cur]的奇偶性变了一下,然后d[parent[cur]]的奇偶性也变了.则度数之和的奇偶性还是不变,仍然为偶数。同时d[cur]变为0,表示cur这个点不需要边了,然后d[cur的父节点]的奇偶性改变的话,表示cur的父亲节点需不需要边的状态也改变了。
【错的次数】
【反思】
【代码】
/* */
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <cstdlib>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ld long double
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rf(x) scnaf("%lf",&x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
#define sz(x) ((int) x.size())
#define ld long double typedef pair<int, int> pii;
typedef pair<LL, LL> pll; //mt19937 myrand(time(0));
//int get_rand(int n){return myrand()%n + 1;}
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 3e5; int n, m,d[N+10],cnt1,cnt0;
bool bo[N + 10];
vector <pii> g[N + 10];
vector <int> ans; void dfs(int x) {
bo[x] = true;
for (pii temp : g[x]) {
int y = temp.first, id = temp.second;
if (!bo[y]) {
dfs(y);
if (d[y] == 1) {
d[x] ^= 1;
d[y] = 0;
ans.pb(id);
}
}
}
} int main() {
//Open();
//Close();
ri(n), ri(m);
rep1(i, 1, n) {
ri(d[i]);
if (d[i] == 1) cnt1++;
if (d[i] == -1) cnt0++;
}
rep1(i, 1, m) {
int x, y;
ri(x), ri(y);
g[x].pb(mp(y,i)), g[y].pb(mp(x,i));
}
if ((cnt1 & 1) && cnt0 == 0) {
puts("-1");
return 0;
}
cnt1 = cnt1 & 1;
rep1(i,1,n)
if (d[i] == -1) {
d[i] = cnt1;
cnt1 = 0;
}
dfs(1);
oi(sz(ans)); puts("");
for (int x : ans)
oi(x), puts("");
return 0;
}
【Codeforces Round #429 (Div. 1) B】Leha and another game about graph的更多相关文章
- 【Codeforces Round #429 (Div. 2) C】Leha and Function
[Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
- 【Codeforces Round #429 (Div. 2) B】 Godsend
[Link]:http://codeforces.com/contest/841/problem/B [Description] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
- 【Codeforces Round #423 (Div. 2) B】Black Square
[Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...
随机推荐
- 最新华为数通HCNP-随堂培训视频课程 大牛讲解高清带实验
2017年最新华为数通HCNP-随堂培训视频课程 大牛讲解高清带实验 2017最新的华为中级HCNP培训,数通方向,讲解非常牛高清课程附带实验. 华为数通HCNP-2017年最新随堂培训视频(高清)\ ...
- Django transaction 误用之后遇到的一个问题与解决方法
今天在调试项目开发好的一个模块的时候,发现了一个很诡异的现象,最后追踪发现是因为在项目中事务处理有误所致.这个问题坑了我好一会,所以记录一下,以免再踩坑.下面开始详述. 我们都知道 Django 框架 ...
- cache基本结构
下图为direct mapped set associative fully associative图示 direct mapped,相当于set number为1 fully ...
- 紫书 例题 9-4 UVa 116 ( 字典序递推顺序)
这道题在递推方式和那个数字三角形有一点相像,很容易推出来 但是这道题要求的是字典序,这里就有一个递推顺序的问题 这里用逆推,顺推会很麻烦,为什么呢? 如果顺推的话,最后一行假设有种情况是最小值,那么你 ...
- 今日SGU 5.18
SGU 125 题意:给你一个数组b[i][j],表示i,j的四周有多少个数字大于它的,问你能不能构造出一个a矩形 收获:dfs + 剪枝 一行一行的dfs,然后第一行去枚举0-9,下一行判断当前选 ...
- nginx下修改svn配置
最近公司的SVN服务器地址做了变更,而我用的操作系统是Ubuntu操作系统,我也不想把以前下载的代码重新进行修改,我想通过修改svn地址,应该可以,终于在网上通过查找资料,找到了解决的方法: ...
- OpenCASCADE中散乱Edge生成Wire
OpenCASCADE中散乱Edge生成Wire eryar@163.com Abstract. In OpenCASCADE a wire can be built from any number ...
- RecyclerView实现瀑布流布局
RecyclerView本身提供了三个LayoutManager的实现 LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager ...
- ManagementObjectSearcher 对象获取串口列表
首先,需引用using System.Management; 可先建个枚举类,如下 #region WIN32 API /// <summary> /// 枚举win32 api /// ...
- golang 数组
数组是Go语言编程中最常用的数据结构之一.顾名思义,数组就是指一系列同一类型数据的集合.数组中包含的每个数据被称为数组元素(element),一个数组包含的元素个数被称为数组的长度. 在Go语言中数组 ...