https://i0.wp.com/eblogarithm.com/wp-content/uploads/2017/08/Codeforces-Round-429-Div-2-problem-D-Leha-and-another-game-about-graph-Solution-In-CC1.png?fit=640%2C360

Codeforces Round #429 (Div. 2), problem: (D) Leha and another game about graph Solution In C/C++

#include<cstdio>
using namespace std;
const int MAXN = 300000+5;
const int MAXE = MAXN*2;
int to[MAXE], nxt[MAXE], head[MAXN], ne;
void addEdge(int f, int t) {
to[++ne] = t;
nxt[ne] = head[f];
head[f] = ne;
}
int n, m;
int d[MAXN];
bool vis[MAXN];
bool mark[MAXE];
int ednum;
int dfs(int u) {
vis[u] = true;
int tot = (d[u] == 1);
for (int i = head[u]; i; i = nxt[i])
{
if (vis[to[i]]) continue;
int v = to[i];
int tmp = dfs(v);
if (tmp & 1) mark[(i+1)>>1] = true, ednum ++;
tot += tmp;
}
return tot;
}
int main()
{
scanf(“%d%d”, &n, &m);
int num = 0;
for (int i = 1; i<=n; i++)
{
scanf(“%d”, &d[i]);
if (d[i] == 1) num++;
}
bool flag = false;
if (num & 1) {
for (int i = 1; i<=n; i++) {
if (d[i] == -1 && !flag) {
d[i] = 1; flag = true;
}else if (d[i] == -1) d[i] = 0;
}
if (!flag) {
puts(“-1”);
return 0;
}
}else {
for (int i = 1; i<=n; i++) if (d[i] == -1) d[i] = 0;
}
for (int i = 1; i<=m; i++) {
int u, v; scanf(“%d%d”, &u, &v);
addEdge(u, v); addEdge(v, u);
}
dfs(1);
printf(“%d\n”, ednum);
for (int i = 1; i <= m; i++) {
if (mark[i]) printf(“%d\n”, i);
}
return 0;
}

(Visited 33 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: