QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18131 | #2583. 战略游戏 | ETK | 100 ✓ | 1243ms | 54216kb | C++23 | 2.2kb | 2022-01-16 14:50:42 | 2024-07-05 04:35:15 |
Judging History
answer
#include <algorithm>
#include <cstdio>
#include <vector>
const int MN = 100005;
int N, M, Q, cnt;
std::vector<int> G[MN], T[MN * 2];
int dfn[MN * 2], low[MN], dfc;
int stk[MN], tp;
void Tarjan(int u) {
low[u] = dfn[u] = ++dfc;
stk[++tp] = u;
for (int v : G[u]) {
if (!dfn[v]) {
Tarjan(v);
low[u] = std::min(low[u], low[v]);
if (low[v] == dfn[u]) {
++cnt;
for (int x = 0; x != v; --tp) {
x = stk[tp];
T[cnt].push_back(x);
T[x].push_back(cnt);
}
T[cnt].push_back(u);
T[u].push_back(cnt);
}
} else
low[u] = std::min(low[u], dfn[v]);
}
}
int dep[MN * 2], faz[MN * 2][18], dis[MN * 2];
void DFS(int u, int fz) {
dfn[u] = ++dfc;
dep[u] = dep[faz[u][0] = fz] + 1;
dis[u] = dis[fz] + (u <= N);
for (int j = 0; j < 17; ++j) faz[u][j + 1] = faz[faz[u][j]][j];
for (int v : T[u])
if (v != fz) DFS(v, u);
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int j = 0, d = dep[x] - dep[y]; d; ++j, d >>= 1)
if (d & 1) x = faz[x][j];
if (x == y) return x;
for (int j = 17; ~j; --j)
if (faz[x][j] != faz[y][j]) x = faz[x][j], y = faz[y][j];
return faz[x][0];
}
int main() {
int Ti;
scanf("%d", &Ti);
while (Ti--) {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; ++i) {
G[i].clear();
dfn[i] = low[i] = 0;
}
for (int i = 1; i <= N * 2; ++i) T[i].clear();
for (int i = 1, x, y; i <= M; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
cnt = N;
dfc = 0, Tarjan(1), --tp;
dfc = 0, DFS(1, 0);
scanf("%d", &Q);
while (Q--) {
static int S, A[MN];
scanf("%d", &S);
int Ans = -2 * S;
for (int i = 1; i <= S; ++i) scanf("%d", &A[i]);
std::sort(A + 1, A + S + 1, [](int i, int j) { return dfn[i] < dfn[j]; });
for (int i = 1; i <= S; ++i) {
int u = A[i], v = A[i % S + 1];
Ans += dis[u] + dis[v] - 2 * dis[LCA(u, v)];
}
if (LCA(A[1], A[S]) <= N) Ans += 2;
printf("%d\n", Ans / 2);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 30
Accepted
time: 617ms
memory: 54216kb
input:
10 94814 186251 42720 65317 33135 40188 90524 91057 15157 59320 56261 84256 49874 66210 18118 38018 34500 43670 14570 37867 53678 90002 13331 40076 27677 48562 36311 65435 29685 49754 22695 61837 59763 84023 2954 70002 18405 72897 7652 64325 69023 79028 27045 81899 26653 32017 1461 81255 6241 78409 ...
output:
10842 1733 17003 6950 3081 12247 9063 5817 5912 4840 6920 32177 14220 2021 54095 13636 35977 54537 47758 8286 51352 47579 86029 46711 27894 32639 51745 10230 8399 11304 45143 52011 58472 439 2726 41590 8326
result:
ok 37 lines
Test #2:
score: 45
Accepted
time: 1243ms
memory: 49552kb
input:
10 98254 198477 14613 72237 63083 73551 14567 32237 32006 56998 14940 52592 52150 61985 3972 84462 19680 42800 9518 55226 52089 66431 42873 68721 4698 93512 53178 79801 11807 42183 45906 77619 45069 47574 16109 83638 19343 49330 30148 51345 73355 83865 37818 69769 54127 92904 32072 92335 7056 71015 ...
output:
32214 45632 10703 9929 5065 5453 21134 1147 10520 8383 14579 4868 24196 19083 19547 52667 5751 8082 35026 36318 21654 4430 26616 29113 41250 5023 31323 34532 21542 18072 33179 30000 22532 43537 9529 14566 14622 20347 15861 5081 12382 16383 17131 7830 3422 7814 29927 2417 24145 8548 1450 33208 6269 1...
result:
ok 1000000 lines
Test #3:
score: 25
Accepted
time: 1069ms
memory: 50028kb
input:
10 46832 199154 1937 21636 422 25158 5067 12645 42564 46740 4459 16622 3322 11404 10269 16390 28904 37319 24540 25151 21880 37822 12789 18753 7634 16767 25495 38865 36915 43797 26907 45920 31649 35332 18827 38743 16391 32795 5736 42007 3936 31488 1622 13275 2420 19285 19601 41219 4797 20725 12352 45...
output:
3339 8701 1771 7277 10508 5825 5044 14372 2864 991 606 14039 9919 8375 5429 5441 3941 3677 1350 596 4748 970 1669 9439 9298 5551 3286 118 1563 7465 204 2500 780 82 7560 959 797 1640 6131 3500 5826 914 1371 2232 3856 4591 14439 13172 2933 1900 5192 3717 1155 11892 5731 13205 344 1801 2285 506 225 170...
result:
ok 341666 lines