Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- public class nccc2s51 {
- public static class Edge1 {
- int ev, cost;
- public Edge1(int ev, int cost) {
- this.ev = ev; this.cost = cost;
- }
- }
- public static class Edge implements Comparable<Edge> {
- public int bv;
- public int ev;
- public int cost;
- public Edge(int bv, int ev, int cost) {
- this.bv = bv;
- this.ev = ev;
- this.cost = cost;
- }
- @Override
- public int compareTo(Edge o) {
- // TODO Auto-generated method stub
- return o.cost-this.cost;
- }
- }
- public static ArrayList<Edge> edges;
- public static ArrayList<Edge> temp;
- public static int V;
- public static int E;
- public static int[] parent;
- public static int[] rank;
- public static ArrayList<Edge1>[] adj;
- public static int bsearch(int l, int r, Edge v) {
- while(l <= r) {
- int mid = (l+r)/2;
- if(temp.get(mid).cost > v.cost) l=mid+1;
- else if(temp.get(mid).cost < v.cost) r = mid-1;
- else {
- if(temp.get(mid).cost > v.cost) r = mid-1;
- else if(temp.get(mid).cost < v.cost) l = mid+1;
- return mid;
- }
- }
- return l;
- }
- public static void update() {
- parent = new int[V+1]; rank = new int[V+1]; adj = new ArrayList[V+1];
- for(int j=1;j<=N;j++) {
- adj[j] = new ArrayList<p>();
- make(j);
- }
- mst();
- }
- public static void main(String[] args) throws Exception {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(System.in));
- String[] A = br.readLine().split(" ");
- V = Integer.parseInt(A[0]);
- E = Integer.parseInt(A[1]);
- edges = new ArrayList<Edge>();
- for(int i=0;i<E;i++) {
- A = br.readLine().split(" ");
- int bv = Integer.parseInt(A[0]);
- int ev = Integer.parseInt(A[1]);
- int cost = Integer.parseInt(A[2]);;
- edges.add(new Edge(bv, ev, cost));
- }
- temp = new ArrayList<Edge>(edges);
- Collections.sort(temp);
- int Q = Integer.parseInt(br.readLine());
- for(int i=0;i<Q;i++) {
- A = br.readLine().split(" ");
- int op = Integer.parseInt(A[0]);
- if(op == 1) {
- int m = Integer.parseInt(A[1]);
- int x = Integer.parseInt(A[2]);
- Edge r = edges.remove(m-1); //original edge
- Edge r2 = new Edge(r.bv,r.ev,x); //new edge
- edges.add(m-1, r2);
- int ind = bsearch(0, E-1, r); //from temp original edge location
- temp.remove(ind);
- ind = bsearch(0, E-2, r2);
- temp.add(ind, r2);
- update(); //max spaning tree
- // System.out.println("update check: " + edges.get(m-1).x + " " + edges.get(m-1).y + " " + edges.get(m-1).z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement