2017-07-25 61 views
0
#include <stdio.h> 

#define MAX 1000000 

int dp[MAX]; 
int P[MAX], C[MAX], K[MAX], child[MAX][1000], index[MAX]; 
int mod = 1000000007; 

void dfs(int i) { 
    int j = 1; 
    while (j <= index[i]) { 
     dfs(child[i][j]); 
     if ((C[child[i][j]] == C[i]) && (K[i] - K[child[i][j]] == 1)) 
      dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod; 
     ++j; 
    } 
} 

int main() { 
    int T, N, X, i, j; 
    scanf("%d", &T); 
    while (T--) { 
     scanf("%d%d", &N, &X); 

     for (i = 0; i < N; ++i) 
      index[i] = 0; 

     for (i = 1; i < N; ++i) { 
      scanf("%d", &P[i]); 
      child[P[i]][++index[P[i]]] = i; 
     } 
     for (i = 0; i < N; ++i) 
      scanf("%d", &C[i]); 
     for (i = 0; i < N; ++i) { 
      scanf("%d", &K[i]); 
      if (K[i]) 
       dp[i] = 0; 
      else 
       dp[i] = 1; 
     } 
     dfs(0); 
     for (i = 0; i < N; ++i) 
      printf("%d\n", dp[i]); 
    } 
    return 0; 
} 

当我编译上面的代码中,我得到了以下错误:定位误差

In function `dfs': 
(.text+0xa): relocation truncated to fit: R_X86_64_32S against symbol `index' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x3b): relocation truncated to fit: R_X86_64_32S against symbol `index' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x4f): relocation truncated to fit: R_X86_64_32S against symbol `C' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x56): relocation truncated to fit: R_X86_64_32S against symbol `C' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x60): relocation truncated to fit: R_X86_64_32S against symbol `K' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x67): relocation truncated to fit: R_X86_64_32S against symbol `K' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x74): relocation truncated to fit: R_X86_64_32S against symbol `dp' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x84): relocation truncated to fit: R_X86_64_32S against symbol `dp' defined in COMMON section in /tmp/cc0VMXET.o 
(.text+0x97): relocation truncated to fit: R_X86_64_32S against symbol `dp' defined in COMMON section in /tmp/cc0VMXET.o 
In function `main': 
(.text.startup+0x6e): relocation truncated to fit: R_X86_64_32S against symbol `index' defined in COMMON section in /tmp/cc0VMXET.o 
(.text.startup+0xba): additional relocation overflows omitted from the output 
error: ld returned 1 exit status 

我知道这是什么错误,它是如何发生的。我已经在stackoverflow中搜索它,但我不明白如何纠正它。有人可以告诉我如何纠正我的代码?

+3

你怎么编?什么是论据?并在什么平台上?什么编译器? – bolov

+0

我认为你已经违反了int变量的容量,mod' –

+0

@ Md.RezwanulHaque大概这个问题的答案是“没有警告”,像往常一样。 –

回答

3

您可能对程序中定义的全局变量的大小达到限制:单独的2D阵列child的大小为4000000000字节(40亿字节)。

要么减小这些全局变量的大小,要么在开始时将它们从堆中分配到calloc()

试试这个版本,其中child从堆中分配:

#include <stdio.h> 
#include <stdlib.h> 

#define MAX 1000000 

int dp[MAX], P[MAX], C[MAX], K[MAX], index[MAX]; 
int (*child)[1000]; 
int mod = 1000000007; 

void dfs(int i) { 
    int j = 1; 
    while (j <= index[i]) { 
     dfs(child[i][j]); 
     if ((C[child[i][j]] == C[i]) && (K[i] - K[child[i][j]] == 1)) 
      dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod; 
     ++j; 
    } 
} 

int main(void) { 
    int T, N, X, i; 

    child = calloc(MAX, sizeof(*child)); 
    if (child == NULL) { 
     fprintf(stderr, "cannot allocate child array (%llu bytes)\n", 
       (unsigned long long)MAX * sizeof(*child)); 
     return 1; 
    } 

    scanf("%d", &T); 
    while (T--) { 
     scanf("%d%d", &N, &X); 

     for (i = 0; i < N; ++i) 
      index[i] = 0; 

     for (i = 1; i < N; ++i) { 
      scanf("%d", &P[i]); 
      child[P[i]][++index[P[i]]] = i; 
     } 
     for (i = 0; i < N; ++i) 
      scanf("%d", &C[i]); 
     for (i = 0; i < N; ++i) { 
      scanf("%d", &K[i]); 
      if (K[i]) 
       dp[i] = 0; 
      else 
       dp[i] = 1; 
     } 
     dfs(0); 
     for (i = 0; i < N; ++i) 
      printf("%d\n", dp[i]); 
    } 
    free(child); 
    return 0; 
} 

注:

  • 您也应该检查是否从标准输入读取的值与分配的尺寸兼容( N <= MAXP[i] < 1000)。

  • 如果分配失败,则可以减少MAX1000

  • dp[i] = (dp[i] % mod + dp[child[i][j]] % mod) % mod;可以简化为dp[i] = (dp[i] + dp[child[i][j]]) % mod;

+0

我试图运行你的代码。以前的问题已被正确地管理;谢谢你。但是,现在它给了NZEC这是segmentaion error.Please告诉我如何改正它。 –

+0

@ShanifAnsari:没有测试数据很难说 – chqrlie