Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 – Final Round Div. 2 Edition), problem: (C) Pavel and barbecue Solution in C/C++

#include <stdio.h>

#define N 200000

int pp[N], used[N];

void dfs(int i) {
if (used[i])
return;
used[i] = 1;
dfs(pp[i]);
}

int main() {
int n, i, sum, cnt;
static int bb[N];

scanf(“%d”, &n);
for (i = 0; i < n; i++) {
scanf(“%d”, &pp[i]);
pp[i]–;
}
sum = 0;
for (i = 0; i < n; i++) {
scanf(“%d”, &bb[i]);
sum += bb[i];
}
cnt = 0;
for (i = 0; i < n; i++)
if (!used[i]) {
cnt++;
dfs(i);
}
printf(“%d\n”, (cnt == 1 ? 0 : cnt) + (sum % 2 == 0 ? 1 : 0));
return 0;
}

#include <stdio.h>

#define N 200000

int pp[N], used[N];

void dfs(int i) {
if (used[i])
return;
used[i] = 1;
dfs(pp[i]);
}

int main() {
int n, i, sum, cnt;
static int bb[N];

scanf(“%d”, &n);
for (i = 0; i < n; i++) {
scanf(“%d”, &pp[i]);
pp[i]–;
}
sum = 0;
for (i = 0; i < n; i++) {
scanf(“%d”, &bb[i]);
sum += bb[i];
}
cnt = 0;
for (i = 0; i < n; i++)
if (!used[i]) {
cnt++;
dfs(i);
}
printf(“%d\n”, (cnt == 1 ? 0 : cnt) + (sum % 2 == 0 ? 1 : 0));
return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Where the OKC Thunder Rank Among the Top 10 NBA Teams Entering August

As the offseason progresses into August, the NBA landscape continues to undergo dramatic transformations. Major headline-grabbing moves, including LeBron James making his historic transition...

Stop Buying Cheap Alternatives This Portable Blender Bottle Is the Real Deal

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

Famed mountaineer Nirmal Purja, 43, among 10 climbers killed after avalanche in Pakistan

Tragedy in the Karakoram: Nirmal Purja Among 10 Lost in Avalanche In a devastating turn of events for the global mountaineering community, renowned high-altitude climber...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!