14 Mar 2013

Fire Escape Routes_solved


/*
CODECHEF
Problem codeFIRESC


*/



#include<stdio.h>
 
inline int min(int a,int b)
{
 if (a<b)
  return a;
 else
  return b;
}
 
int t,n,m,x,y,i,j,id[100002],sz[100002],count,ng;
 
int find(int p) 
{
 while (p != id[p])
     p = id[p];
 return p;
}
 
int connected(int p, int q) 
{
 if(find(p) == find(q))
  return 1;
 else
  return 0;
}
 
void uni(int p, int q) 
{
 int i = find(p);
  int j = find(q);
   if(i == j) 
    return;
    if(sz[i] < sz[j]) 
 { 
  id[i] = j; 
  sz[j] += sz[i]; 
  sz[i]=1;
 }
    else
 {
  id[j] = i; 
  sz[i] += sz[j]; 
  sz[j]=1;
 }
  count--;
}
 
int main()
{ 
 unsigned long long r;
 scanf("%d",&t);
 while(t--)
 {
  int h[100002]={0},c=0;
  ng=0;
  r=1;
  scanf("%d%d",&n,&m);
  count=n;
  for(i=1;i<=n;i++)
  {
   id[i] = i;
   sz[i] = 1;
  }
  for(i=0;i<m;i++)
  {
   scanf("%d%d",&x,&y);
   uni(x,y);
   if(h[x]==0)
   {
    c++;
    h[x]=1;
   }
   if(h[y]==0)
   {
    c++;
    h[y]=1;
   }
  }
  for(i=1;i<=n;i++)
   if(sz[i]>1)
   {
    r = (r*sz[i])%1000000007;
    ng++;
   }
  printf("%d %llu\n",ng+n-c,r);
 }
 return 0;
}  

Categories: , , ,

Related Posts:

  • Palindrome_String /* program to check whether a string is palindrome or not in c++ */ #include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>void main(){clrscr();int i,j,k,flag=0;char a[80];cout&… Read More
  • grapline level-2 solved /*  solution to grapline online change level-2 *\ #include<stdio.h>#include<conio.h>int checkprime(int );void main(){long int n=514230,i,sum=0;for(i=2;i<n-1;i++)    {if(checkprime(i)!… Read More
  • perfect_number /*  program to check whether a given no. is a ""perfect no."" or not. a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors e… Read More
  • Inverse_matrix /*   program to find inverse of a matrix...   */ #include<stdio.h> int main() {   int a[3][3],i,j;   float determinant=0;   printf("Enter the 9 elements of matrix: "… Read More
  • graplin level-1 solved /* solution to graplin inline challenge level-1 */ #include<stdio.h>#include<conio.h>#include<string.h>void cutstr(char *,int ,int );char arr1[150]={'\0'};char arr2[150]={'\0'};char arr11[]="… Read More

0 comments:

Post a Comment

Copyright © 2025 UPgradeCODING | Powered by Blogger