Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- int read(){
- char c=getchar_unlocked();
- int n=0;
- while(!(c>='0' && c<='9'))
- c=getchar_unlocked();
- while(c>='0' && c<='9'){
- n=n*10 + (c-'0');
- c=getchar_unlocked();
- }
- return n;
- }
- int stamp[25][20005]={{0}};//orignally storing the values of stamps j which are asked by the ith group in stamp[i][j]
- int graph[25][25]={{0}};//this stores the groups which intersect with other groups
- int after[25]={0};//sum of all the elements in the graph (coloumn --) which denotes the number of collision for that group
- int possible[25]={0};//value is one if the group of stamp is selected
- int summation[20005]={0};
- int main(){
- int N,M;
- N=read();
- M=read();
- for(int i=1;i<=M;i++){
- int x;
- x=read();
- stamp[i][0]=x;
- for(int j=1;j<=x;j++){int y;
- y=read();
- stamp[i][y]=1;
- }
- }
- for(int j=1;j<=N;j++){
- int sum=0;
- for(int i=1;i<=M;i++){
- if(i==j)
- graph[i][j]=1;// summing up all the coloumns in the stamps matrix which denotes how many groups are asking for a particular stamp
- sum=sum+stamp[i][j];
- }
- summation[j]=sum;
- }
- for(int i=1;i<=N;i++){//making a graph denoting which groups cant be selected together
- if(summation[i]<=1)
- continue;
- for(int j=1;j<=M;j++){
- if(stamp[j][i]==1){
- for(int k=1;k<=M;k++)
- {
- if(stamp[k][i]==1)
- graph[j][k]=1;
- }
- }
- }
- }
- for(int i=1;i<=M;i++){
- int sump=0;
- possible[i]=1;
- for(int j=1;j<=M;j++)
- sump=sump+graph[i][j];
- after[i]=sump;//summing up the elements in the graphs (coloumn)
- }
- //printing the graph matrix which represents which groups cannot sit together
- /* for(int i=1;i<=M;i++){
- for(int j=1;j<=M;j++){
- printf("%d ",graph[i][j]);
- }
- printf("=%d",after[i]);
- printf("\n");
- }*/
- { //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
- int elem=M;
- while(elem>0){
- int min=999,grp=100;
- for(int i=1;i<=M;i++)
- { if(after[i]<=min)
- min=after[i],grp=i;
- }
- 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
- {
- if(graph[grp][k]==1)
- possible[k]=0,elem--;
- }
- possible[grp]=1;
- after[grp]=9999;
- }
- }
- int answer=0;
- for(int i=1;i<=M;i++){
- answer=answer+possible[i];
- }
- printf("%d",answer);
- return 0;
- }
- //7 5 3 1 2 3 3 2 4 7 2 4 5 1 6 2 4 7
- //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