QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613298 | #8941. Even or Odd Spanning Tree | ticking_away# | WA | 94ms | 11852kb | C++17 | 3.4kb | 2024-10-05 13:53:27 | 2024-10-05 13:53:37 |
Judging History
answer
#include <bits/stdc++.h>
#define con const
using namespace std;
using is = int32_t;
using il = int64_t;
using us = uint32_t;
using ul = uint64_t;
template<class T> void mes(T *arr, us n=1, us b=0) { memset(arr, b, n*sizeof(T)); }
template<us LEN>
struct DSU {
us far[LEN] = {0}, rnk[LEN] = {0};
void init(us l, us r) {
mes(rnk+l, r-l);
iota(far+l, far+r, l);
}
us find(us u) {
if(far[u]==u) return u;
else return far[u] = find(far[u]);
}
bool same(us l, us r) { return find(l) == find(r); }
bool merge(us l, us r) {
if((l=find(l))==(r=find(r))) return 0;
if(rnk[l]<rnk[r]) swap(l, r);
else if(rnk[l]==rnk[r]) ++rnk[l];
return far[r]=l, 1;
}
};
// -------------------------
con us MXN = 2e5 + 7;
con us MXM = 5e5 + 7;
con us LGM = 20;
us n, m;
// (w, u, v)
array<us, 3> inp[MXM];
DSU<MXN> dsu;
// (v, w)
using EI = array<us, 2>;
vector<EI> g[MXN];
us dpt[MXN], far[MXN][LGM], mxl[MXN][LGM][2];
void dfs(us u, us f=0) {
for(us i=1; i<LGM; ++i) {
far[u][i] = far[far[u][i-1]][i-1];
mxl[u][i][0] = max(mxl[u][i-1][0], mxl[far[u][i-1]][i-1][0]);
mxl[u][i][1] = max(mxl[u][i-1][1], mxl[far[u][i-1]][i-1][1]);
}
for(auto [v, w]:g[u]) if(v!=f) {
dpt[v] = dpt[u] + 1;
far[v][0] = u;
mxl[v][0][w&1] = w;
dfs(v, u);
}
}
us get_far(us u, us d) {
for(us i=0; d&&i<LGM; ++i, (d>>=1))
if(d&1) u = far[u][i];
return u;
}
us get_lca(us l, us r) {
if(dpt[l]<dpt[r]) swap(l, r);
l = get_far(l, dpt[l]-dpt[r]);
if(l==r) return l;
for(us i=LGM; i--;) {
if(far[l][i]!=far[r][i]) {
l = far[l][i];
r = far[r][i];
}
}
return far[l][0];
}
us get_mxl(us u, us d, bool odd) {
us res = 0;
for(us i=0; d&&i<LGM; ++i, (d>>=1)) {
if(d&1) {
res = max(res, mxl[u][i][odd]);
u = far[u][i];
}
}
return res;
}
ul ans[2];
void solve() {
bool fg = ans[0]>0;
if(!ans[!fg]) {
cout << "-1 -1";
return;
}
for(us i=0; i<m; ++i) {
auto & [w, u, v] = inp[i];
if(w) {
us lca = get_lca(u, v);
us tw = max(get_mxl(u, dpt[u]-dpt[lca], !(w&1)),
get_mxl(v, dpt[v]-dpt[lca], !(w&1)));
if(tw) {
ans[fg] = max(ans[fg], ans[!fg]+(w-tw));
}
}
}
if(ans[fg]==0) ans[fg] = -1;
cout << il(ans[0]) << ' ' << il(ans[1]);
}
void pre() {
cin >> n >> m;
for(us i=0, u, v, w; i<m; ++i) {
cin >> u >> v >> w;
inp[i] = {w, --u, --v};
}
sort(inp, inp+m);
us cnt = 0;
dsu.init(0, n);
ul res = 0;
for(us i=0; i<m; ++i) {
auto & [w, u, v] = inp[i];
if(dsu.merge(u, v)) {
res += w;
g[u].push_back({v, w});
g[v].push_back({u, w});
w = 0; ++cnt;
}
}
if(cnt==n-1)
ans[res&1] = res;
dfs(0);
}
void aft() {
for(us i=0; i<n; ++i)
g[i].clear();
ans[0] = ans[1] = 0;
cout << '\n';
}
us _t = 1;
void init() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> _t;
}
int main() {
init();
for(us i=1; i<=_t;) {
pre(); solve();
++i<=_t?aft():exit(0);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11852kb
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: 94ms
memory: 11772kb
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 3941554631 1262790434 2111126551 1263932600 2177809565 5444593736 1180186165 2248358640 6538424517 7962881622 3738936375 5320518474 1058677117 7679759462 3402127725 5480837692 1187873655 1395482806 5643556337 3456885934 7555537467 3943951826 8218623321 2479987500 6370555249 2909126794 388...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '3941554631'