Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class k {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n, m, q;
- n = sc.nextInt();
- m = sc.nextInt();
- q = sc.nextInt();
- DisjointUnionSets uf = new DisjointUnionSets(n);
- int[] price = new int[n];
- for (int i = 0; i < price.length; i++) {
- price[i] = sc.nextInt();
- }
- for (int i = 0; i < m; i++) {
- int u, v;
- u = sc.nextInt() - 1;
- v = sc.nextInt() - 1;
- uf.union(u, v);
- }
- Map<Integer, GFG> mp = new HashMap<>();
- for (int i = 0; i < n; i++) {
- if (!mp.containsKey(uf.find(i))) {
- GFG hp = new GFG(n);
- mp.put(uf.find(i), hp);
- }
- GFG hp = mp.get(uf.find(i));
- hp.insert(price[i]);
- }
- for (int i = 0; i < q; i++) {
- int t, u, v;
- t = sc.nextInt();
- u = sc.nextInt() - 1;
- v = sc.nextInt() - 1;
- if (t == 1) {
- GFG hp = mp.get(uf.find(u));
- hp.delete(price[u], 1);
- price[u] = v + 1;
- hp.insert(price[u]);
- } else {
- if (uf.find(u) == uf.find(v)) {
- System.out.println(0);
- } else {
- GFG hp1 = mp.get(uf.find(u));
- GFG hp2 = mp.get(uf.find(v));
- int frs = hp1.peek();
- int scnd = hp2.peek();
- System.out.println(frs + scnd);
- }
- }
- }
- sc.close();
- }
- }
- // A Java program to implement Disjoint Set Data
- // Structure.
- class DisjointUnionSets {
- int[] rank, parent;
- int n;
- int numsets;
- // Constructor
- public DisjointUnionSets(int n)
- {
- rank = new int[n];
- parent = new int[n];
- this.n = n;
- numsets = n;
- makeSet();
- }
- // Creates n sets with single item in each
- void makeSet()
- {
- for (int i = 0; i < n; i++) {
- // Initially, all elements are in
- // their own set.
- parent[i] = i;
- }
- }
- // Returns representative of x's set
- int find(int x)
- {
- // Finds the representative of the set
- // that x is an element of
- if (parent[x] != x) {
- // if x is not the parent of itself
- // Then x is not the representative of
- // his set,
- parent[x] = find(parent[x]);
- // so we recursively call Find on its parent
- // and move i's node directly under the
- // representative of this set
- }
- return parent[x];
- }
- // Unites the set that includes x and the set
- // that includes x
- void union(int x, int y)
- {
- // Find representatives of two sets
- int xRoot = find(x), yRoot = find(y);
- // Elements are in the same set, no need
- // to unite anything.
- if (xRoot == yRoot)
- return;
- // If x's rank is less than y's rank
- if (rank[xRoot] < rank[yRoot])
- // Then move x under y so that depth
- // of tree remains less
- parent[xRoot] = yRoot;
- // Else if y's rank is less than x's rank
- else if (rank[yRoot] < rank[xRoot])
- // Then move y under x so that depth of
- // tree remains less
- parent[yRoot] = xRoot;
- else // if ranks are the same
- {
- // Then move y under x (doesn't matter
- // which one goes where)
- parent[yRoot] = xRoot;
- // And increment the result tree's
- // rank by 1
- rank[xRoot] = rank[xRoot] + 1;
- }
- numsets--;
- }
- }
- // Java Program to Implement Heaps
- // by Illustrating Min Heap
- // Main class (MinHeap)
- class GFG {
- // Member variables of this class
- private int[] Heap;
- private int size;
- private int maxsize;
- // Initializing front as static with unity
- private static final int FRONT = 1;
- // Constructor of this class
- public GFG(int maxsize)
- {
- // This keyword refers to current object itself
- this.maxsize = maxsize;
- this.size = 0;
- Heap = new int[this.maxsize + 5];
- Heap[0] = Integer.MIN_VALUE;
- }
- // Method 1
- // Returning the position of
- // the parent for the node currently
- // at pos
- private int parent(int pos) { return pos / 2; }
- // Method 2
- // Returning the position of the
- // left child for the node currently at pos
- private int leftChild(int pos) { return (2 * pos); }
- // Method 3
- // Returning the position of
- // the right child for the node currently
- // at pos
- private int rightChild(int pos)
- {
- return (2 * pos) + 1;
- }
- // Method 4
- // Returning true if the passed
- // node is a leaf node
- private boolean isLeaf(int pos)
- {
- if (pos > (size / 2)) {
- return true;
- }
- return false;
- }
- // Method 5
- // To swap two nodes of the heap
- private void swap(int fpos, int spos)
- {
- int tmp;
- tmp = Heap[fpos];
- Heap[fpos] = Heap[spos];
- Heap[spos] = tmp;
- }
- // Method 6
- // To heapify the node at pos
- private void minHeapify(int pos)
- {
- if(!isLeaf(pos)){
- int swapPos= pos;
- // swap with the minimum of the two children
- // to check if right child exists. Otherwise default value will be '0'
- // and that will be swapped with parent node.
- if(rightChild(pos)<=size)
- swapPos = Heap[leftChild(pos)]<Heap[rightChild(pos)]?leftChild(pos):rightChild(pos);
- else
- swapPos= Heap[leftChild(pos)];
- if(Heap[pos]>Heap[leftChild(pos)]
- || ((rightChild(pos) <=size) && Heap[pos]> Heap[rightChild(pos)])){
- swap(pos,swapPos);
- minHeapify(swapPos);
- }
- }
- }
- // Method 7
- // To insert a node into the heap
- public void insert(int element)
- {
- Heap[++size] = element;
- int current = size;
- while (Heap[current] < Heap[parent(current)]) {
- swap(current, parent(current));
- current = parent(current);
- }
- }
- // Method 9
- // To remove and return the minimum
- // element from the heap
- public int remove()
- {
- int popped = Heap[FRONT];
- Heap[FRONT] = Heap[size--];
- minHeapify(FRONT);
- return popped;
- }
- public int peek() {
- return Heap[FRONT];
- }
- public void delete(int key, int pos) {
- if (pos > size) {
- return;
- }
- if (Heap[pos] == key) {
- // delete
- Heap[pos] = Heap[size--];
- minHeapify(pos);
- } else if (Heap[pos] > key) {
- delete(key, leftChild(pos));
- } else {
- delete(key, rightChild(pos));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement