【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两种颜色的格子; 让你在这个格子 ...
随机推荐
- 如何在Ubuntu14.04中安装mysql
接触过MySQL的小伙伴们都知道,在Windows下安装MySQL是一件让人十分头大的事情,但是在Ubuntu等其他Linux系统中安装MySQL就简单很多了,具体的教程如下.1.在Ubuntu的命令 ...
- Linux samba服务器安装调试。
samba 做为主要的windown 和 Linux 通讯的服务器之一. 1. 查看是否安装了smb服务. #rpm -ga|grep samba 或者 #service smb status ...
- 学习TF:《TensorFlow实战》中文版PDF+源代码
深度学习乃至人工智能正逐渐在FinTech领域发挥巨大的作用,其应用包括自动报告生成.金融智能搜索.量化交易和智能投顾.而TensorFlow为金融业方便地使用深度学习提供了可能.<Tensor ...
- QQ空间说说爬虫
QQ空间说说爬虫 闲来无事,写了一个QQ空间的爬虫,主要是爬取以前的说说,然后生成词云. 这次采用的主要模块是selenium,这是一个模拟浏览器的模块,一开始我不想用这个模块写的,但是后面分析的时候 ...
- C# async/await异步编程深入理解
异步函数简介 一般指 async 修饰符声明得.可包含await表达式得方法或匿名函数. 声明方式 异步方法的声明语法与其他方法完全一样, 只是需要包含 async 关键字.async可以出现在返回值 ...
- 9.9递归和动态规划(六)——打印n对括号的所有有效组合(即左右括号正确配对)
/** * 功能:打印n对括号的所有有效组合(即左右括号正确配对). */ 两种方法: 方法一: /** * 思路:在括号的最前面或者原有的每对括号中面插入一对括号. 至于其它任何位置.比方字符串的 ...
- es65 跨模块常量
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 49.大量qq号及qq密码挖掘到结构体并实现精确分配内存以及快速排序
原数据: 处理后的数据: 完整代码: //思路; //数据清洗(把空格替换成'\0') -> 精确获取每一行(要提前获取source的总长度) -> 获取出每一行再精确分割并把分割后的数据 ...
- js闭包注意事项
/关于闭包使用应该注意的 // 1 闭包在父函数每次调用都会产生不同的闭包 // 2 闭包中子函数使用父函数变量的变量不是复制 是引用 // 3 闭包在循环中使用
- 如何使用定时器settimeout、setInterval执行能传递参数的函数(转)
来自: http://www.cnblogs.com/wkylin/archive/2012/09/07/2674911.html var userName="jack";//根据 ...