http://poj.org/problem?id=2941
随机算法
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int a[1234][1234], n, stack[1234]; 6 7 int check() 8 { 9 int i, j, temp, sum1 = 0, sum2, m; 10 for(i=1; i<=n; i++) 11 { 12 sum1 += a[i][i]; 13 stack[i] = i; 14 } 15 m = 300000; 16 while(m--) 17 { 18 sum2 = sum1; 19 j = rand()%n+1; 20 sum2 -= (a[1][stack[1]] + a[j][stack[j]]); 21 temp = stack[j]; 22 stack[j] = stack[1]; 23 stack[1] = temp; 24 sum2 += (a[1][stack[1]] + a[j][stack[j]]); 25 if(sum1 != sum2) 26 { 27 return 0; 28 } 29 } 30 return 1; 31 } 32 33 int main() 34 { 35 int i, j; 36 while(scanf("%d", &n), n) 37 { 38 for(i=1; i<=n; i++) 39 { 40 for(j=1; j<=n; j++) 41 { 42 scanf("%d", &a[i][j]); 43 } 44 } 45 printf(check()? "homogeneous\n": "not homogeneous\n"); 46 } 47 return 0; 48 }