I need to convert this C++ code to C. Chegg experts answered this question twice
ID: 3875425 • Letter: I
Question
I need to convert this C++ code to C. Chegg experts answered this question twice wrong so far and they only copy pasted my code for some reason (I filed a complaint)
This is the code. I will repeat I NEED IT IN C RIGHT NOW ITS C++
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
const int Maxn = 200;
vector<int> d[Maxn];
int par[Maxn];
int rev[Maxn];
bool vs[Maxn];
bool dfs(int i) {
if (i < 0) return true;
if (vs[i]) return false;
vs[i] = true;
for (int u : d[i])
if (dfs(rev[u])) {
par[i] = u;
rev[u] = i;
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
string s;
getline(cin, s);
for (int i = 0; i < n; i++) {
getline(cin, s);
stringstream ss(s);
vector<int> mk(n, 1);
mk[i] = 0;
int x;
while (ss >> x)
mk[x] = 0;
cout << "value of x is" << x;
for (int x = 0; x < n; x++)
if (mk[x])
d[i].push_back(x);
}
memset(par, -1, sizeof par);
memset(rev, -1, sizeof rev);
for (bool ok = true; ok; ) {
ok = false;
memset(vs, 0, sizeof vs);
for (int i = 0; i < n; i++)
if (par[i] < 0) {
ok |= dfs(i);
}
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (par[i] < 0);
cout << ans;
system("pause");
}
Explanation / Answer
#include<string.h>
#include<stdio.h>
#define Maxn 200
int d[Maxn][Maxn];
int par[Maxn];
int rev[Maxn];
int vs[Maxn];
int sz[Maxn];
int dfs(int i) {
if (i < 0) return 1;
if (vs[i]) return 0;
vs[i] = 1;
for (int ind=0;ind<sz[i];ind++){
int u = d[i][ind];
if (dfs(rev[u])) {
par[i] = u;
rev[u] = i;
return 1;
}
}
return 0;
}
int main() {
int n;
scanf("%d",&n);
for (int i = 0; i < n; i++) {
int mk[n];
for(int ind=0;ind<n;ind++)
mk[ind]=1;
mk[i] = 0;
char temp;
int x;
do{
scanf("%d%c", &x, &temp);
mk[x]=0;
} while(temp!= ' ');
/*
printf("value of x is %d",x);*/
int ind=0;
for (int x = 0; x < n; x++)
if (mk[x])
d[i][ind++] = x;
sz[i] = ind;
}
memset(par, -1, sizeof par);
memset(rev, -1, sizeof rev);
for (int ok = 1; ok; ) {
ok = 0;
memset(vs, 0, sizeof vs);
for (int i = 0; i < n; i++)
if (par[i] < 0) {
ok |= dfs(i);
}
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (par[i] < 0);
printf("%d",ans);
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.