QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729051 | #8941. Even or Odd Spanning Tree | Wubaozi123 | TL | 16ms | 108932kb | C++17 | 2.2kb | 2024-11-09 16:25:28 | 2024-11-09 16:25:28 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MaxN=200000+10;
int n,m,u,v,c,ans,mnde,bel[MaxN],sm,t;
int fa[MaxN][21],dep[MaxN],mx[MaxN][21][2];
vector<pair<int,int>>V[MaxN];
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>>Q,Q2;
//doublizing
int find(int x){
return (bel[x]==x?x:bel[x]=find(bel[x]));
}
void dfs(int u,int faa){
for(int i=1;fa[u][i-1]>0;i++){
fa[u][i]=fa[fa[u][i-1]][i-1];
mx[u][i][0]=max(mx[u][i-1][0],mx[fa[u][i-1]][i-1][0]);
mx[u][i][1]=max(mx[u][i-1][1],mx[fa[u][i-1]][i-1][1]);
}
for(auto [v,c]:V[u]){
if(v==faa)continue;
fa[v][0]=u;
mx[v][0][c%2]=c;
dep[v]=dep[u]+1;
dfs(v,u);
}
}
int lca(int a,int b,bool c){
if(dep[a]<dep[b])swap(a,b);
int delp=dep[a]-dep[b],res=0;
for(int i=20;i>=0;i--){
if(delp&(1<<i)){
res=m,max(res,mx[a][i][c]);
a=fa[a][i];
}
}
if(a==b)return res;
for(int i=20;i>=0;i--){
if(fa[a][i]!=fa[b][i]){
res=max(res,max(mx[a][i][c],mx[b][i][c]));
a=fa[a][i];
b=fa[b][i];
}
}
res=max(res,max(mx[a][0][c],mx[b][0][c]));
return res;
}
void solve(){
for(int i=1;i<=n;i++)V[i].clear();
memset(mx,-1,sizeof(mx));
memset(fa,-1,sizeof(fa));
mnde=100000000,ans=sm=0;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u>>v>>c;
if(u==v)continue;
Q.emplace(c,u,v);
}
for(int i=1;i<=n;i++)bel[i]=i;
while(!Q.empty()){
auto [c,u,v]=Q.top();Q.pop();
int a=find(u),b=find(v);
if(a==b){
Q2.emplace(c,u,v);//unchosen edge
continue;
}
bel[a]=b;
ans+=c;sm++;
V[u].emplace_back(v,c);//build tree
V[v].emplace_back(u,c);
}
if(sm+1<n){
cout<<"-1 -1"<<endl;
return ;
}
dfs(1,0);//f**king init for LCA
while(!Q2.empty()){
auto [c,u,v]=Q2.top();Q2.pop();
int del=lca(u,v,abs(c%2-1));
mnde=min(mnde,c-del);
}
if(mnde<=0||mnde>=100000000){
if(ans%2)cout<<"-1 "<<ans<<endl;
else cout<<ans<<" -1"<<endl;
}else{
if(ans%2)cout<<mnde+ans<<" "<<ans<<endl;
else cout<<mnde+ans<<" "<<ans<<endl;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 108932kb
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
Time Limit Exceeded
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:
3159441841 3140067932 1309454727 1262790434 1263932600 -1 1211686233 1180186165 2261370885 2248358640 -1 3738936375 -1 1058677117 -1 3402127725 1228634386 1187873655 1410162043 1395482806 3516310363 3456885934 3988876469 3943951826 2576467979 2479987500 2930167271 2909126794 2483103706 -1 1852884410...