1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <iomanip> using namespace std;
const int MAXN = 10010; const int MAXM = 50010; const int INF = 0x3f3f3f3f;
class LFS { public: LFS() { memset(head, -1, sizeof head); ecnt = 0; n = 0; } LFS(int N) { memset(head, -1, sizeof head); ecnt = 0; n = N; } void adde(int from, int to, int w) { e[ecnt].to = to; e[ecnt].w = w; e[ecnt].next = head[from]; head[from] = ecnt++; } void addde(int a, int b, int w) { adde(a, b, w); adde(b, a, w); }
protected: struct Edge { int to, next, w; } e[MAXM * 2]; int head[MAXN]; int ecnt; int n;
private: virtual void dfs(int u, int fa) { for (int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; if (v != fa) { dfs(v, u); } } } };
#include <stack>
class SCC_Tarjan : public LFS { public: int scccnt; int belong[MAXN];
SCC_Tarjan(int n) : LFS(n) { memset(dfn, -1, sizeof dfn); memset(low, -1, sizeof low); memset(ins, false, sizeof ins); memset(belong,0,sizeof belong); tim = 1; scccnt = 0; }
void solve() { for(int i = 1;i<=n;i++){ if(dfn[i] == -1){ tarjan(i,0); } } }
void createnew(LFS* map){ for(int i = 1;i<=n;i++){ for(int j = head[i];j!=-1;j = e[j].next){ int u = belong[i]; int v = belong[e[j].to]; if(u != v){ map->adde(u,v,e[j].w); } } } }
protected: stack<int> s; bool ins[MAXN]; int mina[MAXN]; int low[MAXN], dfn[MAXN]; int tim; void tarjan(int u,int fa) { dfn[u] = low[u] = tim++; s.push(u); ins[u] = true; for (int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; if (fa == v){ continue; } if (dfn[v] == -1) { tarjan(v,u); low[u] = min(low[u], low[v]); } else { if (ins[v]) { low[u] = min(low[u], dfn[v]); } } } if (dfn[u] == low[u]) { scccnt++; int v = 0; while (v != u) { v = s.top(); s.pop(); ins[v] = false; belong[v] = scccnt; } } } };
class LCA : public LFS{ public: int dep[MAXN]; LCA(int n) : LFS(n) { memset(dep, -1, sizeof dep); } void pre(int rt = 1) { dfs(rt, 1, 0); } int querylca(int a, int b) { if (dep[a] > dep[b]) swap(a, b); int h = dep[b] - dep[a]; for (int i = 20; i >= 0; i--) { if(h & (1 << i)) { b = f[b][i]; } } if (a == b) return a; for (int i = 20; i >= 0; i--) { if (f[a][i] == f[b][i]) continue; a = f[a][i]; b = f[b][i]; } return f[a][0]; }
protected: int f[MAXN][22];
private: void dfs(int u, int d, int fa) { dep[u] = d; f[u][0] = fa; for (int i = 1; i < 21; i++) { f[u][i] = f[f[u][i - 1]][i - 1]; } for (int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; if (dep[v] == -1) { dfs(v, d + 1, u); } } } };
#include <cctype> #include <cstdio>
inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { X = (X << 3) + (X << 1) + (ch ^ 48); ch = getchar(); } return w ? -X : X; }
int main(){ int n = read(); int m = read(); SCC_Tarjan *all = new SCC_Tarjan(n); for(int i = 1;i<=m;i++){ int u = read(); int v = read(); all->addde(u,v,1); } all->solve(); LCA* map = new LCA(all->scccnt); all->createnew(map); map->pre(); int tcnt = read(); for(int i = 1;i<=tcnt;i++){ int a,b; a = all->belong[read()]; b = all->belong[read()]; int lca = map->querylca(a,b); int ans = map->dep[a] + map->dep[b] - map->dep[lca]*2 + 1; char tmp[64] = {0}; int pos = 0; while (ans){ tmp[pos++] = (((bool)(ans & 1)) + '0'); ans >>= 1; } for(;pos>0;pos--){ putchar(tmp[pos-1]); } putchar('\n'); } }
|