QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661566 | #8941. Even or Odd Spanning Tree | Corycle# | WA | 74ms | 20324kb | C++14 | 3.0kb | 2024-10-20 16:53:11 | 2024-10-20 16:53:11 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
using namespace std;
const ll inf=1e18;
const int N=2e5+5;
const int M=20;
int read(){
int s=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
return s*f;
}
int n,m,fa[N],p[N][M],dep[N];
ll Min[2][N][M];
vector<pair<int,int> >G[N];
struct Edge{int x,y,w;}P[N*3];
bool cmp(Edge A,Edge B){return A.w<B.w;}
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
void Clear(){
for(int i=1;i<=n;i++){
G[i].clear();
dep[i]=0;
}
}
void DFS(int x,int depth){
dep[x]=depth;
for(auto e:G[x]){
int y=e.first,w=e.second;
if(dep[y])continue;
Min[w&1][y][0]=w;
Min[!(w&1)][y][0]=inf;
p[y][0]=x;
for(int i=1;i<M;i++){
p[y][i]=p[p[y][i-1]][i-1];
Min[0][y][i]=min(Min[0][y][i-1],Min[0][p[y][i-1]][i-1]);
Min[1][y][i]=min(Min[1][y][i-1],Min[1][p[y][i-1]][i-1]);
}
DFS(y,depth+1);
}
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int k=M-1;k>=0;k--)if(dep[x]-(1<<k)>=dep[y])x=p[x][k];
if(x==y)return x;
for(int k=M-1;k>=0;k--)if(p[x][k]!=p[y][k]){x=p[x][k];y=p[y][k];}
return p[x][0];
}
ll Query(int x,int y,int t){
ll ans=inf;
if(dep[x]<dep[y])swap(x,y);
for(int k=M-1;k>=0;k--)
if(dep[x]-(1<<k)>=dep[y]){
ans=min(ans,Min[t][x][k]);
x=p[x][k];
}
if(x==y)return ans;
for(int k=M-1;k>=0;k--)
if(p[x][k]!=p[y][k]){
ans=min(ans,Min[t][x][k]);
ans=min(ans,Min[t][y][k]);
x=p[x][k];y=p[y][k];
}
return min(ans,min(Min[t][x][0],Min[t][y][0]));
}
int main(){
for(int i=0;i<M;i++)Min[0][0][i]=Min[1][0][i]=Min[0][1][i]=Min[1][1][i]=inf;
int T=read();
while(T--){
n=read();m=read();
for(int i=1;i<=m;i++){
P[i].x=read();P[i].y=read();P[i].w=read();
}
sort(P+1,P+m+1,cmp);
for(int i=1;i<=n;i++)fa[i]=i;
ll sum=0;
int num=0;
for(int i=1;i<=m;i++){
int x=Find(P[i].x),y=Find(P[i].y);
if(x==y)continue;
sum+=P[i].w;
fa[x]=y;
G[P[i].x].push_back(mp(P[i].y,P[i].w));
G[P[i].y].push_back(mp(P[i].x,P[i].w));
num++;
}
if(num!=n-1){puts("-1 -1");Clear();continue;}
DFS(1,1);
ll ans1=sum,ans2=inf;
for(int i=1;i<=m;i++){
int x=P[i].x,y=P[i].y,w=P[i].w;
int z=LCA(x,y);
ll val=Query(x,y,!(w&1));
if(val==inf)continue;
// cout<<"Query:"<<!(w&1)<<": "<<x<<" "<<y<<" "<<val<<endl;
ans2=min(ans2,ans1-val+w);
}
if(ans2==inf)ans2=-1;
if(ans1&1)swap(ans1,ans2);
printf("%lld %lld\n",ans1,ans2);
Clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20324kb
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: 74ms
memory: 13992kb
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'