Advertisement
Go-Ice

NTHU Online Judge #10938

Mar 14th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. main(){
  5.     int nodeAmount; /* 0 < N < 1000 */
  6.     int edgeAmount; /* 0 < M < 20000*/
  7.     int quesAmount; /* 0 < Q < 2000 */
  8.     int nodeA, nodeB;
  9.     int **ptr2 = NULL;
  10.     int target;
  11.     int cnt, index;
  12.     long long int sum;
  13.  
  14.     while ( scanf("%d %d %d", &nodeAmount, &edgeAmount, &quesAmount)!=EOF ){
  15.         ptr2 = (int**)malloc((nodeAmount+1) * sizeof(int*));
  16.         for ( cnt=0 ; cnt<=nodeAmount ; cnt++ )
  17.             ptr2[cnt] = (int*)malloc((nodeAmount+1) * sizeof(int));
  18.  
  19.         for ( cnt=0 ; cnt<edgeAmount ; cnt++ ){
  20.             scanf("%d %d", &nodeA, &nodeB);
  21.             ptr2[nodeA][nodeB] = 1;
  22.             ptr2[nodeB][nodeA] = 1;
  23.         }
  24.  
  25.         for ( cnt=0, sum=0 ; cnt<quesAmount ; cnt++ ){
  26.             scanf("%d", &target);
  27.             for ( index=1 ; index<=nodeAmount ; index++ )
  28.                 if ( ptr2[target][index]==1 )
  29.                     sum += index;
  30.         }
  31.  
  32.         printf("%lld\n", sum);
  33.  
  34.         for (cnt=0 ; cnt<=nodeAmount ; cnt++ )
  35.             free(ptr2[cnt]);
  36.         free(ptr2);
  37.     } /* END while */
  38.  
  39.     system("PAUSE");
  40.     return EXIT_SUCCESS;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement