QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#695797 | #8941. Even or Odd Spanning Tree | 2018haha# | WA | 105ms | 20852kb | C++17 | 2.3kb | 2024-10-31 20:49:13 | 2024-10-31 20:49:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=500010;
int n,m;
vector<pair<int,int>> G[N];
int fa[N];
int dep[N];
int F[N][22],W[N][22][2];
void Dfs(int u,int fa)
{
for(int j=1;j<20;j++)
{
F[u][j]=F[F[u][j-1]][j-1];
W[u][j][0]=min(W[u][j-1][0],W[F[u][j-1]][j-1][0]);
W[u][j][1]=min(W[u][j-1][1],W[F[u][j-1]][j-1][1]);
}
for(auto [v,w]:G[u])
{
if(v==fa) continue;
F[v][0]=u;
W[v][0][0]=W[v][0][1]=1000000010;
W[v][0][w%2]=w;
dep[v]=dep[u]+1;
Dfs(v,u);
}
}
int Find(int x)
{
if(x==fa[x]) return x;
return fa[x]=Find(fa[x]);
}
int Query(int x,int y,int id)
{
if(dep[x]<dep[y]) swap(x,y);
int ret=1000000010;
for(int j=19;j>=0;j--)
if(dep[F[x][j]]>=dep[y])
ret=min(ret,W[x][j][id]),x=F[x][j];
if(x==y) return ret;
for(int j=19;j>=0;j--)
if(F[x][j]!=F[y][j])
{
ret=min(ret,W[x][j][id]);
ret=min(ret,W[y][j][id]);
x=F[x][j];
y=F[y][j];
}
ret=min(ret,W[x][0][id]);
ret=min(ret,W[y][0][id]);
return ret;
}
void Solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
G[i].clear(),fa[i]=i;
int cnt=0;
vector<array<int,3>> input(m),edge;
for(int i=0;i<m;i++)
{
auto &[z,x,y]=input[i];
cin>>x>>y>>z;
}
sort(input.begin(),input.end());
ll Ans=0;
for(int i=0;i<m;i++)
{
auto [z,x,y]=input[i];
int X=Find(x),Y=Find(y);
if(X==Y)
{
edge.push_back({z,x,y});
continue;
}
G[x].push_back({y,z});
G[y].push_back({x,z});
fa[X]=Y;
Ans+=z;
cnt++;
}
if(cnt<n-1)
{
cout<<"-1 -1\n";
return;
}
dep[1]=1;
Dfs(1,0);
ll Ans2=10000000000000000;
for(auto [z,x,y]:edge)
{
int ans=Query(x,y,(z%2)^1);
if(ans>1000000000) continue;
Ans2=min(Ans2,Ans-ans+z);
}
if(Ans2==10000000000000000) Ans2=-1;
if(Ans%2!=0) swap(Ans,Ans2);
cout<<Ans<<" "<<Ans2<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 20852kb
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: 105ms
memory: 20052kb
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 1332462897 1263932600 1395151561 1189242112 1180186165 2248358640 2277656157 3776889482 3738936375 1093500444 1058677117 3444549292 3402127725 1258609116 1187873655 1395482806 1411327105 3456885934 3486092007 3943951826 3988876469 2479987500 2733874051 2909126794 317...
result:
wrong answer 4th numbers differ - expected: '1309454727', found: '1332462897'