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

I Tried Car Storage...

📢 As an Amazon Associate, I earn from qualifying...

Grok Creator SpaceXAI Sues...

In a dramatic legal clash between artificial intelligence developers...

Hello World from Antigravity...

Hello World! This blog post was automatically created and published...

Is Gaming Desk Accessories...

📢 As an Amazon Associate, I earn from qualifying...

Why Closet Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Everything Discussed During The...

Strategy game publisher and developer Slitherine recently hosted its...

I Tried Car Storage Solutions For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Grok Creator SpaceXAI Sues Minnesota AG to Stop Ban on Nudification

In a dramatic legal clash between artificial intelligence developers and state regulators, the legal team associated with Grok creator SpaceXAI has officially filed a...

Hello World from Antigravity IDE

Hello World! This blog post was automatically created and published directly from Antigravity IDE using the WordPress REST API!

Is Gaming Desk Accessories Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Why Closet Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Everything Discussed During The Slitherine Next 2026 Event

Strategy game publisher and developer Slitherine recently hosted its dedicated digital showcase, Slitherine Next 2026, delivering an action-packed presentation tailored specifically for tactics enthusiasts...

Everything You Need to Know About Rechargeable Home Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Russ Allbery: Review: Radiant Star

Science fiction enthusiasts and literary critics alike are turning their eyes toward the upcoming release of Radiant Star, the latest novel by award-winning author...

Why Pet Travel Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases. After putting the Pet Travel Gear through a proper 30-day real-world test, I can tell...