-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireEscapeRoutesDFS.java
More file actions
122 lines (97 loc) · 2.86 KB
/
FireEscapeRoutesDFS.java
File metadata and controls
122 lines (97 loc) · 2.86 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by hardCode on 2/24/2016.
*/
public class FireEscapeRoutesDFS {
static ArrayList<Integer>adj[];
final static int MAX=100001;
final static long M=1000000007;
int n,m,size;
static boolean visited[];
static BufferedReader br;
public FireEscapeRoutesDFS(int n, int m) {
this.n = n;
this.m = m;
for (int i = 1; i <=n ; i++) {
adj[i]=new ArrayList<>();
}
Arrays.fill(visited,1,n+1,false);
}
public static void main(String[] args) throws IOException {
adj=new ArrayList[MAX];
visited=new boolean[MAX];
new Thread(null, new Runnable() {
public void run() {
String s[];
br=new BufferedReader(new InputStreamReader(System.in));
int t= 0;
try {
t = Integer.parseInt(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
int n,m;
try {
while (t--> 0){
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
new FireEscapeRoutesDFS(n,m).solve();
}
} catch (IOException e) {
e.printStackTrace();
} catch (StackOverflowError e) {
System.out.println("RTE");
}
}
}, "1", 1 << 26).start();
}
void solve() throws IOException {
String s[];
int x,y,cc=0;
long prod=1;
for (int i = 0; i < m; i++) {
s=br.readLine().split("\\s");
x=Integer.parseInt(s[0]);
y=Integer.parseInt(s[1]);
// System.out.println(adj[x]+" "+adj[y]);
adj[x].add(y);
adj[y].add(x);
//System.out.println(adj[x]+" "+adj[y]);
}
/*
for (int i = 1; i <=n ; i++) {
System.out.println(adj[i]);
}*/
for (int u = 1; u <=n; u++) {
if (!visited[u]){
cc++;
size=0;
//System.out.println(u);
dfs(u);
prod*=size;
if (prod>=M){
prod%=M;
}
}
}
System.out.println(cc+" "+prod);
}
private void dfs(int u) {
visited[u]=true;
size++;
//System.out.println(u);
int s=adj[u].size();
for (int i = 0; i<s; i++) {
int v=adj[u].get(i);
if (!visited[v]){
dfs(v);
}
}
}
}