【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两种颜色的格子; 让你在这个格子 ...
随机推荐
- VUE里子组件获取父组件动态变化的值
在VUE里父组件给子组件间使用props方式传递数据,但是希望父组件的一个状态值改变然后子组件也能监听到这个数据的改变来更新子组件的状态. 场景:子组件通过props获取父组件传过来的数据,子组件存在 ...
- 干货分享 -- Math
昼猫笔记 JavaScript -- Math Math也是JS的内置对象,但是它不是一个构造函数,它属于一个工具类不用创建对象,它封装了数学运算相关的属性和方法,今天就来写下常用的函数[API(ap ...
- SYSU 6356 Dispatching
Dispatching Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on SYSU. Original ...
- java种instanceof方法和getclass方法的区别
在比较一个类是否和另一个类属于同一个类实例的时候,我们通常可以采用instanceof和getClass两种方法通过两者是否相等来判断,但是两者在判断上面是有差别的,下面写个测试类. public c ...
- Javascript和jquery事件--事件冒泡和事件捕获
jQuery 是一个 JavaScript 库,jQuery 极大地简化了 JavaScript 编程,在有关jq的描述中,jq是兼容现有的主流浏览器,比如谷歌.火狐,safari等(当然是指较新的版 ...
- sessionStorage的使用方法
本篇是关于sessionStorage的使用方法的介绍,简单几行代码,实现sessionStorage,请大家查阅 (1)在需要设置sessionStorage的页面写如下代码可以存入sessionS ...
- C# WinForm设置透明
1:通过设置窗体的 TransparencyKey实现 例:窗体中的白色会变成透明 this.BackColor =Color.White; this.TransparencyKey = ...
- git把本地文件上传到github上的步骤
1.清除clean 2.返回上一级cd .. 3.克隆仓库地址git clone+地址 4.添加忽悠文件vim .gitignore 5查看cat .gitignore 6.进入到test,并且添加所 ...
- OPENSSL 制作 Ikev2证书
OPENSSL 制作 Ikev2证书 在一个 VPS 上配置 IKEV2 VPN 服务器时,用 OPENSSL 制作了所需的数字证书,奇怪的怎么弄都无法连接服务器,一直提示 "IKE_SA ...
- H5+混合移动app
H5+混合移动app 前言 经过2个多月的艰苦奋斗,app的第一个版本已经快完工了,期间遇到了太多的坑,作为一个喜欢分享的人,我当然不会吝啬分享这爬坑历程.不要问我有多坑,我会告诉你很多,很多.... ...