首页 技术 正文
技术 2022年11月15日
0 收藏 636 点赞 2,741 浏览 857 个字

链接

大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值

首先要注意到该题只需要考虑dfs树即可, 因为反向边一定不会产生贡献

存在权值为-1的点, 则直接以权值为-1的点为根dfs

若无权值为-1的点, 则答案不一定存在, 任选一个点为根dfs即可

#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define x first
#define y second
#define pb push_back
using namespace std;
typedef pair<int,int> pii;const int N = 4e5+10, INF = 0x3f3f3f3f;
int a[N], b[N], c[N], vis[N], f[N], n, m, k, t;
vector<pii> g[N];void dfs(int x) {
if (vis[x]) return;
vis[x]=1;
for (auto e:g[x]) {
dfs(e.x);
if (a[e.x]==1) a[e.x]=0,f[e.y]^=1,a[x]^=1;
}
}int main() {
scanf("%d%d", &n, &m);
int rt = 1;
REP(i,1,n) scanf("%d", a+i),a[i]==-1?rt=i:0;
REP(i,1,m) {
int u, v;
scanf("%d%d",&u,&v);
g[u].pb({v,i}),g[v].pb({u,i});
}
dfs(rt);
if (a[rt]==1) return puts("-1"),0;
int cnt = 0;
REP(i,1,m) cnt += f[i];
printf("%d\n", cnt);
REP(i,1,m) if (f[i]) printf("%d ",i);
puts("");
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902