File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int [] parent;
7+
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ int G = Integer . parseInt(br. readLine());
11+ int P = Integer . parseInt(br. readLine());
12+
13+ parent = new int [G + 1 ];
14+ for (int i = 1 ; i <= G ; i++ ) {
15+ parent[i] = i;
16+ }
17+
18+ int count = 0 ;
19+ for (int i = 0 ; i < P ; i++ ) {
20+ int gi = Integer . parseInt(br. readLine());
21+
22+ int availableGate = find(gi);
23+
24+ if (availableGate == 0 ) break ;
25+
26+ count++ ;
27+
28+ union(availableGate, availableGate - 1 );
29+ }
30+
31+ System . out. println(count);
32+ }
33+
34+ static int find (int x ) {
35+ if (parent[x] == x) return x;
36+ return parent[x] = find(parent[x]);
37+ }
38+
39+ static void union (int x , int y ) {
40+ x = find(x);
41+ y = find(y);
42+ if (x != y) {
43+ parent[x] = y;
44+ }
45+ }
46+ }
47+ ```
You can’t perform that action at this time.
0 commit comments