File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed
Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+ static long [] dp;
20+ static long [] s;
21+ static int [] root;
22+ static int N , M ;
23+ static final long mod = (int )1e9 + 7 ;
24+
25+ static int f(int x) { return x== root[x] ? x : (root[x]= f(root[x])); }
26+
27+ public static void main(String [] args) throws Exception {
28+
29+ ready();
30+ solve();
31+
32+ bwEnd();
33+ }
34+
35+ static void ready() throws Exception {
36+
37+ nextLine();
38+ N = nextInt();
39+ M = nextInt();
40+ dp = new long [N + 1 ];
41+ s = new long [N + 1 ];
42+ root = new int [N + 1 ];
43+ for (int i= 1 ;i<= N ;i++ ) root[i] = i;
44+
45+ }
46+
47+ static void solve() throws Exception {
48+
49+ dp[1 ] = 1 ;
50+ s[1 ] = 1 ;
51+ for (int i= 2 ;i<= N ;i++ ) {
52+ long [] ndp = new long [N + 1 ];
53+ ndp[1 ] = 1 ;
54+ ndp[i] = 1 ;
55+ for (int j= 2 ;j< i;j++ ) {
56+ ndp[j] = (dp[j] * j + dp[j- 1 ]) % mod;
57+ }
58+
59+ dp = ndp;
60+ for (int j= 1 ;j<= i;j++ ) {
61+ s[i] = (s[i] + dp[j]) % mod;
62+ }
63+ }
64+
65+ while (M -- > 0 ) {
66+ nextLine();
67+ int a = nextInt(), b = nextInt();
68+ int x = f(a), y = f(b);
69+ if (x != y) {
70+ N -- ;
71+ root[x] = y;
72+ }
73+ bw. write(s[N ] + " \n " );
74+ }
75+
76+ }
77+
78+ }
79+
80+ ```
You can’t perform that action at this time.
0 commit comments