【链接】点击打开链接


【题意】


给出一个连通图,并给每个点赋一个d值0或1或-1,要求选出一个边的集合,使得所有的点i要么d[i] == -1,要么  dgree[i] % 2 == d[i],dgree[i]代表i结点的度数。

【题解】


先统计出所有点所要求的度数的和cnt1。(不考虑-1);
这里的cnt1的奇偶性也就是最后的图所有点的度数和奇偶性.
显然只要是一张图,它的所有的度数的和就必须为偶数
所以,如果cnt1是个奇数,且没有为-1的点,那么直接输出-1就好,无解.
如果有-1的话,那么显然我们是可以把它凑成偶数的。
这样把所有的-1都改成0或1,然后满足n个点的度数和为偶数就好了。
度数和为偶数之后,我们从任意一个点开始进行dfs.
我们可以考虑它任意的一个生成树。(其他地方不加边)
(不加任何边也算是偶数!)
然后从树的叶子节点往上处理。
(我们接下来的任务就是维护所有节点的度数之和总为偶数)
对于当前节点cur.
如果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的父亲节点需不需要边的状态也改变了。
我们最后的目标,显然是让所有的d[cur]都变成0,也即所有的点都不需要边了
而我们的操作能时刻保证,所有的d值的和都为偶数,则根节点进行操作完之后,所有d的值的和仍然为偶数
根节点以下的所有点的值都变成了0,根节点的d值为0或为1,那么也就是说根节点的d值也变成了0
这就说明可以让所有的d值都为0.
输出加入的边就好。

【错的次数】


0

【反思】


图的点的度数之和总是一个偶数的。
用生成树来简化图的问题。
只要度数之和为偶数.(只要求每个点的度数的奇偶性)那么一定有解。

【代码】

/*

*/
#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的更多相关文章

  1. 【Codeforces Round #429 (Div. 2) C】Leha and Function

    [Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...

  2. 【Codeforces Round #429 (Div. 2) A】Generous Kefa

    [Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...

  3. 【Codeforces Round #429 (Div. 2) B】 Godsend

    [Link]:http://codeforces.com/contest/841/problem/B [Description] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...

  4. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  5. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  6. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  7. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  8. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

  9. 【Codeforces Round #423 (Div. 2) B】Black Square

    [Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...

随机推荐

  1. Vim使用心得总结

    基本快捷键 v 进入可视模式 i / a 光标前/后插入模式 I / A 行首/末插入模式 Crtl+c 进入命令模式 Crtl+v 进入块可视模式 Q 进入EX模式 gh 进入选择模式 u 撤销 U ...

  2. 温习 socket http tcp

    Socket是一个接口,可以实现TCP或者UDP的传输HTTP是协议 资料: 1.TCP/IP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接. ...

  3. javaScript for in循环遍历对象

    for循环常被我们用来遍历数组,而如何遍历对象呢? 这时就需要用到for in循环了 写一个遍历对象名简写如下: for(var xxx in ooo){console.log(xxx)} 其中xxx ...

  4. HOJ——T 2275 Number sequence

    http://acm.hit.edu.cn/hoj/problem/view?id=2275 Source : SCU Programming Contest 2006 Final   Time li ...

  5. sqoop 1.4.4-cdh5.1.2高速入门

    一.高速入门 (一)下载安装 1.下载并解压 wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz tar - ...

  6. Python 面向对象 —— super 的使用(Python 2.x vs Python 3.x)

    注意区分当前的 Python 版本是 2.X 还是 3.X,Python 3.X 在 super 的使用上较之 Python 2.X 有较大的变化: 1. Python 2.x class Conta ...

  7. Elasticsearch之源码分析(shard分片规则)

    前期博客是 Elasticsearch之源码编译 (1)elasticsearch在建立索引时,根据id或(id,类型)进行hash,得到hash值之后再与该索引的分片数量取模,取模的值即为存入的分片 ...

  8. Kinect 开发 —— 用户交互设计的若干思考

    Metro 风格 windows 8 Kinect Hub 手势原型设计 悬停选择     翻页控制 关节点重叠的处理方法 将箭靶设置在画面的边缘,这样玩家持弓的角度与屏幕保持一个大约45度的锐角,这 ...

  9. HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others)    Memory Lim ...

  10. 虚拟机上的Ubuntu开机显示“无法应用原保存的显示器配置”

    如图: 解决方法: 删除monitors.xml 文件 rm ~/.config/monitors.xml