QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426511 | #2583. 战略游戏 | Max_s_xaM | 0 | 725ms | 60060kb | C++14 | 4.6kb | 2024-05-31 13:41:22 | 2024-05-31 13:41:24 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 2e5 + 10;
int n, m, q;
int len, a[MAXN];
vector <int> e[MAXN], g[MAXN];
int dfn[MAXN], low[MAXN], clk, bcc;
int st[MAXN], top;
inline void Tarjan(int u)
{
dfn[u] = low[u] = ++clk, st[++top] = u;
for (auto v : e[u])
if (!dfn[v])
{
Tarjan(v), low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u])
{
bcc++;
while (st[top] != v) g[st[top]].push_back(bcc), g[bcc].push_back(st[top]), top--;
g[v].push_back(bcc), g[bcc].push_back(v), top--;
g[u].push_back(bcc), g[bcc].push_back(u);
}
}
else low[u] = min(low[u], dfn[v]);
}
int dis[MAXN], dep[MAXN], f[MAXN][21], lg[MAXN];
void DFS(int u, int fa)
{
dfn[u] = ++clk, dep[u] = dep[fa] + 1, dis[u] = dis[fa] + (u <= n), f[u][0] = fa;
for (int i = 1; i < lg[dep[u]]; i++)
f[u][i] = f[f[u][i - 1]][i - 1];
for (auto v : g[u])
if (v != fa) DFS(v, u);
}
inline int LCA(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
while (dep[x] > dep[y]) x = f[x][lg[dep[x] - dep[y]] - 1];
if (x == y) return x;
for (int i = lg[dep[x]] - 1; ~i; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
inline int Dist(int u, int v) {return dis[u] - dis[v];}
inline void Solve()
{
Read(n), Read(m);
for (int i = 1; i <= n; i++)
{
e[i].clear();
dfn[i] = low[i] = 0;
}
for (int i = 1; i <= bcc; i++)
{
g[i].clear();
dis[i] = dep[i] = 0;
for (int j = 0; j < 20; j++) f[i][j] = 0;
}
clk = top = 0, bcc = n;
for (int i = 1, u, v; i <= m; i++) Read(u), Read(v), e[u].push_back(v), e[v].push_back(u);
Tarjan(1);
clk = 0, DFS(1, 0);
Read(q);
while (q--)
{
Read(len);
for (int i = 1; i <= len; i++) Read(a[i]);
sort(a + 1, a + len + 1, [&](int x, int y) {return dfn[x] < dfn[y];});
st[top = 1] = a[1];
int ans = 1 - len;
for (int i = 2; i <= len; i++)
{
int lca = LCA(st[top], a[i]);
while (top > 1 && dep[st[top - 1]] >= dep[lca]) ans += Dist(st[top], st[top - 1]), top--;
if (st[top] == lca) st[++top] = a[i];
else ans += Dist(st[top], lca), st[top] = lca, st[++top] = a[i];
}
while (top > 1) ans += Dist(st[top], st[top - 1]), top--;
cout << ans << '\n';
}
}
int main()
{
// freopen("B.in", "r", stdin);
// freopen("B.out", "w", stdout);
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
for (int i = 1; i < MAXN; i++) lg[i] = lg[i >> 1] + 1;
int T;
Read(T);
while (T--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 317ms
memory: 60060kb
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 9064 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 8327
result:
wrong answer 7th lines differ - expected: '9063', found: '9064'
Test #2:
score: 0
Wrong Answer
time: 725ms
memory: 56424kb
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:
32215 45633 10703 9929 5065 5453 21134 1147 10520 8383 14579 4868 24197 19084 19548 52667 5751 8083 35026 36319 21654 4431 26616 29113 41250 5024 31323 34532 21542 18072 33180 30001 22532 43538 9529 14566 14622 20347 15862 5081 12383 16383 17132 7831 3422 7814 29927 2417 24145 8549 1450 33208 6270 1...
result:
wrong answer 1st lines differ - expected: '32214', found: '32215'
Test #3:
score: 0
Wrong Answer
time: 680ms
memory: 57068kb
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:
wrong answer 68th lines differ - expected: '3654', found: '3655'