QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#631036 | #8941. Even or Odd Spanning Tree | Rnfmabj | TL | 451ms | 16888kb | C++14 | 2.3kb | 2024-10-11 21:32:44 | 2024-10-11 21:32:44 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=2e5+5,maxm=5e5+5,inf=-1ll<<60;
ll n,m;
struct edge1{
ll x,y,val;
friend bool operator < (const edge1 &x,const edge1 &y){
return x.val==y.val?x.x<y.x:x.val<y.val;
}
}E[maxm];
ll b[maxn];
vector<pair<ll,ll> >e[maxn];
#define mp make_pair
#define fi first
#define se second
#define pb push_back
ll fnd(ll x){return b[x]==x?x:fnd(b[x]);}//路径压缩
void merge(ll x,ll y){
x=fnd(x),y=fnd(y);
b[x]=y;
}
ll fa[maxn][19],dep[maxn];
ll mn[2][maxn][19];
void dfs(ll x){
for(ll i=1;i<=18;i++){
fa[x][i]=fa[fa[x][i-1]][i-1];
// if(!fa[x][i])break;
}
for(ll i=1;i<=18;i++){
// if(!fa[x][i])break;
mn[0][x][i]=max(mn[0][fa[x][i-1]][i-1],mn[0][x][i-1]);
mn[1][x][i]=max(mn[1][fa[x][i-1]][i-1],mn[1][x][i-1]);
}
for(auto it:e[x]){
ll v=it.fi,val=it.se;
if(v==fa[x][0])continue;
fa[v][0]=x;
mn[val&1][v][0]=val;
dep[v]=dep[x]+1;
dfs(v);
}
}
ll query(ll x,ll y,ll ty){
if(dep[x]<dep[y])swap(x,y);
ll res=inf;
for(ll i=18;i>=0;i--){
if(dep[fa[x][i]]>=dep[y]){
res=max(res,mn[ty][x][i]);
x=fa[x][i];
}
}
if(x==y)return res;
for(ll i=18;i>=0;i--){
if(fa[x][i]!=fa[y][i]){
res=max(res,max(mn[ty][x][i],mn[ty][y][i]));
x=fa[x][i],y=fa[y][i];
}
}
res=max(res,max(mn[ty][x][0],mn[ty][y][0]));
return res;
}
bool vis[maxm];
void solve(){
cin>>n>>m;
for(ll i=1;i<=n;i++)b[i]=i,e[i].clear();
for(ll i=0;i<=n;i++){//1->0
for(ll j=0;j<=18;j++){
mn[0][i][j]=mn[1][i][j]=inf;
}
}
for(ll i=1;i<=m;i++){
cin>>E[i].x>>E[i].y>>E[i].val;
vis[i]=0;
}
sort(E+1,E+1+m);
ll sum=0,ans=-inf;
for(ll i=1;i<=m;i++){
ll x=E[i].x,y=E[i].y,val=E[i].val;
x=fnd(x),y=fnd(y);
if(x!=y)merge(x,y),sum+=val,e[E[i].x].pb(mp(E[i].y,val)),e[E[i].y].pb(mp(E[i].x,val)),vis[i]=1;
}
for(ll i=1;i<=n;i++){
if(fnd(i)!=fnd(1)){
cout<<"-1 -1"<<endl;
return ;
}
}
dep[1]=1;
dfs(1);
for(ll i=1;i<=m;i++){
if(vis[i])continue;
ans=min(ans,sum+E[i].val-query(E[i].x,E[i].y,(E[i].val&1)^1));
}
if(sum%2==1)swap(sum,ans);
if(sum==-inf)sum=-1;
if(ans==-inf)ans=-1;
cout<<sum<<" "<<ans<<endl;
}
ll T=1;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 15916kb
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: 124ms
memory: 15960kb
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: 0
Accepted
time: 451ms
memory: 16888kb
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 411997066938 411996924725 231780454372 231780901543 156856996316 156856345151 239278757000 239278493321 288480848080 288481502909 347301406524 347301269265 362642903994 362643090967 158361335208 1...
result:
ok 200 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
10 15547 50000 1 2 762013057 2 3 473514078 3 4 73078629 4 5 932961699 5 6 157436174 6 7 190061912 7 8 140204258 8 9 737720271 9 10 386190699 10 11 92718916 11 12 68384258 12 13 389445848 13 14 906761733 14 15 644882062 15 16 429774551 16 17 330771475 17 18 815808541 18 19 239714301 19 20 350847429 2...
output:
2833348523676 2833348592065 4133065384586 4133065395925