QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#700383 | #8941. Even or Odd Spanning Tree | rikka_lyly# | WA | 120ms | 3968kb | C++20 | 3.8kb | 2024-11-02 12:52:24 | 2024-11-02 12:52:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define MAXN 200010
#define int ll
namespace BCJ
{
int fa[MAXN];
void init(int n)
{
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
}
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void unity(int x, int y)
{
fa[find(x)] = find(y);
}
};
void solve()
{
int n, m;
cin >> n >> m;
int logn = log2(n) + 3;
vector<pair<int, pair<int, int>>> eds(m);
vector<bool> tag(m);
for (int i = 0; i < m; i++)
{
cin >> eds[i].second.first >> eds[i].second.second >> eds[i].first;
}
vector<vector<pair<int, int>>> adj(n + 1);
BCJ::init(n);
sort(eds.begin(), eds.end());
int ans = 0;
int toted = 0;
for (int i = 0; i < m; i++)
{
auto &[w, uv] = eds[i];
auto &[u, v] = uv;
if(BCJ::find(u) != BCJ::find(v))
{
// cerr << "u=" << u << " v=" << v << endl;
ans += w;
BCJ::unity(u, v);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
tag[i] = 1;
toted++;
}
}
if(toted != n - 1)
{
cout << -1 << " " << -1 << '\n';
return;
}
vector<vector<int>> fa(n + 1, vector<int>(logn, 0));
vector<vector<array<int, 2>>> mxnum(n + 1, vector<array<int, 2>>(logn, {0, 0}));
vector<int> deep(n + 1);
auto dfs = [&](auto self, int cur, int father, int lstval) -> void
{
deep[cur] = deep[father] + 1;
fa[cur][0] = father;
mxnum[cur][0][lstval % 2] = lstval;
for (int i = 1; i < logn; i++)
{
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
mxnum[cur][i][0] = max(mxnum[cur][i - 1][0], mxnum[fa[cur][i - 1]][i - 1][0]);
mxnum[cur][i][1] = max(mxnum[cur][i - 1][1], mxnum[fa[cur][i - 1]][i - 1][1]);
}
for (auto &[to, w] : adj[cur])
{
if(to == father)
continue;
self(self, to, cur, w);
}
};
dfs(dfs, 1, 0, 0);
auto getmxnum = [&](int x, int y) -> array<int, 2>
{
if(deep[x] > deep[y])
swap(x, y);
array<int, 2> ans = {0, 0};
int temp = deep[y] - deep[x];
for (int i = 0; i < logn; i++)
{
if(temp >> i & 1)
{
ans = max(ans, mxnum[y][i]);
y = fa[y][i];
}
}
if(x == y)
return ans;
for (int i = logn - 1; i >= 0; i--)
{
if(fa[x][i] != fa[y][i])
{
ans = max(ans, mxnum[x][i]);
ans = max(ans, mxnum[y][i]);
x = fa[x][i];
y = fa[y][i];
}
}
ans = max(ans, mxnum[x][0]);
ans = max(ans, mxnum[y][0]);
return ans;
};
int newans = ans;
for (int i = 0; i < m; i++)
{
if(tag[i])
continue;
auto &[w, uv] = eds[i];
auto &[u, v] = uv;
auto newmx = getmxnum(u, v);
newans = max(newans, ans + w - newmx[!(w % 2)]);
}
if(newans == ans)
{
if(ans % 2 == 0)
cout << ans << " " << -1 << '\n';
else
cout << -1 << " " << ans << '\n';
}
else
{
if(ans % 2 == 0)
cout << ans << " " << newans << '\n';
else
cout << newans << " " << ans << '\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3684kb
input:
3 2 1 1 2 5 3 1 1 3 1 4 4 1 2 1 1 3 1 1 4 1 2 4 2
output:
-1 5 -1 -1 4 3
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 120ms
memory: 3968kb
input:
10000 16 50 1 2 649251632 2 3 605963017 3 4 897721528 4 5 82446151 5 6 917109168 6 7 79647183 7 8 278566178 7 9 573504723 3 10 679859251 8 11 563113083 1 12 843328546 10 13 594049160 11 14 997882839 7 15 569431750 2 16 244494799 16 7 960172373 13 4 317888322 13 3 446883598 9 3 678142319 5 8 43208692...
output:
3140067932 4082296793 1262790434 2180055478 1263932600 2193298680 2175442364 1180186165 2248358640 3214682981 4696867870 3738936375 2041651868 1058677117 4376848953 3402127725 2159453260 1187873655 1395482806 2361844128 3456885934 4378025294 3943951826 4840504690 2479987500 3398715355 2909126794 389...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '4082296793'