showing different output for same input(nearly)












1















Guys I am new to competitive programming ,I am facing a small problem
while giving input
In the question number of vertices are starting from 1 to n
But i write the program considering that nodes are starting from 0



But when i am giving input the test cases by reducing 1 from each vertices for each edge my program is running fine out of the given test cases;



given test case;
1(checking for first one only otherwise 2 was given)
4
1 2
1 3
3 4
2 2
1 2
3 4

my test case(after reducing 1 from edges):
1
4
0 1
0 2
2 3
2 2
0 1
2 3


Link for the question:



https://hackerrank-challenge-pdfs.s3.amazonaws.com/29036-the-story-of-a-tree-English?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1542565481&Signature=WoegY4gKUz0OUDEQ3n2UT80FUc0%3D&response-content-disposition=inline%3B%20filename%3Dthe-story-of-a-tree-English.pdf&response-content-type=application%2Fpdf



But when i am changing what i am changing
graph[(u-1)][(v-1)] = 1;
graph[(v-1)][(u-1)] = 1; while taking input edges
also here alice[(vchild-1)] = (upar-1);
to take the given test case as it is in my program but my answer coming wrong this time I am also reducing 1 from each vertices while taking input edges Why this is happening?



    #pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
int visited[1000],parent[100],alice[100];
struct queue {
int rear;
int front;
int capacity;
int* array;
};

struct queue* createqueue(int capacity) {
struct queue* Q = (struct queue*)malloc(sizeof(struct queue));
Q->capacity = capacity;
Q->front = -1;
Q->rear = -1;
Q->array = (int*)malloc(sizeof(int)*capacity);
return Q;
}
int isempty(struct queue* Q) {
return(Q->front == -1 && Q->rear == -1);

}
int isfull(struct queue* Q) {
return((Q->rear + 1) % Q->capacity == Q->front);

}
void push(struct queue* Q, int data) {
if (isfull(Q))
return;
else if (isempty(Q))
{
Q->rear = 0;
Q->front = 0;
Q->array[Q->rear] = data;
}
else {
Q->rear = ((Q->rear + 1) % Q->capacity);
Q->array[Q->rear] = data;
}
}
int pop(struct queue* Q) {
if (isempty(Q))
return -1;
else if (Q->rear == Q->front) {
int temp = Q->rear;
Q->rear = -1;
Q->front = -1;
return(Q->array[temp]);
}
else {
int temp = Q->front;
Q->front = ((Q->front + 1) % Q->capacity);
return Q->array[temp];

}
}
void bfs(int** graph ,int ver,int s) {
struct queue* Q=createqueue(100);
push(Q, s);
visited[s] = 1;
int v, w;
while (!isempty(Q)) {
v = pop(Q);
for (int j = 0; j < ver; j++) {
if (visited[j] == 0 && graph[v][j] == 1)
{
parent[j] = v;
push(Q, j);
visited[j] = 1;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t) {
int** graph;
int i, ver, u, v;
scanf("%d", &ver);
graph = (int**)malloc(sizeof(int*)*ver);
for (i = 0; i < ver; i++)
graph[i] = (int*)malloc(sizeof(int)*ver);
for (int i = 0; i < ver; i++) {
for (int j = 0; j < ver; j++) {
graph[i][j] = 0;
}
}
// printf("%d", graph[1][1]);
for (int j = 0; j < ver - 1; j++) {
scanf("%d %d", &u, &v);
graph[u-1][v-1] = 1;
graph[v-1][u-1] = 1;
}
int g, k;
scanf("%d %d", &g, &k);
int count = 0, win = 0;
int vchild, upar;
for (int i = 0; i < ver; i++)
alice[i] = -1;
for (int i = 0; i < g; i++) {
scanf("%d %d", &upar, &vchild);
alice[vchild-1] = upar-1;
}
for (int i = 0; i < v; i++) {
bfs(graph, v, i);
for (int j = 0; j < v; j++) {
if (alice[i] != -1 && alice[i] == parent[i])
count++;
}
if (count >= k)
win++;
}
for (int i = 2; i <= win && i <= ver; i++) {
if (win%i == 0 && ver%i == 0) {
win = win / i;
ver = ver / i;
}
}
printf("%d/%dn", win, ver);
t--;
}




}









share|improve this question























  • Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

    – Retired Ninja
    Nov 18 '18 at 18:30













  • sir please help

    – user10628441
    Nov 18 '18 at 18:31











  • Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

    – Retired Ninja
    Nov 18 '18 at 18:54


















1















Guys I am new to competitive programming ,I am facing a small problem
while giving input
In the question number of vertices are starting from 1 to n
But i write the program considering that nodes are starting from 0



But when i am giving input the test cases by reducing 1 from each vertices for each edge my program is running fine out of the given test cases;



given test case;
1(checking for first one only otherwise 2 was given)
4
1 2
1 3
3 4
2 2
1 2
3 4

my test case(after reducing 1 from edges):
1
4
0 1
0 2
2 3
2 2
0 1
2 3


Link for the question:



https://hackerrank-challenge-pdfs.s3.amazonaws.com/29036-the-story-of-a-tree-English?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1542565481&Signature=WoegY4gKUz0OUDEQ3n2UT80FUc0%3D&response-content-disposition=inline%3B%20filename%3Dthe-story-of-a-tree-English.pdf&response-content-type=application%2Fpdf



But when i am changing what i am changing
graph[(u-1)][(v-1)] = 1;
graph[(v-1)][(u-1)] = 1; while taking input edges
also here alice[(vchild-1)] = (upar-1);
to take the given test case as it is in my program but my answer coming wrong this time I am also reducing 1 from each vertices while taking input edges Why this is happening?



    #pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
int visited[1000],parent[100],alice[100];
struct queue {
int rear;
int front;
int capacity;
int* array;
};

struct queue* createqueue(int capacity) {
struct queue* Q = (struct queue*)malloc(sizeof(struct queue));
Q->capacity = capacity;
Q->front = -1;
Q->rear = -1;
Q->array = (int*)malloc(sizeof(int)*capacity);
return Q;
}
int isempty(struct queue* Q) {
return(Q->front == -1 && Q->rear == -1);

}
int isfull(struct queue* Q) {
return((Q->rear + 1) % Q->capacity == Q->front);

}
void push(struct queue* Q, int data) {
if (isfull(Q))
return;
else if (isempty(Q))
{
Q->rear = 0;
Q->front = 0;
Q->array[Q->rear] = data;
}
else {
Q->rear = ((Q->rear + 1) % Q->capacity);
Q->array[Q->rear] = data;
}
}
int pop(struct queue* Q) {
if (isempty(Q))
return -1;
else if (Q->rear == Q->front) {
int temp = Q->rear;
Q->rear = -1;
Q->front = -1;
return(Q->array[temp]);
}
else {
int temp = Q->front;
Q->front = ((Q->front + 1) % Q->capacity);
return Q->array[temp];

}
}
void bfs(int** graph ,int ver,int s) {
struct queue* Q=createqueue(100);
push(Q, s);
visited[s] = 1;
int v, w;
while (!isempty(Q)) {
v = pop(Q);
for (int j = 0; j < ver; j++) {
if (visited[j] == 0 && graph[v][j] == 1)
{
parent[j] = v;
push(Q, j);
visited[j] = 1;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t) {
int** graph;
int i, ver, u, v;
scanf("%d", &ver);
graph = (int**)malloc(sizeof(int*)*ver);
for (i = 0; i < ver; i++)
graph[i] = (int*)malloc(sizeof(int)*ver);
for (int i = 0; i < ver; i++) {
for (int j = 0; j < ver; j++) {
graph[i][j] = 0;
}
}
// printf("%d", graph[1][1]);
for (int j = 0; j < ver - 1; j++) {
scanf("%d %d", &u, &v);
graph[u-1][v-1] = 1;
graph[v-1][u-1] = 1;
}
int g, k;
scanf("%d %d", &g, &k);
int count = 0, win = 0;
int vchild, upar;
for (int i = 0; i < ver; i++)
alice[i] = -1;
for (int i = 0; i < g; i++) {
scanf("%d %d", &upar, &vchild);
alice[vchild-1] = upar-1;
}
for (int i = 0; i < v; i++) {
bfs(graph, v, i);
for (int j = 0; j < v; j++) {
if (alice[i] != -1 && alice[i] == parent[i])
count++;
}
if (count >= k)
win++;
}
for (int i = 2; i <= win && i <= ver; i++) {
if (win%i == 0 && ver%i == 0) {
win = win / i;
ver = ver / i;
}
}
printf("%d/%dn", win, ver);
t--;
}




}









share|improve this question























  • Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

    – Retired Ninja
    Nov 18 '18 at 18:30













  • sir please help

    – user10628441
    Nov 18 '18 at 18:31











  • Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

    – Retired Ninja
    Nov 18 '18 at 18:54
















1












1








1








Guys I am new to competitive programming ,I am facing a small problem
while giving input
In the question number of vertices are starting from 1 to n
But i write the program considering that nodes are starting from 0



But when i am giving input the test cases by reducing 1 from each vertices for each edge my program is running fine out of the given test cases;



given test case;
1(checking for first one only otherwise 2 was given)
4
1 2
1 3
3 4
2 2
1 2
3 4

my test case(after reducing 1 from edges):
1
4
0 1
0 2
2 3
2 2
0 1
2 3


Link for the question:



https://hackerrank-challenge-pdfs.s3.amazonaws.com/29036-the-story-of-a-tree-English?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1542565481&Signature=WoegY4gKUz0OUDEQ3n2UT80FUc0%3D&response-content-disposition=inline%3B%20filename%3Dthe-story-of-a-tree-English.pdf&response-content-type=application%2Fpdf



But when i am changing what i am changing
graph[(u-1)][(v-1)] = 1;
graph[(v-1)][(u-1)] = 1; while taking input edges
also here alice[(vchild-1)] = (upar-1);
to take the given test case as it is in my program but my answer coming wrong this time I am also reducing 1 from each vertices while taking input edges Why this is happening?



    #pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
int visited[1000],parent[100],alice[100];
struct queue {
int rear;
int front;
int capacity;
int* array;
};

struct queue* createqueue(int capacity) {
struct queue* Q = (struct queue*)malloc(sizeof(struct queue));
Q->capacity = capacity;
Q->front = -1;
Q->rear = -1;
Q->array = (int*)malloc(sizeof(int)*capacity);
return Q;
}
int isempty(struct queue* Q) {
return(Q->front == -1 && Q->rear == -1);

}
int isfull(struct queue* Q) {
return((Q->rear + 1) % Q->capacity == Q->front);

}
void push(struct queue* Q, int data) {
if (isfull(Q))
return;
else if (isempty(Q))
{
Q->rear = 0;
Q->front = 0;
Q->array[Q->rear] = data;
}
else {
Q->rear = ((Q->rear + 1) % Q->capacity);
Q->array[Q->rear] = data;
}
}
int pop(struct queue* Q) {
if (isempty(Q))
return -1;
else if (Q->rear == Q->front) {
int temp = Q->rear;
Q->rear = -1;
Q->front = -1;
return(Q->array[temp]);
}
else {
int temp = Q->front;
Q->front = ((Q->front + 1) % Q->capacity);
return Q->array[temp];

}
}
void bfs(int** graph ,int ver,int s) {
struct queue* Q=createqueue(100);
push(Q, s);
visited[s] = 1;
int v, w;
while (!isempty(Q)) {
v = pop(Q);
for (int j = 0; j < ver; j++) {
if (visited[j] == 0 && graph[v][j] == 1)
{
parent[j] = v;
push(Q, j);
visited[j] = 1;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t) {
int** graph;
int i, ver, u, v;
scanf("%d", &ver);
graph = (int**)malloc(sizeof(int*)*ver);
for (i = 0; i < ver; i++)
graph[i] = (int*)malloc(sizeof(int)*ver);
for (int i = 0; i < ver; i++) {
for (int j = 0; j < ver; j++) {
graph[i][j] = 0;
}
}
// printf("%d", graph[1][1]);
for (int j = 0; j < ver - 1; j++) {
scanf("%d %d", &u, &v);
graph[u-1][v-1] = 1;
graph[v-1][u-1] = 1;
}
int g, k;
scanf("%d %d", &g, &k);
int count = 0, win = 0;
int vchild, upar;
for (int i = 0; i < ver; i++)
alice[i] = -1;
for (int i = 0; i < g; i++) {
scanf("%d %d", &upar, &vchild);
alice[vchild-1] = upar-1;
}
for (int i = 0; i < v; i++) {
bfs(graph, v, i);
for (int j = 0; j < v; j++) {
if (alice[i] != -1 && alice[i] == parent[i])
count++;
}
if (count >= k)
win++;
}
for (int i = 2; i <= win && i <= ver; i++) {
if (win%i == 0 && ver%i == 0) {
win = win / i;
ver = ver / i;
}
}
printf("%d/%dn", win, ver);
t--;
}




}









share|improve this question














Guys I am new to competitive programming ,I am facing a small problem
while giving input
In the question number of vertices are starting from 1 to n
But i write the program considering that nodes are starting from 0



But when i am giving input the test cases by reducing 1 from each vertices for each edge my program is running fine out of the given test cases;



given test case;
1(checking for first one only otherwise 2 was given)
4
1 2
1 3
3 4
2 2
1 2
3 4

my test case(after reducing 1 from edges):
1
4
0 1
0 2
2 3
2 2
0 1
2 3


Link for the question:



https://hackerrank-challenge-pdfs.s3.amazonaws.com/29036-the-story-of-a-tree-English?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1542565481&Signature=WoegY4gKUz0OUDEQ3n2UT80FUc0%3D&response-content-disposition=inline%3B%20filename%3Dthe-story-of-a-tree-English.pdf&response-content-type=application%2Fpdf



But when i am changing what i am changing
graph[(u-1)][(v-1)] = 1;
graph[(v-1)][(u-1)] = 1; while taking input edges
also here alice[(vchild-1)] = (upar-1);
to take the given test case as it is in my program but my answer coming wrong this time I am also reducing 1 from each vertices while taking input edges Why this is happening?



    #pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
int visited[1000],parent[100],alice[100];
struct queue {
int rear;
int front;
int capacity;
int* array;
};

struct queue* createqueue(int capacity) {
struct queue* Q = (struct queue*)malloc(sizeof(struct queue));
Q->capacity = capacity;
Q->front = -1;
Q->rear = -1;
Q->array = (int*)malloc(sizeof(int)*capacity);
return Q;
}
int isempty(struct queue* Q) {
return(Q->front == -1 && Q->rear == -1);

}
int isfull(struct queue* Q) {
return((Q->rear + 1) % Q->capacity == Q->front);

}
void push(struct queue* Q, int data) {
if (isfull(Q))
return;
else if (isempty(Q))
{
Q->rear = 0;
Q->front = 0;
Q->array[Q->rear] = data;
}
else {
Q->rear = ((Q->rear + 1) % Q->capacity);
Q->array[Q->rear] = data;
}
}
int pop(struct queue* Q) {
if (isempty(Q))
return -1;
else if (Q->rear == Q->front) {
int temp = Q->rear;
Q->rear = -1;
Q->front = -1;
return(Q->array[temp]);
}
else {
int temp = Q->front;
Q->front = ((Q->front + 1) % Q->capacity);
return Q->array[temp];

}
}
void bfs(int** graph ,int ver,int s) {
struct queue* Q=createqueue(100);
push(Q, s);
visited[s] = 1;
int v, w;
while (!isempty(Q)) {
v = pop(Q);
for (int j = 0; j < ver; j++) {
if (visited[j] == 0 && graph[v][j] == 1)
{
parent[j] = v;
push(Q, j);
visited[j] = 1;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t) {
int** graph;
int i, ver, u, v;
scanf("%d", &ver);
graph = (int**)malloc(sizeof(int*)*ver);
for (i = 0; i < ver; i++)
graph[i] = (int*)malloc(sizeof(int)*ver);
for (int i = 0; i < ver; i++) {
for (int j = 0; j < ver; j++) {
graph[i][j] = 0;
}
}
// printf("%d", graph[1][1]);
for (int j = 0; j < ver - 1; j++) {
scanf("%d %d", &u, &v);
graph[u-1][v-1] = 1;
graph[v-1][u-1] = 1;
}
int g, k;
scanf("%d %d", &g, &k);
int count = 0, win = 0;
int vchild, upar;
for (int i = 0; i < ver; i++)
alice[i] = -1;
for (int i = 0; i < g; i++) {
scanf("%d %d", &upar, &vchild);
alice[vchild-1] = upar-1;
}
for (int i = 0; i < v; i++) {
bfs(graph, v, i);
for (int j = 0; j < v; j++) {
if (alice[i] != -1 && alice[i] == parent[i])
count++;
}
if (count >= k)
win++;
}
for (int i = 2; i <= win && i <= ver; i++) {
if (win%i == 0 && ver%i == 0) {
win = win / i;
ver = ver / i;
}
}
printf("%d/%dn", win, ver);
t--;
}




}






c algorithm graph






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 17:47







user10628441




















  • Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

    – Retired Ninja
    Nov 18 '18 at 18:30













  • sir please help

    – user10628441
    Nov 18 '18 at 18:31











  • Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

    – Retired Ninja
    Nov 18 '18 at 18:54





















  • Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

    – Retired Ninja
    Nov 18 '18 at 18:30













  • sir please help

    – user10628441
    Nov 18 '18 at 18:31











  • Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

    – Retired Ninja
    Nov 18 '18 at 18:54



















Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

– Retired Ninja
Nov 18 '18 at 18:30







Seems like an excellent case to step through in a debugger so you can see what the difference is. You also missed changing a line in your modified test case.

– Retired Ninja
Nov 18 '18 at 18:30















sir please help

– user10628441
Nov 18 '18 at 18:31





sir please help

– user10628441
Nov 18 '18 at 18:31













Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

– Retired Ninja
Nov 18 '18 at 18:54







Add some error checking on your scanf lines, it returns a value for a reason. Add some printfs at each stage of your program so you can see what's happening as it runs. Change your -1 to -X so you can easily change your program to accept the 1-based and 0-based data so you can compare the output of the print statements you added and see where the difference is. Read through this: ericlippert.com/2014/03/05/how-to-debug-small-programs Debugging is the most important skill a programmer can have. Learn now while your programs are small.

– Retired Ninja
Nov 18 '18 at 18:54














1 Answer
1






active

oldest

votes


















1














Your code has several problems:




  • The scope of your variables is too wide. You reuse variables without resetting them. You can avoid that by using the closest possible scope when defining variables and by initialising them when you define them. (Reusing old values applies to your parent and visited arrays and to the count.

  • When you count Alice's correct guesses, the iterating variable of the inner loop is j, but yu test alice[i] and parent[i].

  • When you simplify the fraction, the if should be a while, otherwise you will miss out on squares and cubes.

  • In main, you mix up the number of vertices ver and the variable v a lot.


But why do the outputs differ when you give the zero-indexd variables as input?



As I said above, you use v often when you really want ver. The variable v is used only correctly when you scan the edges and is, of course, different for one-based and zero-based input. Close scpoing is your friend here, too: Make u and v local to the loop where you scan the edges.



(For what it's worth, I don't think the matrix representation of the graph is useful for larger graphs, because the graph is sparse. You may lose a lot of time when scanning through rows of 100,000 entries repeatedly.)






share|improve this answer
























  • Please i am still not getting the correct answer

    – user10628441
    Nov 18 '18 at 19:32











  • Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

    – M Oehm
    Nov 18 '18 at 19:35













  • Thank you so much I will learn when i will try myself

    – user10628441
    Nov 18 '18 at 19:52











  • I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

    – user10628441
    Nov 19 '18 at 9:26













  • I will not ask you again only once please

    – user10628441
    Nov 19 '18 at 9:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363807%2fshowing-different-output-for-same-inputnearly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your code has several problems:




  • The scope of your variables is too wide. You reuse variables without resetting them. You can avoid that by using the closest possible scope when defining variables and by initialising them when you define them. (Reusing old values applies to your parent and visited arrays and to the count.

  • When you count Alice's correct guesses, the iterating variable of the inner loop is j, but yu test alice[i] and parent[i].

  • When you simplify the fraction, the if should be a while, otherwise you will miss out on squares and cubes.

  • In main, you mix up the number of vertices ver and the variable v a lot.


But why do the outputs differ when you give the zero-indexd variables as input?



As I said above, you use v often when you really want ver. The variable v is used only correctly when you scan the edges and is, of course, different for one-based and zero-based input. Close scpoing is your friend here, too: Make u and v local to the loop where you scan the edges.



(For what it's worth, I don't think the matrix representation of the graph is useful for larger graphs, because the graph is sparse. You may lose a lot of time when scanning through rows of 100,000 entries repeatedly.)






share|improve this answer
























  • Please i am still not getting the correct answer

    – user10628441
    Nov 18 '18 at 19:32











  • Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

    – M Oehm
    Nov 18 '18 at 19:35













  • Thank you so much I will learn when i will try myself

    – user10628441
    Nov 18 '18 at 19:52











  • I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

    – user10628441
    Nov 19 '18 at 9:26













  • I will not ask you again only once please

    – user10628441
    Nov 19 '18 at 9:27
















1














Your code has several problems:




  • The scope of your variables is too wide. You reuse variables without resetting them. You can avoid that by using the closest possible scope when defining variables and by initialising them when you define them. (Reusing old values applies to your parent and visited arrays and to the count.

  • When you count Alice's correct guesses, the iterating variable of the inner loop is j, but yu test alice[i] and parent[i].

  • When you simplify the fraction, the if should be a while, otherwise you will miss out on squares and cubes.

  • In main, you mix up the number of vertices ver and the variable v a lot.


But why do the outputs differ when you give the zero-indexd variables as input?



As I said above, you use v often when you really want ver. The variable v is used only correctly when you scan the edges and is, of course, different for one-based and zero-based input. Close scpoing is your friend here, too: Make u and v local to the loop where you scan the edges.



(For what it's worth, I don't think the matrix representation of the graph is useful for larger graphs, because the graph is sparse. You may lose a lot of time when scanning through rows of 100,000 entries repeatedly.)






share|improve this answer
























  • Please i am still not getting the correct answer

    – user10628441
    Nov 18 '18 at 19:32











  • Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

    – M Oehm
    Nov 18 '18 at 19:35













  • Thank you so much I will learn when i will try myself

    – user10628441
    Nov 18 '18 at 19:52











  • I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

    – user10628441
    Nov 19 '18 at 9:26













  • I will not ask you again only once please

    – user10628441
    Nov 19 '18 at 9:27














1












1








1







Your code has several problems:




  • The scope of your variables is too wide. You reuse variables without resetting them. You can avoid that by using the closest possible scope when defining variables and by initialising them when you define them. (Reusing old values applies to your parent and visited arrays and to the count.

  • When you count Alice's correct guesses, the iterating variable of the inner loop is j, but yu test alice[i] and parent[i].

  • When you simplify the fraction, the if should be a while, otherwise you will miss out on squares and cubes.

  • In main, you mix up the number of vertices ver and the variable v a lot.


But why do the outputs differ when you give the zero-indexd variables as input?



As I said above, you use v often when you really want ver. The variable v is used only correctly when you scan the edges and is, of course, different for one-based and zero-based input. Close scpoing is your friend here, too: Make u and v local to the loop where you scan the edges.



(For what it's worth, I don't think the matrix representation of the graph is useful for larger graphs, because the graph is sparse. You may lose a lot of time when scanning through rows of 100,000 entries repeatedly.)






share|improve this answer













Your code has several problems:




  • The scope of your variables is too wide. You reuse variables without resetting them. You can avoid that by using the closest possible scope when defining variables and by initialising them when you define them. (Reusing old values applies to your parent and visited arrays and to the count.

  • When you count Alice's correct guesses, the iterating variable of the inner loop is j, but yu test alice[i] and parent[i].

  • When you simplify the fraction, the if should be a while, otherwise you will miss out on squares and cubes.

  • In main, you mix up the number of vertices ver and the variable v a lot.


But why do the outputs differ when you give the zero-indexd variables as input?



As I said above, you use v often when you really want ver. The variable v is used only correctly when you scan the edges and is, of course, different for one-based and zero-based input. Close scpoing is your friend here, too: Make u and v local to the loop where you scan the edges.



(For what it's worth, I don't think the matrix representation of the graph is useful for larger graphs, because the graph is sparse. You may lose a lot of time when scanning through rows of 100,000 entries repeatedly.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 19:10









M OehmM Oehm

21.4k31832




21.4k31832













  • Please i am still not getting the correct answer

    – user10628441
    Nov 18 '18 at 19:32











  • Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

    – M Oehm
    Nov 18 '18 at 19:35













  • Thank you so much I will learn when i will try myself

    – user10628441
    Nov 18 '18 at 19:52











  • I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

    – user10628441
    Nov 19 '18 at 9:26













  • I will not ask you again only once please

    – user10628441
    Nov 19 '18 at 9:27



















  • Please i am still not getting the correct answer

    – user10628441
    Nov 18 '18 at 19:32











  • Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

    – M Oehm
    Nov 18 '18 at 19:35













  • Thank you so much I will learn when i will try myself

    – user10628441
    Nov 18 '18 at 19:52











  • I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

    – user10628441
    Nov 19 '18 at 9:26













  • I will not ask you again only once please

    – user10628441
    Nov 19 '18 at 9:27

















Please i am still not getting the correct answer

– user10628441
Nov 18 '18 at 19:32





Please i am still not getting the correct answer

– user10628441
Nov 18 '18 at 19:32













Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

– M Oehm
Nov 18 '18 at 19:35







Well, revise your code. It has a lot of errors. I've pointed out some. Also heed the advice about debugging given to you in comments. It is you who wants to enter the competition, so it should also be your code. (The idea of such competitions is to learn by programming.)

– M Oehm
Nov 18 '18 at 19:35















Thank you so much I will learn when i will try myself

– user10628441
Nov 18 '18 at 19:52





Thank you so much I will learn when i will try myself

– user10628441
Nov 18 '18 at 19:52













I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

– user10628441
Nov 19 '18 at 9:26







I tried my best whole night sir and i also fix the issues that you told me still i am passing only one test case I myself tried to make a number of large test and i was passing all of them but still it is not passing system test case and in some one or two test case it is showing segmentaion fault also please see my code once more [ideone.com/5pNxn5 ]

– user10628441
Nov 19 '18 at 9:26















I will not ask you again only once please

– user10628441
Nov 19 '18 at 9:27





I will not ask you again only once please

– user10628441
Nov 19 '18 at 9:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363807%2fshowing-different-output-for-same-inputnearly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini