QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#111055 | #6559. A Tree and Two Edges | Tobo | WA | 192ms | 7972kb | C++20 | 3.9kb | 2023-06-05 17:56:04 | 2023-06-05 17:56:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define N 50005
vector<int> adj[N];
pair<int, int> a[2];
int n, m, fa[N], dep[N], siz[N], top[N], son[N], dfn[N], tim[N], tot1;
int uni[N];
int find(int x)
{
if (x == uni[x])
return x;
else
return uni[x] = find(uni[x]);
}
void dfs1(int f, int cur)
{
fa[cur] = f;
dep[cur] = dep[f] + 1;
siz[cur] = 1;
int mx = -1;
for (const auto i : adj[cur])
{
if (i != f)
{
dfs1(cur, i);
siz[cur] += siz[i];
if (siz[i] > mx)
{
mx = siz[i];
son[cur] = i;
}
}
}
}
void dfs2(int t, int cur)
{
top[cur] = t;
dfn[cur] = ++tot1;
tim[tot1] = cur;
if (son[cur])
dfs2(t, son[cur]);
for (const auto i : adj[cur])
{
if (i != fa[cur] && i != son[cur])
{
dfs2(i, i);
}
}
}
int lca(int x, int y)
{
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
x = fa[top[x]];
}
if (dep[x] < dep[y])
swap(x, y);
return y;
}
int k, p[N], tot2, dis[N], sta[N], tp;
void solve(int u, int v)
{
sort(p + 1, p + k + 1, [](int a, int b)
{ return dfn[a] < dfn[b]; });
tot2 = 0;
sta[tp = 1] = 1;
map<int, set<int>> adj;
adj[a[0].first].insert(a[0].second);
adj[a[1].first].insert(a[1].second);
adj[a[0].second].insert(a[0].first);
adj[a[1].second].insert(a[1].first);
set<int> points;
points.insert(1);
for (int i = 1; i <= k; i++)
points.insert(p[i]);
for (int i = 1; i <= k; i++)
{
int f = lca(p[i], sta[tp]);
points.insert(f);
if (f != sta[tp])
{
while (dfn[f] < dfn[sta[tp - 1]])
{
adj[sta[tp - 1]].insert(sta[tp]);
adj[sta[tp]].insert(sta[tp - 1]);
tp--;
}
if (dfn[f] > dfn[sta[tp - 1]])
{
adj[f].insert(sta[tp]);
adj[sta[tp]].insert(f);
sta[tp] = f;
}
else
{
adj[f].insert(sta[tp]);
adj[sta[tp]].insert(f);
tp--;
}
}
if (sta[tp] != p[i])
sta[++tp] = p[i];
}
for (int i = 1; i < tp; i++)
{
adj[sta[i + 1]].insert(sta[i]);
adj[sta[i]].insert(sta[i + 1]);
}
int ans = 0;
for (int i : points)
{
dis[i] = 0;
adj[i].erase(i);
}
function<void(int)> dfs = [&](int cur) -> void
{
if (cur == v)
{
ans++;
return;
}
for (int i : adj[cur])
{
if (!dis[i])
{
dis[i] = 1;
dfs(i);
dis[i] = 0;
}
}
};
dis[u] = 1;
dfs(u);
dis[u] = 0;
cout << ans << '\n';
}
void solve()
{
cin >> n >> m;
iota(uni + 1, uni + n + 1, 1);
for (int i = 1, u, v; i <= 1 + n; i++)
{
cin >> u >> v;
int fx = find(u), fy = find(v);
if (fx != fy)
{
adj[u].push_back(v);
adj[v].push_back(u);
uni[fx] = fy;
}
else
{
if (a[0] == pair<int, int>{0, 0})
a[0] = {u, v};
else
a[1] = {u, v};
}
}
dfs1(0, 1);
dfs2(1, 1);
for (int i = 1; i <= m; i++)
{
cin >> p[1] >> p[2];
tie(p[3], p[4]) = a[0];
tie(p[5], p[6]) = a[1];
k = 6;
solve(p[1], p[2]);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6640kb
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: 3ms
memory: 6244kb
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: 178ms
memory: 7888kb
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: 192ms
memory: 7852kb
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: 0
Accepted
time: 192ms
memory: 7972kb
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:
4 2 2 2 4 2 1 2 2 2 4 2 1 2 4 2 2 4 4 4 2 1 4 2 2 4 2 4 2 2 1 4 2 2 1 1 4 4 2 2 2 4 2 4 2 4 4 4 2 2 4 2 2 4 1 4 1 4 4 2 4 4 2 2 2 2 4 2 2 1 2 1 4 2 4 4 4 4 2 2 2 4 4 1 2 1 2 4 4 4 2 2 2 2 4 4 4 4 4 2 4 2 4 4 2 4 4 4 4 4 2 4 2 2 4 4 4 1 2 2 2 4 1 2 4 1 2 2 4 2 4 4 2 4 2 2 4 1 4 1 2 4 2 2 4 4 4 4 4 4 ...
result:
ok 50000 lines
Test #6:
score: 0
Accepted
time: 164ms
memory: 7928kb
input:
50000 50000 1451 2795 8910 29108 638 5810 24117 38535 2769 44109 7603 8789 14090 14819 5315 11076 22885 25853 26110 39470 1513 20322 13635 44414 1284 5229 5998 19700 1872 45691 5872 37168 4991 6456 34921 41632 16532 30269 3118 4987 2732 20486 26292 44061 2054 41607 20367 21071 33204 36717 35801 4725...
output:
2 2 4 4 4 4 4 2 4 4 4 4 4 4 4 2 1 4 4 1 4 4 1 4 4 2 4 2 4 2 1 4 4 4 4 1 1 1 4 2 4 1 4 2 4 1 1 2 2 2 4 4 1 1 1 4 1 1 4 2 4 4 1 4 2 2 4 4 4 2 1 4 4 1 1 4 4 1 2 1 2 1 4 2 1 2 4 2 4 4 4 4 4 1 2 2 1 1 1 4 2 1 4 4 2 4 2 4 4 1 1 4 4 2 2 1 2 1 1 4 4 4 4 4 4 1 1 1 4 2 4 1 2 2 1 4 4 4 4 4 4 1 4 4 2 1 2 2 2 1 ...
result:
ok 50000 lines
Test #7:
score: -100
Wrong Answer
time: 118ms
memory: 7936kb
input:
50000 50000 1106 3307 13115 16051 30404 45806 2295 20076 3525 6384 9118 24628 3288 26835 17933 47506 26180 48256 23161 45529 10483 15545 5252 35302 10105 16247 14301 26402 104 216 562 29098 1517 16503 1494 5468 8057 47252 5582 15425 8766 41483 10952 31098 20891 49612 13088 18868 18880 28314 8650 208...
output:
1 2 4 2 2 2 2 4 2 4 4 4 2 2 2 2 2 2 2 2 4 2 2 1 2 1 2 4 1 2 4 2 1 2 4 4 4 2 2 2 2 2 2 1 2 1 2 1 2 2 2 2 4 2 1 2 4 4 2 2 2 1 4 2 4 2 2 4 2 2 2 2 2 2 1 4 2 2 2 4 4 2 1 2 1 2 4 2 2 4 4 1 2 2 2 4 1 2 1 2 2 2 2 4 4 4 1 2 2 2 2 2 4 2 4 2 2 2 1 2 4 2 1 4 4 4 2 2 2 2 2 2 2 4 2 2 2 4 2 2 2 1 2 1 4 2 2 2 2 4 ...
result:
wrong answer 5th lines differ - expected: '4', found: '2'