-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireEscapeRoutes.java
More file actions
99 lines (71 loc) · 1.95 KB
/
FireEscapeRoutes.java
File metadata and controls
99 lines (71 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by hardCode on 2/20/2016.
*/
public class FireEscapeRoutes {
static int x[];
final static long M=1000000007;
static BufferedReader br;
public static void main(String[] args) throws IOException {
br=new BufferedReader(new InputStreamReader(System.in));
int t;
int n,m;
String s[];
t=Integer.parseInt(br.readLine());
x=new int[100001];
while (t-->0){
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
for (int i = 1; i <=n; i++) {
x[i]=i;
}
solve(n, m);
}
}
static void solve(int n,int m) throws IOException {
//System.out.println(Arrays.toString(x));
String s[];
long prod=1,count=0;
for (int i = 0; i <m ; i++) {
s=br.readLine().split("\\s");
setUnion(Integer.parseInt(s[0]),Integer.parseInt(s[1]));
}
for (int i = 1; i <= n; i++) {
findSet(i);
}
Arrays.sort(x,1,n+1);
//System.out.println(Arrays.toString(x));
for (int i = 1,j; i <= n; i++) {
for (j = i+1; j <= n; j++) {
if (x[i]!=x[j])break;
}
count++;
prod=prod*(j-i)%M;
i=j-1;
}
System.out.println(count+" "+prod);
}
static void setUnion(int a,int b)
{
x[findSet(a)]=findSet(b);
}
static int findSet(int a){
/* int i=0,y[]=new int[100001];
while (x[xenny]!=xenny){
y[i]=xenny;
xenny=x[xenny];
i++;
}
for (int j = 0; j < i; j++) {
x[y[j]]=x[xenny];
}
*/
if (x[a]!=a)x[a]=findSet(x[a]);
return x[a];
}
}