Advertisement
ogiorgil

Untitled

Jan 21st, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class k {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n, m, q;
  7.  
  8. n = sc.nextInt();
  9. m = sc.nextInt();
  10. q = sc.nextInt();
  11.  
  12. DisjointUnionSets uf = new DisjointUnionSets(n);
  13.  
  14. int[] price = new int[n];
  15.  
  16. for (int i = 0; i < price.length; i++) {
  17. price[i] = sc.nextInt();
  18. }
  19.  
  20. for (int i = 0; i < m; i++) {
  21. int u, v;
  22. u = sc.nextInt() - 1;
  23. v = sc.nextInt() - 1;
  24. uf.union(u, v);
  25. }
  26.  
  27.  
  28. Map<Integer, GFG> mp = new HashMap<>();
  29. for (int i = 0; i < n; i++) {
  30. if (!mp.containsKey(uf.find(i))) {
  31. GFG hp = new GFG(n);
  32. mp.put(uf.find(i), hp);
  33. }
  34. GFG hp = mp.get(uf.find(i));
  35. hp.insert(price[i]);
  36. }
  37.  
  38. for (int i = 0; i < q; i++) {
  39. int t, u, v;
  40. t = sc.nextInt();
  41. u = sc.nextInt() - 1;
  42. v = sc.nextInt() - 1;
  43.  
  44. if (t == 1) {
  45. GFG hp = mp.get(uf.find(u));
  46. hp.delete(price[u], 1);
  47. price[u] = v + 1;
  48. hp.insert(price[u]);
  49. } else {
  50. if (uf.find(u) == uf.find(v)) {
  51. System.out.println(0);
  52. } else {
  53. GFG hp1 = mp.get(uf.find(u));
  54. GFG hp2 = mp.get(uf.find(v));
  55. int frs = hp1.peek();
  56. int scnd = hp2.peek();
  57. System.out.println(frs + scnd);
  58. }
  59. }
  60. }
  61. sc.close();
  62. }
  63. }
  64.  
  65. // A Java program to implement Disjoint Set Data
  66. // Structure.
  67.  
  68.  
  69. class DisjointUnionSets {
  70. int[] rank, parent;
  71. int n;
  72. int numsets;
  73.  
  74. // Constructor
  75. public DisjointUnionSets(int n)
  76. {
  77. rank = new int[n];
  78. parent = new int[n];
  79. this.n = n;
  80. numsets = n;
  81. makeSet();
  82. }
  83.  
  84. // Creates n sets with single item in each
  85. void makeSet()
  86. {
  87. for (int i = 0; i < n; i++) {
  88. // Initially, all elements are in
  89. // their own set.
  90. parent[i] = i;
  91. }
  92. }
  93.  
  94. // Returns representative of x's set
  95. int find(int x)
  96. {
  97. // Finds the representative of the set
  98. // that x is an element of
  99. if (parent[x] != x) {
  100. // if x is not the parent of itself
  101. // Then x is not the representative of
  102. // his set,
  103. parent[x] = find(parent[x]);
  104.  
  105. // so we recursively call Find on its parent
  106. // and move i's node directly under the
  107. // representative of this set
  108. }
  109.  
  110. return parent[x];
  111. }
  112.  
  113. // Unites the set that includes x and the set
  114. // that includes x
  115. void union(int x, int y)
  116. {
  117. // Find representatives of two sets
  118. int xRoot = find(x), yRoot = find(y);
  119.  
  120. // Elements are in the same set, no need
  121. // to unite anything.
  122. if (xRoot == yRoot)
  123. return;
  124.  
  125. // If x's rank is less than y's rank
  126. if (rank[xRoot] < rank[yRoot])
  127.  
  128. // Then move x under y so that depth
  129. // of tree remains less
  130. parent[xRoot] = yRoot;
  131.  
  132. // Else if y's rank is less than x's rank
  133. else if (rank[yRoot] < rank[xRoot])
  134.  
  135. // Then move y under x so that depth of
  136. // tree remains less
  137. parent[yRoot] = xRoot;
  138.  
  139. else // if ranks are the same
  140. {
  141. // Then move y under x (doesn't matter
  142. // which one goes where)
  143. parent[yRoot] = xRoot;
  144.  
  145. // And increment the result tree's
  146. // rank by 1
  147. rank[xRoot] = rank[xRoot] + 1;
  148. }
  149. numsets--;
  150. }
  151. }
  152.  
  153.  
  154. // Java Program to Implement Heaps
  155. // by Illustrating Min Heap
  156.  
  157. // Main class (MinHeap)
  158. class GFG {
  159.  
  160. // Member variables of this class
  161. private int[] Heap;
  162. private int size;
  163. private int maxsize;
  164.  
  165. // Initializing front as static with unity
  166. private static final int FRONT = 1;
  167.  
  168. // Constructor of this class
  169. public GFG(int maxsize)
  170. {
  171.  
  172. // This keyword refers to current object itself
  173. this.maxsize = maxsize;
  174. this.size = 0;
  175.  
  176. Heap = new int[this.maxsize + 5];
  177. Heap[0] = Integer.MIN_VALUE;
  178. }
  179.  
  180. // Method 1
  181. // Returning the position of
  182. // the parent for the node currently
  183. // at pos
  184. private int parent(int pos) { return pos / 2; }
  185.  
  186. // Method 2
  187. // Returning the position of the
  188. // left child for the node currently at pos
  189. private int leftChild(int pos) { return (2 * pos); }
  190.  
  191. // Method 3
  192. // Returning the position of
  193. // the right child for the node currently
  194. // at pos
  195. private int rightChild(int pos)
  196. {
  197. return (2 * pos) + 1;
  198. }
  199.  
  200. // Method 4
  201. // Returning true if the passed
  202. // node is a leaf node
  203. private boolean isLeaf(int pos)
  204. {
  205.  
  206. if (pos > (size / 2)) {
  207. return true;
  208. }
  209.  
  210. return false;
  211. }
  212.  
  213. // Method 5
  214. // To swap two nodes of the heap
  215. private void swap(int fpos, int spos)
  216. {
  217.  
  218. int tmp;
  219. tmp = Heap[fpos];
  220.  
  221. Heap[fpos] = Heap[spos];
  222. Heap[spos] = tmp;
  223. }
  224.  
  225. // Method 6
  226. // To heapify the node at pos
  227. private void minHeapify(int pos)
  228. {
  229. if(!isLeaf(pos)){
  230. int swapPos= pos;
  231. // swap with the minimum of the two children
  232. // to check if right child exists. Otherwise default value will be '0'
  233. // and that will be swapped with parent node.
  234. if(rightChild(pos)<=size)
  235. swapPos = Heap[leftChild(pos)]<Heap[rightChild(pos)]?leftChild(pos):rightChild(pos);
  236. else
  237. swapPos= Heap[leftChild(pos)];
  238.  
  239. if(Heap[pos]>Heap[leftChild(pos)]
  240. || ((rightChild(pos) <=size) && Heap[pos]> Heap[rightChild(pos)])){
  241. swap(pos,swapPos);
  242. minHeapify(swapPos);
  243. }
  244.  
  245. }
  246. }
  247.  
  248. // Method 7
  249. // To insert a node into the heap
  250. public void insert(int element)
  251. {
  252.  
  253. Heap[++size] = element;
  254. int current = size;
  255.  
  256. while (Heap[current] < Heap[parent(current)]) {
  257. swap(current, parent(current));
  258. current = parent(current);
  259. }
  260. }
  261.  
  262.  
  263. // Method 9
  264. // To remove and return the minimum
  265. // element from the heap
  266. public int remove()
  267. {
  268.  
  269. int popped = Heap[FRONT];
  270. Heap[FRONT] = Heap[size--];
  271. minHeapify(FRONT);
  272.  
  273. return popped;
  274. }
  275.  
  276. public int peek() {
  277. return Heap[FRONT];
  278. }
  279.  
  280. public void delete(int key, int pos) {
  281. if (pos > size) {
  282. return;
  283. }
  284. if (Heap[pos] == key) {
  285. // delete
  286. Heap[pos] = Heap[size--];
  287. minHeapify(pos);
  288. } else if (Heap[pos] > key) {
  289. delete(key, leftChild(pos));
  290. } else {
  291. delete(key, rightChild(pos));
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement