【发布时间】:2019-04-21 04:10:24
【问题描述】:
伙计们,我是竞争性编程的新手,我面临一个小问题 在提供输入的同时 在问题中的顶点数从 1 到 n 但我编写程序时考虑到节点从 0 开始
但是当我通过从每条边的每个顶点减少 1 来输入测试用例时,我的程序在给定的测试用例中运行良好;
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
问题链接:
但是当我改变我正在改变的
graph[(u-1)][(v-1)] = 1;
graph[(v-1)][(u-1)] = 1; 在获取输入边时
也在这里alice[(vchild-1)] = (upar-1);
在我的程序中采用给定的测试用例,但这次我的答案出错了,我还在获取输入边的同时从每个顶点减少 1 为什么会发生这种情况?
#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/%d\n", win, ver);
t--;
}
}
【问题讨论】:
-
在调试器中单步执行似乎是一个很好的案例,因此您可以看到有什么区别。您还错过了在修改后的测试用例中更改一行。
-
先生请帮忙
-
在你的 scanf 行上添加一些错误检查,它返回一个值是有原因的。在程序的每个阶段添加一些 printfs,这样您就可以看到它运行时发生了什么。将您的 -1 更改为 -X 以便您可以轻松地更改您的程序以接受基于 1 和基于 0 的数据,以便您可以比较您添加的打印语句的输出并查看差异在哪里。通读:ericlippert.com/2014/03/05/how-to-debug-small-programs 调试是程序员可以拥有的最重要的技能。在您的程序还很小的时候立即学习。