Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pi;
- typedef vector<int> vi;
- /*
- site: HackerRank
- problem: Sherlock and Queries on the Graph
- najdi ja tetratkata so bomba, pisuva "POWER" vo portokalovo i "time" vo belo na crna pozadina
- resenieto na list e posle esejot za makedonski
- se otkazav od pisuvanje
- */
- const int MAXN = 100005;
- const int MAXLOG = 20;
- int N, M, Q;
- vector<vector<int>> G[MAXN];
- int numcalls;
- int num[MAXN], low[MAXN];
- vector<pair<int, int>> bridges;
- unordered_set<int> blocked[MAXN];
- void artb(int nd, int prnt) {
- num[nd] = low[nd] = numcalls++;
- for (int to : G[nd]) {
- if (num[to] == -1) {
- artb(to, nd);
- if (low[to] > num[nd]) { // bridge
- bridges.emplace_back(nd, to);
- blocked[nd].insert(to);
- blocked[to].insert(nd);
- }
- low[nd] = min(low[nd], low[to]);
- } else if (to != prnt) {
- low[nd] = min(low[nd], num[to]);
- }
- }
- }
- int numcc;
- int ccid[MAXN];
- vector<vector<int>> H[MAXN];
- void makecc(int nd) {
- ccid[nd] = numcc;
- for (int to : G[nd]) {
- if (blocked[nd].count(to) == 0) {
- makecc(to);
- }
- }
- }
- int depth[MAXN];
- int P[MAXN][MAXLOG];
- void getdepth(int u, int p, int d) {
- depth[u] = d;
- for (int v : H[u]) {
- if (v != p) {
- P[v][0] = u;
- getdepth(v, u, d+1);
- }
- }
- }
- struct lcadata {
- int node, int dist1, int dist2;
- lcadata(int a, int b, int c) {
- node = a, dist1 = b, dist2 = c;
- }
- lca() {}
- };
- lcadata LCA(int a, int b) {
- bool swapped = false;
- if (depth[a] < depth[b]) {
- swapped = true;
- swap(a, b);
- }
- lcadata res(0, 0, 0);
- for (int j = MAXLOG-1; j >= 0; --j) {
- if (depth[a] - (1 << j) >= depth[b]) {
- res.dist1 += (1 << j);
- a = P[a][j];
- }
- }
- if (swapped) swap(res.dist1, res.dist2);
- if (a == b) return res;
- for (int j = MAXLOG-1; j >= 0; --j) {
- if (P[a][j] != -1 && P[a][j] != P[b][j]) {
- res.dist1 += (1 << j);
- res.dist2 += (1 << j);
- a = P[a][j];
- b = P[b][j];
- }
- }
- res.dist1 += 1;
- res.dist2 += 1;
- res.node = P[a][0];
- return res;
- }
- int ccnd1, ccnd2, cclca;
- int firstnode(int a) {
- int b = a, j = 0;
- while (true) {
- if (incc(b)) {
- }
- }
- }
- void Main() {
- cin >> N >> M >> Q;
- for (int i = 0; i < M; ++i) {
- int a, b;
- cin >> a >> b;
- G[a].push_back(b);
- G[b].push_back(a);
- }
- memset(num, -1, sizeof(num));
- numcalls = 0;
- for (int i = 1; i <= N; ++i) {
- if (num[i] == -1) {
- artb(i, -1);
- }
- }
- memset(ccid, -1, sizeof(ccid));
- numcc = 0;
- for (auto& edge : bridges) {
- int u = edge.first, v = edge.second;
- if (ccid[u] == -1) {
- makecc(u);
- ++numcc;
- }
- if (ccid[v] == -1) {
- makecc(v);
- ++numcc;
- }
- H[ccid[u]].push_back(ccid[v]);
- H[ccid[v]].push_back(ccid[u]);
- }
- memset(P, -1, sizeof(p));
- getdepth(0, -1, 0);
- for (int j = 1; j < MAXLOG; ++j) {
- for (int i = 0; i < numcc; ++i) {
- if (P[i][j-1] != -1) {
- P[i][j] = P[ P[i][j-1] ][j-1];
- }
- }
- }
- while (Q--) {
- int a, b, c, d;
- cin >> a >> b >> c >> d;
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- #ifdef _DEBUG
- // freopen("in.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- #endif
- Main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement