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;
}

Kathy Griffin Shares She...

Comedian Kathy Griffin has never been one to shy...

Mikie Sherrill wants Nets...

The political arena and the sports world have collided...

Why Everyone is Obsessed with the Emergency Keychain Power Bank

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Kathy Griffin Shares She Was Hospitalized Over Complications From Colonoscopy

Comedian Kathy Griffin has never been one to shy away from sharing the raw, unfiltered details of her life. In her latest health update,...

Why Everyone is Obsessed with the Mini Handheld Pocket Fan

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Power Scrubber Kit

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Mikie Sherrill wants Nets back in New Jersey, but team ‘perfectly happy in Brooklyn’

The political arena and the sports world have collided once again in the tri-state area. New Jersey's newly elected Governor, Mikie Sherrill, has raised...

Why Everyone is Obsessed with the Earbud Cleaning Pen Tool

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Powerful Electric Air Duster

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

There is a cloud of alcohol drifting through the constellation Aquila that is 1,000 times the diameter of our solar system and contains enough...

Imagine a vast, shimmering cloud floating through the cold expanse of deep space, holding enough alcohol to supply every tavern on Earth for eternity....

Why Everyone is Obsessed with the Wireless Karaoke Microphone

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...