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