QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359952 | #6559. A Tree and Two Edges | LaStataleBlue | WA | 77ms | 10336kb | C++17 | 3.2kb | 2024-03-21 05:41:20 | 2024-03-21 05:41:20 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <utility>
#include <array>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
constexpr int MAXN = 5e4 + 5;
constexpr int LOGMAXN = 17;
vector<int> g[MAXN];
int lift[MAXN][LOGMAXN];
int tin[MAXN], tout[MAXN], timer;
pii be1, be2;
bool visited[MAXN];
void dfs(const int n, const int p)
{
lift[n][0] = p;
for (int i = 1; i < LOGMAXN; i++)
lift[n][i] = lift[lift[n][i - 1]][i - 1];
visited[n] = true;
tin[n] = timer++;
for (const int a : g[n])
{
if (a == p)
continue;
if (!visited[a])
dfs(a, n);
else
{
be1 = {a, n};
swap(be1, be2);
}
}
tout[n] = timer++;
}
bool is_ancestor(const int u, const int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
struct Path
{
const int top, bot;
constexpr static Path nullpath() { return {-1, -1}; }
bool isnull() const { return top == -1 && bot == -1; }
bool intersect(const Path &o) const
{
return is_ancestor(top, o.top) && is_ancestor(o.top, bot) ||
is_ancestor(top, o.bot) && is_ancestor(o.bot, bot) ||
is_ancestor(o.top, top) && is_ancestor(top, o.bot) ||
is_ancestor(o.top, bot) && is_ancestor(bot, o.bot);
}
};
array<Path, 2> get_paths(int u, int v)
{
if (is_ancestor(u, v))
return {Path{u, v}, Path::nullpath()};
if (is_ancestor(v, u))
return {Path{v, u}, Path::nullpath()};
const int start_u = u;
for (int i = LOGMAXN - 1; i >= 0; i--)
if (!is_ancestor(lift[u][i], v))
u = lift[u][i];
return {Path{u, start_u}, Path{lift[u][0], v}};
}
bool check(const initializer_list<pii> n)
{
vector<Path> paths;
for (const pii a : n)
{
const auto [p1, p2] = get_paths(a.first, a.second);
paths.push_back(p1);
if (!p2.isnull())
paths.push_back(p2);
}
for (int i = 0; i < paths.size(); i++)
for (int j = 0; j < i; j++)
if (paths[i].intersect(paths[j]))
return false;
return true;
}
int main()
{
int N, Q;
scanf("%d %d", &N, &Q);
for (int i = 0, u, v; i < N + 1; i++)
{
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
timer = 0;
dfs(1, 1);
for (int q = 0, u, v; q < Q; q++)
{
scanf("%d %d", &u, &v);
int ans = 1;
ans +=
check({pii(u, be1.first), pii(be1.second, v)}) ||
check({pii(u, be1.second), pii(be1.first, v)});
ans +=
check({pii(u, be2.first), pii(be2.second, v)}) ||
check({pii(u, be2.second), pii(be2.first, v)});
ans +=
check({pii(u, be1.first), pii(be1.second, be2.first), pii(be2.second, v)}) ||
check({pii(u, be1.first), pii(be1.second, be2.second), pii(be2.first, v)}) ||
check({pii(u, be1.second), pii(be1.first, be2.first), pii(be2.second, v)}) ||
check({pii(u, be1.second), pii(be1.first, be2.second), pii(be2.first, v)}) ||
check({pii(u, be2.first), pii(be2.second, be1.first), pii(be1.second, v)}) ||
check({pii(u, be2.first), pii(be2.second, be1.second), pii(be1.first, v)}) ||
check({pii(u, be2.second), pii(be2.first, be1.first), pii(be1.second, v)}) ||
check({pii(u, be2.second), pii(be2.first, be1.second), pii(be1.first, v)});
printf("%d\n", ans);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4960kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
3 3 3 3 3 4
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 4972kb
input:
6 4 1 2 1 3 1 6 2 3 3 4 3 5 4 5 1 2 1 3 1 4 1 6
output:
2 2 4 1
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 67ms
memory: 10280kb
input:
50000 50000 11561 23122 14261 28523 24407 48814 17947 35894 14803 29607 19557 39115 12415 24830 9583 19167 18397 36794 439 878 18040 36080 17150 34300 7922 15845 18938 37877 18625 37250 6459 12919 9818 19636 3579 7158 21323 42646 23882 47764 13603 27207 8353 16707 15907 31814 20935 41871 11686 23372...
output:
4 3 3 4 4 3 1 3 4 1 3 4 3 4 3 3 1 3 3 3 4 4 4 3 3 3 4 3 3 3 1 3 3 3 3 4 4 4 4 3 4 3 3 3 4 3 4 4 4 4 3 4 4 4 4 3 3 3 4 4 4 4 4 4 3 4 3 4 1 4 1 1 4 3 3 4 3 3 1 4 3 3 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 3 4 3 3 3 1 3 4 4 3 4 3 4 3 3 4 1 4 3 3 3 4 4 4 4 3 3 3 3 3 4 4 4 4 3 3 4 3 4 4 3 3 4 4 4 1 3 3 3 3 4 4 3 ...
result:
ok 50000 lines
Test #4:
score: 0
Accepted
time: 77ms
memory: 10336kb
input:
50000 50000 1730 3460 17535 35071 14108 28216 20630 41260 2091 4182 8112 16225 21373 42746 6685 13371 21142 42284 12168 24337 22564 45128 16103 32207 9254 18508 21369 42739 1955 3911 13696 27392 3929 7858 1777 3555 23382 46764 830 1660 17722 35444 11495 22991 10184 20369 13697 27395 24728 49456 4037...
output:
1 1 3 3 3 4 4 4 4 4 3 3 3 4 1 3 4 3 3 1 3 3 4 3 1 4 3 3 4 3 3 4 4 4 1 1 4 1 3 4 3 1 4 4 3 3 3 4 1 4 4 1 3 1 3 3 3 1 1 3 3 3 3 3 4 3 4 4 3 3 4 4 4 3 3 4 4 4 3 4 3 3 3 3 3 3 3 3 4 4 1 4 3 4 1 4 4 4 4 3 4 1 4 4 3 4 3 4 3 4 3 1 4 3 1 1 3 3 4 4 1 3 3 3 4 3 3 4 4 4 4 4 3 3 4 3 4 1 1 3 4 4 3 4 4 3 3 4 4 3 ...
result:
ok 50000 lines
Test #5:
score: -100
Wrong Answer
time: 69ms
memory: 10260kb
input:
50000 50000 21879 43758 12510 25020 2593 5187 16048 32096 9697 19394 12606 25212 3860 7720 8231 16462 23983 47966 10852 21705 6919 13839 1385 2770 4040 8080 14298 28596 22248 44496 4245 8490 14486 28972 11445 22891 21557 43114 20946 41892 23374 46749 78 157 4617 9234 8198 16396 12228 24456 16125 322...
output:
3 3 3 3 3 3 1 3 3 1 3 3 1 3 3 3 3 3 3 3 3 1 3 3 1 3 3 3 3 1 1 3 3 3 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 1 3 1 3 3 3 3 3 3 3 3 3 3 3 3 1 3 1 3 3 3 3 3 3 3 3 1 3 3 1 3 1 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 1 1 1 3 3 1 3 3 1 3 3 3 3 3 3 3 3 3 3 3 1 3 1 3 3 1 3 3 3 3 3 3 3 ...
result:
wrong answer 1st lines differ - expected: '4', found: '3'