Background
Xs high school chemistry competition team coach is a love for hearth stone.
One day, he takes an invigilation while rubbing the hearth stone, and you, as a great god of Information competitions, have come to join us.
However, your competing friends turned to you for help.
"How to Do question 1,354th" <-- sign language he asked.
Description
You can refer to the following question: Given a hydrocarbon, it only contains a single key (A Comprehension explanation for junior high school students: a pile of carbon is connected with a horizontal line, and the horizontal line is a single line ).
Then ragranos, the king of phillon, used his flame to purify all the rings (???). All the ring carbon is converted into one carbon ..
Specify multiple groups of carbon and find the total amount of carbon between them. (It does not matter ).
But because you are taking the test, you can only tell your friends the answer in sign language. You decided to use binary to represent the final answer. (Don't care. It's irrelevant to the question ).
Input/Output Format
Input Format:
The first line contains two integers n, m. indicating that there are N points and M root keys.
In the next m row, there are two integers U in each row. V indicates that there is a key for carbon U and carbon v.
The next integer tot represents the number of queries
Next, the tot row contains two integers, A and B, indicating the numbers of the two carbon numbers.
Output Format:
Total tot rows
One binary number per line
Input and Output sample input sample:
3 21 22 321 22 3
Output example:
1010
Double connected component + LCA for distance
I don't know why it's a black question. I can even use a small hacker like me ..
It mainly involves finding the distance between Tarjan and LCA and rebuilding the point.
Directly Add code
# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <algorithm> # include <cmath> # include <vector> # include <stack> using namespace STD; inline int read () {int x = 0; bool F = 1; char c = getchar (); While (! Isdigit (c) {If (C = '-') f = 0; C = getchar ();} while (isdigit (c )) {x = x * 10 + C-'0'; C = getchar ();} If (! F) return 0-x; return X;} const int M = 10010; const int n = 50010; vector <int> E [m]; // No permission graph, directly store the vector <int> G [m]; int n, m, TOT, dfn [m], low [m], vis [m], color [m], tim, Megumi, X [N], Y [N], F [m] [25], depth [m], Qaq; stack <int> S; inline void Tarjan (int u, int FA) {dfn [u] = low [u] = ++ Tim; S. push (U); For (INT I = 0; I <E [u]. size (); I ++) {int v = E [u] [I]; If (V = FA) continue; // double connected component if (! Dfn [v]) {Tarjan (v, U); low [u] = min (low [u], low [v]);} else if (! Color [v]) low [u] = min (low [u], dfn [v]);} If (low [u] = dfn [u]) {++ Megumi; while (S. top ()! = U) {color [S. top ()] = Megumi; S. pop ();} color [S. top ()] = Megumi; S. pop () ;}} inline void rebuild () {for (INT I = 1; I <= m; I ++) {If (color [x [I] ^ color [Y [I]) g [color [x [I]. push_back (color [Y [I]), g [color [Y [I]. push_back (color [x [I]); // The value must be color [x [I]. It has been stuck for half an hour qwq} inline void DFS (INT U, int FA) {If (vis [u]) return; vis [u] = 1; depth [u] = depth [fa] + 1; for (INT I = 1; I <= 20; I ++) {f [u] [I] = f [f [u] [I-1] [I-1];} For (INT I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; If (V = FA) continue; F [v] [0] = u; DFS (v, u) ;}} inline int LCA (int A, int B) {If (depth [a] <depth [B]) Swap (A, B ); for (INT I = 20; I> = 0; I --) {If (depth [f [a] [I]> = depth [B]) A = f [a] [I];} if (a = B) return a; For (INT I = 20; I> = 0; I --) {If (F [a] [I]! = F [B] [I]) {A = f [a] [I], B = f [B] [I];} return f [a] [0];} inline void print (int x) {// print binary if (! X) return; print (x> 1); printf ("% d", X & 1);} int main () {// freopen ("Haha. in "," r ", stdin); n = read (), M = read (); For (INT I = 1; I <= m; I ++) {x [I] = read (), Y [I] = read (); e [x [I]. push_back (Y [I]); e [Y [I]. push_back (X [I]) ;}for (INT I = 1; I <= N; I ++) {If (! Dfn [I]) Tarjan (I, I) ;}rebuild (); Megumi ++; For (INT I = 1; I ^ Megumi; I ++) {If (! Vis [I]) DFS (I, I);} tot = read (); For (INT I = 1; I <= tot; I ++) {int X, y; X = read (), Y = read (); int L = LCA (color [X], color [y]); print (depth [color [x]-depth [l] + depth [color [y]-depth [l] + 1); printf ("\ n ");} // fclose (stdin); Return Qaq ;}
[Luogu p2783] God of Organic Chemistry (the blackest question)