Advertisement
Guest User

Solution for MIKE3

a guest
Mar 18th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include<cstdio>
  2.  
  3. int read(){
  4. char c=getchar_unlocked();
  5.  int n=0;
  6. while(!(c>='0' && c<='9'))
  7. c=getchar_unlocked();
  8. while(c>='0' && c<='9'){
  9. n=n*10 + (c-'0');
  10. c=getchar_unlocked();
  11. }
  12. return n;
  13. }
  14. int stamp[25][20005]={{0}};//orignally storing the values of stamps j which are asked by the ith group in stamp[i][j]
  15. int graph[25][25]={{0}};//this stores the groups which intersect with other groups
  16. int after[25]={0};//sum of all the elements in the graph (coloumn --) which denotes the number of collision for that group
  17. int possible[25]={0};//value is one if the group of stamp is selected
  18.  
  19. int summation[20005]={0};
  20.  
  21. int main(){
  22.     int N,M;
  23.     N=read();
  24.     M=read();
  25.    
  26.     for(int i=1;i<=M;i++){
  27.         int x;
  28.         x=read();
  29.         stamp[i][0]=x;
  30.         for(int j=1;j<=x;j++){int y;
  31.         y=read();
  32.         stamp[i][y]=1;
  33.         }
  34.     }
  35.      
  36.    
  37.     for(int j=1;j<=N;j++){
  38.         int sum=0;
  39.     for(int i=1;i<=M;i++){
  40.         if(i==j)
  41.         graph[i][j]=1;// summing up all the coloumns in the stamps matrix which denotes how many groups are asking for a particular stamp
  42.         sum=sum+stamp[i][j];
  43.     }
  44.         summation[j]=sum;
  45.     }
  46.    
  47.     for(int i=1;i<=N;i++){//making a graph denoting which groups cant be selected together
  48.         if(summation[i]<=1)
  49.         continue;
  50.         for(int j=1;j<=M;j++){
  51.            if(stamp[j][i]==1){
  52.                
  53.               for(int k=1;k<=M;k++)
  54.               {
  55.                   if(stamp[k][i]==1)
  56.                   graph[j][k]=1;
  57.               }
  58.            }
  59.         }
  60.     }
  61.    
  62.    
  63.    
  64.     for(int i=1;i<=M;i++){
  65.         int sump=0;
  66.         possible[i]=1;
  67.      for(int j=1;j<=M;j++)
  68.         sump=sump+graph[i][j];
  69.         after[i]=sump;//summing up the elements in the graphs (coloumn)
  70.     }
  71.    
  72.     //printing the graph matrix which represents which groups cannot sit together
  73.     /* for(int i=1;i<=M;i++){
  74.     for(int j=1;j<=M;j++){
  75.     printf("%d ",graph[i][j]);
  76.     }
  77.     printf("=%d",after[i]);
  78.     printf("\n");
  79.     }*/
  80.    
  81.   { //this piece finds the possible groups which can sit starting from the [row] which has lowest number of 1s because it will have least number of collisions
  82.     int elem=M;
  83.     while(elem>0){
  84.     int min=999,grp=100;
  85.     for(int i=1;i<=M;i++)
  86.        { if(after[i]<=min)
  87.             min=after[i],grp=i;
  88.        }
  89.     for(int k=1;k<=M&&grp<=M;k++)//after selecting a group from the graph matrix remove all the intersecting groups from the possible matrix
  90.     {  
  91.         if(graph[grp][k]==1)
  92.         possible[k]=0,elem--;
  93.     }
  94.         possible[grp]=1;
  95.         after[grp]=9999;
  96.     }
  97.     }
  98.    
  99.    int answer=0;
  100.    for(int i=1;i<=M;i++){
  101.         answer=answer+possible[i];
  102.    }
  103.    printf("%d",answer);
  104.  
  105.    return 0;
  106. }
  107. //7 5 3 1 2 3 3 2 4 7 2 4 5 1 6 2 4 7
  108. //10 7 3 1 2 4 3 1 2 4 2 1 2 1 1 2 2 4 2 5 6 4 1 5 7 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement