relative.datCode:#include <stdio.h> #include <stdlib.h> #define bool short #define true 1 #define false 0 #define maxP 10000 int set_relative(int relative[maxP][2], int m, int n) { int chart[maxP][maxP]; int i, j, r1, r2, max_contact; bool group[maxP]; bool contact; for (i = 0; i < m; i++) for (j = 0; j < m; j++) chart[i][j] = 0; max_contact = 1; chart[relative[0][0]-1][relative[0][1]-1] = 1; chart[relative[0][1]-1][relative[0][0]-1] = 1; for (i = 0; i < n; i++) { contact = false; r1 = relative[i][0]-1; r2 = relative[i][1]-1; for (j = 0; j < m; j++) { if (chart[j][r1] != 0) { chart[r1][r2] = chart[j][r1]; chart[r2][r1] = chart[j][r1]; contact = true; break; } else if (chart [r2][j] != 0) { chart[r1][r2] = chart[r2][j]; chart[r2][r1] = chart[r2][j]; contact = true; break; } } if (!contact) { max_contact++; chart[r1][r2] = max_contact; chart[r2][r1] = max_contact; } } for (i = 0; i < m; i++) { group[relative[i][0]-1] = true; group[relative[i][1]-1] = true; } for (i = 0; i < m; i++) if (group[i] == false) max_contact++; return max_contact; } int main() { FILE *fr, *fw; int max_contact, m, n, i; int relative[maxP][2]; fr = fopen("relative.dat", "rt"); fw = fopen("relative.out", "wt"); fscanf(fr, "%d %d", &m, &n); for (i = 0; i < n; i++) fscanf(fr, "%d %d", &relative[i][0], &relative[i][1]); max_contact = set_relative(relative, m, n); fprintf(fw, "%d", max_contact); fclose(fr); fclose(fw); return 0; }

Code:8 6 1 2 1 4 2 3 2 4 5 6 5 8There has no compiling wrong message. during my debugging, I think the chart 2darray overflow. or appeal other problem?Code:mathsniper:~# gcc -O2 -o relative relative.c mathsniper:~# ./relative Segmentation fault