結果

問題 No.529 帰省ラッシュ
ユーザー tanzakutanzaku
提出日時 2017-02-07 23:42:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,076 ms / 4,500 ms
コード長 16,661 bytes
コンパイル時間 4,319 ms
コンパイル使用メモリ 96,088 KB
実行使用メモリ 137,080 KB
最終ジャッジ日時 2024-05-09 16:03:37
合計ジャッジ時間 23,282 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
46,844 KB
testcase_01 AC 105 ms
41,204 KB
testcase_02 AC 107 ms
41,436 KB
testcase_03 AC 123 ms
41,212 KB
testcase_04 AC 320 ms
46,716 KB
testcase_05 AC 334 ms
47,200 KB
testcase_06 AC 329 ms
47,240 KB
testcase_07 AC 334 ms
47,580 KB
testcase_08 AC 2,558 ms
86,048 KB
testcase_09 AC 2,560 ms
99,292 KB
testcase_10 AC 2,824 ms
116,260 KB
testcase_11 AC 2,688 ms
119,864 KB
testcase_12 AC 2,206 ms
90,304 KB
testcase_13 AC 2,388 ms
137,080 KB
testcase_14 AC 2,376 ms
95,140 KB
testcase_15 AC 2,764 ms
120,912 KB
testcase_16 AC 3,076 ms
130,624 KB
testcase_17 AC 2,717 ms
127,072 KB
testcase_18 AC 2,692 ms
121,976 KB
testcase_19 AC 2,508 ms
120,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class _484_InputTest {
	void range(int v, int low, int high) {
		if (v < low || v > high) {
			throw new RuntimeException();
		}
	}
	
	final Scanner in = new Scanner(System.in);
	int read1(String s) {
		if (!Character.isDigit(s.charAt(0))) throw new RuntimeException();
		if (!Character.isDigit(s.charAt(s.length() - 1))) throw new RuntimeException();
		return Integer.parseInt(s);
	}
	int[] readN(String s, int n) {
		if (!Character.isDigit(s.charAt(0))) throw new RuntimeException();
		if (!Character.isDigit(s.charAt(s.length() - 1))) throw new RuntimeException();
		
		String[] s2 = s.split(" ");
		if (s2.length != n) throw new RuntimeException();
		int[] res = new int[n];
		for (int i = 0; i < n; i++) res[i] = read1(s2[i]);
		return res;
	}

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int[] nmq = readN(in.nextLine(), 3);
			int n = nmq[0];
			int m = nmq[1];
			int Q = nmq[2];
			range(n, 2, 100000);
			range(m, 1, 200000);
			range(Q, 2, 200000);
			SimpleAdjListGraph g = new SimpleAdjListGraph(n, 2*m);
			for (int i = 0; i < m; i++) {
				int[] AB = readN(in.nextLine(), 2);
				int A = AB[0];
				int B = AB[1];
				range(A, 1, n);
				range(B, 1, n);
				g.addEdge(A - 1, B - 1);
				g.addEdge(B - 1, A - 1);
			}
			
			List<Integer> br = new ArrayList<>();
			List<List<Integer>> tecomp = new ArrayList<>();
			g.bridgeNoRec(br, tecomp);
			
			cmp = new int[n];
			for (int i = 0; i < tecomp.size(); i++) {
				for (int t : tecomp.get(i)) {
					cmp[t] = i;
				}
			}
			
			tree = new HeavyLightDecomposition(tecomp.size());
			for (int e : br) {
				tree.addEdge(cmp[g.to[e^0]], cmp[g.to[e^1]]);
				tree.addEdge(cmp[g.to[e^1]], cmp[g.to[e^0]]);
			}
			tree.init();
			
			set = new TreeSet[cmp.length];
			for (int i = 0; i < cmp.length; i++) {
				set[i] = new TreeSet<>();
				set[i].add(-1);
			}
			
			seg = new Seg2[tree.getHeavyEdgeCount()];
			for (int i = 0; i < tree.getHeavyEdgeCount(); i++) {
				seg[i] = new Seg2(tree.getHeavyEdgeLength(i));
				for (int j = 0; j < tree.getHeavyEdgeLength(i); j++) {
					seg[i].update(j, -1);
				}
			}

			TreeSet<Integer> duplicateCheck = new TreeSet<>();
			while (Q-- > 0) {
				int[] t3 = readN(in.nextLine(), 3);
				int t = t3[0];
				range(t, 1, 2);
				
				if (t == 1) {
					int u = t3[1];
					int w = t3[2];
					range(u, 1, n);
					range(w, 1, 1_000_000_000);
					
					if (!duplicateCheck.add(w)) {
						throw new RuntimeException();
					}
					
					int v = cmp[u-1];
					mp.remove(set[v].last());
					set[v].add(w);
					mp.put(set[v].last(), v);
					seg[tree.getHeavyEdgeId(v)].update(tree.getIndexInHeavyEdge(v), set[v].last());
					
				} else if (t == 2) {
					int S = t3[1];
					int T = t3[2];
					range(S, 1, n);
					range(T, 1, n);
					
					int ans = query(cmp[S-1], cmp[T-1]);
					remove(ans);
					System.out.println(ans);
				}
			}
		}
	}
	
	int query(int u, int v) {
		int ans = -1;
		while (true) {
			int hi = tree.getHeavyEdgeId(u);
			int hj = tree.getHeavyEdgeId(v);
			
			if (hi == hj) {
				int i0 = tree.getIndexInHeavyEdge(u);
				int i1 = tree.getIndexInHeavyEdge(v);
				int val = seg[hi].get(Math.min(i0, i1), Math.max(i0, i1) + 1);
				ans = Math.max(ans, val);
				break;
			}

			int di = tree.getHeavyEdgeDepth(hi);
			int dj = tree.getHeavyEdgeDepth(hj);
			
			if (di >= dj) {
				int val = seg[hi].get(0, tree.getIndexInHeavyEdge(u) + 1);
				ans = Math.max(ans, val);
				u = tree.getParentVertex(hi);
			} else {
				int val = seg[hj].get(0, tree.getIndexInHeavyEdge(v) + 1);
				ans = Math.max(ans, val);
				v = tree.getParentVertex(hj);
			}
		}
		return ans;
	}
	
	void remove(int ans) {
		if (ans == -1) return;
		int v = mp.get(ans);
		mp.remove(ans);
		set[v].pollLast();
		mp.put(set[v].last(), v);
		seg[tree.getHeavyEdgeId(v)].update(tree.getIndexInHeavyEdge(v), set[v].last());
	}
	
	void put(int v, int w) {
		mp.remove(set[v].last());
		set[v].add(w);
		mp.put(set[v].last(), v);
		seg[tree.getHeavyEdgeId(v)].update(tree.getIndexInHeavyEdge(v), set[v].last());
	}

	int[] cmp;
	HeavyLightDecomposition tree;
	TreeMap<Integer, Integer> mp = new TreeMap<>();
	TreeSet<Integer>[] set;
	Seg2[] seg;
	
	
	
	static public class HeavyLightDecomposition {
		final G g;
		
		public HeavyLightDecomposition(final int V) {
			g = new G(V, 2 * (V - 1));
		}
		
		void addEdge(int s, int t) {
			g.addEdge(s, t);
		}
		
		int getHeavyEdgeCount() {
			return g.heavyEdgeCount;
		}
		
		int getHeavyEdgeLength(int heavyEdgeId) {
			return g.heavyEdge[heavyEdgeId].length;
		}
		
		int getHeavyEdgeDepth(int heavyEdgeId) {
			return g.heavyEdgeDepth[heavyEdgeId];
		}
		
		int getHeavyEdgeId(int vertexIndex) {
			return g.belongHeavyEdgeIndex[vertexIndex];
		}
		
		int getIndexInHeavyEdge(int vertexIndex) {
			return g.heavyEdgeIndex[vertexIndex];
		}
		
		int getParentVertex(int heavyEdgeId) {
			return g.heavyEdgeParent[g.heavyEdge[heavyEdgeId][0]];
		}
		
		void init() { g.init(); }
		
		static
		class G {
			int m, V, E;
			int[] head, next, to, childSize, parent;
			int[] heavyEdgeIndex;
			int[] belongHeavyEdgeIndex;
			int[] heavyEdgeParent;
			int[][] heavyEdge;
			int[] heavyEdgeVertexCount;
			int[] heavyEdgeDepth;
			int heavyEdgeCount;

			boolean[] vis;
			int[] st;
			
			public G(int V, int E) {
				head = new int[V];
				childSize = new int[V];
				heavyEdgeIndex = new int[V];
				belongHeavyEdgeIndex = new int[V];
				heavyEdgeVertexCount = new int[V];
				heavyEdgeDepth = new int[V];
				heavyEdgeParent = new int[V];
				parent = new int[V];
				next = new int[E];
				to = new int[E];
				vis = new boolean[head.length];
				st = new int[head.length + 1];
				this.V = V;
				this.E = E;
				clear();
			}
			
			public void clear() {
				m = 0;
				Arrays.fill(head, -1);
			}

			public void addEdge(int s, int t) {
				next[m] = head[s];
				head[s] = m;
				to[m++] = t;
			}

			public void init() {
				calcChildSize(0);
				initHeavyEdge(0);
			}

			
			public void calcChildSize(final int root) {
				int sp = 0;

				parent[root] = -1;
				st[sp++] = root;
				while(sp != 0) {
					final int v = st[--sp];
					if(!vis[v]) {
						st[sp++] = v;
						childSize[v]++;
						vis[v] = true;
						for(int e = head[v]; e != -1; e = next[e]) {
							final int u = to[e];
							if(!vis[u]) {
								parent[u] = v;
								st[sp++] = u;
							}
						}
					}
					else if(parent[v] != -1) {
						childSize[parent[v]] += childSize[v];
					}
				}
			}

			public void initHeavyEdge(final int root) {
				int sp = 0;
				st[sp++] = root;
				Arrays.fill(vis, false);
				heavyEdgeIndex[root] = 0;
				belongHeavyEdgeIndex[root] = heavyEdgeCount++;
				heavyEdgeParent[root] = -1;
				while(sp != 0) {
					final int v = st[--sp];
					if(!vis[v]) {
						st[sp++] = v;
						vis[v] = true;

						heavyEdgeVertexCount[belongHeavyEdgeIndex[v]]++;
//						System.err.println(belongHeavyEdgeIndex[v] + " " + heavyEdgeVertexCount[belongHeavyEdgeIndex[v]]);

						int maxChildIndex = -1;
						int maxChildSize = 0;
						for(int e = head[v]; e != -1; e = next[e]) {
							final int u = to[e];
							if(!vis[u]) {
								if(maxChildSize < childSize[u]) {
									maxChildIndex = u;
									maxChildSize = childSize[u];
								}
//								System.err.println(v + " -> " + u + " " + maxChildIndex);
								st[sp++] = u;
							}
						}
						

						for(int e = head[v]; e != -1; e = next[e]) {
							final int u = to[e];
							if(!vis[u] && maxChildIndex != u) {
								heavyEdgeIndex[u] = 0;
								heavyEdgeDepth[heavyEdgeCount] = heavyEdgeDepth[belongHeavyEdgeIndex[v]] + 1;
//								heavyEdgeVertexCount[heavyEdgeCount] = 1;
								belongHeavyEdgeIndex[u] = heavyEdgeCount++;
								heavyEdgeParent[u] = v;
							}
						}
						
						if(maxChildIndex != -1) {
							heavyEdgeIndex[maxChildIndex] = heavyEdgeIndex[v] + 1;
							belongHeavyEdgeIndex[maxChildIndex] = belongHeavyEdgeIndex[v];
							heavyEdgeParent[maxChildIndex] = heavyEdgeParent[v];
						}
					}
				}
				
				heavyEdge = new int[heavyEdgeCount][];
				for(int i = 0; i < heavyEdgeCount; i++) {
					heavyEdge[i] = new int[heavyEdgeVertexCount[i]];
				}
				
				for(int i = 0; i < V; i++) {
					final int j = belongHeavyEdgeIndex[i];
//					System.err.println(i + " " + j + " " + heavyEdgeIndex[i] + " " + heavyEdge[j].length);
					heavyEdge[j][heavyEdgeIndex[i]] = i;
				}
			}
		}

	}
	
	static class Seg2 {
		final int n;
		final int[] seg;
		
		public Seg2(final int n) {
			this.n = Integer.highestOneBit(n) << 1;
			seg = new int[this.n << 1];
		}

		int get(int l, int r) { return get(l, r, 0, 0, n); }
		int get(int l, int r, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return -1;
			if(l <= curL && curR <= r) { return seg[k]; }
			final int curM = (curL + curR) / 2;
			int lv = get(l, r, 2 * k + 1, curL, curM);
			int rv = get(l, r, 2 * k + 2, curM, curR);
			return Math.max(lv, rv);
		}
		
		void update(int i, int v) {
			i += n - 1;
			seg[i] = v;
			while(i != 0) {
				i = (i - 1) / 2;
				seg[i] = Math.max(seg[2*i+1], seg[2*i+2]);
			}
		}
	}


	static class SimpleAdjListGraph {
		int m, V, E;
		int[] head, next, to;
	
		public SimpleAdjListGraph(int V, int E) {
			head = new int[V];
			next = new int[E];
			to = new int[E];
			this.V = V;
			this.E = E;
			clear();
		}
	
		public void clear() {
			m = 0;
			Arrays.fill(head, -1);
		}
	
		public void addEdge(int s, int t) {
			next[m] = head[s];
			head[s] = m;
			to[m++] = t;
		}

		// 橋,二重辺連結成分分解(非再帰版)
		// http://www.prefield.com/algorithm/graph/bridge.html
		List<Integer> br;
		List<List<Integer>> tecomp;
		Stack<Integer> roots;
		Stack<Integer> S;
		boolean[] inS;
		int[] num;
		int[] stv;
		int[] stu;
		int[] stt;
		int[] parent;
		void bridgeNoRec(List<Integer> br, List<List<Integer>> tecomp) {
			this.br = br;
			this.tecomp = tecomp;
			final int n = head.length;
			num = new int[n];
			inS = new boolean[n];
			roots = new Stack<Integer>();
			S = new Stack<Integer>();
			stv = new int[V+2*E];
			stu = new int[V+2*E];
			stt = new int[V+2*E];
			parent = new int[V];
			int time = 0;
			for(int i = 0; i < n; i++) if(num[i] == 0) {
				visitBridgeNoRec(i, n, time);
				br.remove(br.size() - 1);
			}
		}
		
		private void visitBridgeNoRec(int v, int u, int time) {
			int sp = 0;

			stv[sp] = v;
			stu[sp] = u;
			stt[sp] = time;
			parent[sp] = -1;
			sp++;
			
			while(sp != 0) {
				sp--;
				time = stt[sp];
				u = stu[sp];
				v = stv[sp];
				
				if(num[v] != 0) {
					int eu = -1;
					for(int e = head[v]; e != -1; e = next[e]) {
						int w = to[e];
						
						if(w == u) { eu = e; }
						if(parent[w] != v && u != w && inS[w]) {
							while(num[roots.peek()] > num[w]) {
								roots.pop();
							}
						}
					}
					if(v == roots.peek()) {
						br.add(eu);
						List<Integer> xs = new ArrayList<Integer>();
						while(true) {
							int w = S.pop();
							inS[w] = false;
							xs.add(w);
							if(v == w) break;
						}
						tecomp.add(xs);
						roots.pop();
					}
				}
				else {
					num[v] = ++time;
					S.push(v);
					inS[v] = true;
					roots.push(v);
					
					stv[sp] = v;
					stu[sp] = u;
					stt[sp] = time;
					sp++;

					for(int e = head[v]; e != -1; e = next[e]) {
						int w = to[e];
						if(num[w] == 0) {
							stv[sp] = w;
							stu[sp] = v;
							stt[sp] = time;
							parent[w] = v;
							sp++;
//							visitBridge(w, v, br, tecomp, roots, S, inS, num, time);
						}
					}
				}
			}
		}
	}
	
	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
	}
	public static void main(String[] args) throws IOException { new _484_InputTest().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		private PrintWriter out = new PrintWriter(System.out);

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}
0