QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#674398 | #8941. Even or Odd Spanning Tree | realman | WA | 135ms | 16420kb | C++14 | 2.1kb | 2024-10-25 15:34:23 | 2024-10-25 15:34:24 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+10,M=5e5+10;
int n,m,F[N],dep[N],f[N][21][2],fa[N][21],lg[N];ll ans[2];
vector<pair<int,int> > g[N];
struct Edge
{
int u,v,w,op;
}e[M];
bool cmp(Edge x,Edge y)
{
return x.w<y.w;
}
int find(int x)
{
return F[x]=x==F[x]?x:find(F[x]);
}
int solve(int u,int v,int op)
{
int maxx=-1e9;
if(dep[u]<dep[v]) swap(u,v);
for(int i=0;i<=lg[dep[u]-dep[v]];i++)
if((1<<i)&(dep[u]-dep[v]))
maxx=max(maxx,f[u][i][op]),u=fa[u][i];
if(u==v) return maxx;
for(int i=lg[dep[u]];i>=0;i--)
if(fa[u][i]^fa[v][i])
{
maxx=max(maxx,f[u][i][op]),u=fa[u][i];
maxx=max(maxx,f[v][i][op]),v=fa[v][i];
}
maxx=max(maxx,max(f[u][0][op],f[v][0][op]));
return maxx;
}
void dfs(int p,int fath,int las)
{
dep[p]=dep[fath]+1;
fa[p][0]=fath;
f[p][0][las&1]=las;
f[p][0][las&1^1]=-1e9;
for(int i=1;i<=lg[dep[p]];i++)
{
fa[p][i]=fa[fa[p][i-1]][i-1];
for(int j=0;j<2;j++)
f[p][i][j]=max(f[p][i-1][j],f[fa[p][i-1]][i-1][j]);
}
for(int i=0;i<g[p].size();i++)
{
int to=g[p][i].first;
if(to==fath) continue;
dfs(to,p,g[p][i].second);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) F[i]=i,g[i].clear();
for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=m;i++)
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w),e[i].op=0;
sort(e+1,e+m+1,cmp);
int cnt=0;ll sum=0;
for(int i=1;i<=m;i++)
if(find(e[i].u)!=find(e[i].v))
{
cnt++;e[i].op=1;sum+=e[i].w;
g[e[i].u].push_back(make_pair(e[i].v,e[i].w));
g[e[i].v].push_back(make_pair(e[i].u,e[i].w));
F[find(e[i].u)]=find(e[i].v);
}
if(cnt!=n-1)
{
printf("-1 -1\n");
continue;
}
ans[sum&1]=sum;
ans[sum&1^1]=-1;
dfs(1,0,-1e9);
for(int i=1;i<=m;i++)
if(!e[i].op)
{
int num=solve(e[i].u,e[i].v,e[i].w&1^1);
if(num<0) continue;
if(ans[sum&1^1]==-1ll) ans[sum&1^1]=sum-num+e[i].w;
else ans[sum&1^1]=min(ans[sum&1^1],sum-num+e[i].w);
}
printf("%lld %lld\n",ans[0],ans[1]);
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 14196kb
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: 0
Accepted
time: 102ms
memory: 16152kb
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 3159441841 1262790434 1309454727 1263932600 1307926149 1189242112 1180186165 2248358640 2261370885 3776889482 3738936375 1093500444 1058677117 3433711014 3402127725 1201099898 1187873655 1395482806 1410162043 3456885934 3486092007 3943951826 3988876469 2479987500 2535532661 2909126794 293...
result:
ok 20000 numbers
Test #3:
score: -100
Wrong Answer
time: 135ms
memory: 16420kb
input:
100 1806 5000 1 2 139994119 2 3 184924112 3 4 670813536 4 5 443325848 5 6 767909046 6 7 531742851 7 8 364385440 8 9 918195219 9 10 731896671 10 11 671688362 11 12 558665746 12 13 154497842 13 14 28636459 14 15 570343686 15 16 419626123 16 17 817852951 17 18 517701907 18 19 952451718 19 20 141747977 ...
output:
380509244078 380509217939 236564714828 236564588423 317690341848 317690193297 416922743878 416923542879 411602058138 411996924725 231780454372 231575603125 156856996316 156856345151 239198911000 239278493321 288480848080 288481502909 347103891276 347301269265 362642903994 362643090967 158361335208 1...
result:
wrong answer 9th numbers differ - expected: '411997066938', found: '411602058138'