QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#539514 | #8941. Even or Odd Spanning Tree | ucup-team4717# | WA | 62ms | 41464kb | C++14 | 1.7kb | 2024-08-31 15:01:02 | 2024-08-31 15:01:03 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
return x*f;
}
int T,n,m,ans;
struct Node{
int u,v,w;
}e[N];
map<int,int> mp[N];
int f[N],cnt,sum;
vector<int> p[N];
inline int Find(int x){
return x==f[x]?x:f[x]=Find(f[x]);
}
bool cmp(Node a,Node b){
return a.w<b.w;
}
void merge(int x,int y,int w){
if(p[x].size()<p[y].size()){
f[x]=y;
for(int u:p[x])
p[y].emplace_back(u),mp[u][y]=w;
}
else{
f[y]=x;
for(int u:p[y])
p[x].emplace_back(u),mp[u][x]=w;
}
cnt++;
sum+=w;
}
signed main(){
//freopen("road.in","r",stdin);
//freopen("road.out","w",stdout);
T=read();
while(T--){
n=read(),m=read(),ans=cnt=sum=0;
for(int i=1;i<=m;i++){
e[i].u=read();
e[i].v=read();
e[i].w=read();
}
sort(e+1,e+1+m,cmp);
for(int i=1;i<=n;i++) f[i]=i,p[i].emplace_back(i);
for(int i=1;i<=m;i++){
int x=Find(e[i].u),y=Find(e[i].v);
if(x==y) continue;
merge(x,y,e[i].w);
}
if(cnt<n-1){
printf("-1 -1\n");
for(int i=1;i<=n;i++) p[i].clear(),mp[i].clear();
continue;
}
int ans1=sum,ans2=-1;
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++){
int x=Find(e[i].u),y=Find(e[i].v);
if(x==y){
int o=max(mp[e[i].u][x],mp[e[i].v][x]);
if((o&1)^(e[i].w&1)){
ans2=ans1+e[i].w-o;
break;
}
continue;
}
merge(x,y,e[i].w);
}
if(ans1&1) swap(ans1,ans2);
printf("%lld %lld\n",ans1,ans2);
for(int i=1;i<=n;i++) p[i].clear(),mp[i].clear();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 41284kb
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: 62ms
memory: 41464kb
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 3191118501 1262790434 1474221255 1263932600 1307926149 1189242112 1180186165 2248358640 2277656157 3776889482 3738936375 1268531116 1058677117 3451376434 3402127725 1201099898 1187873655 1395482806 1440596255 3456885934 3500724419 3943951826 3988876469 2479987500 2752449745 2909126794 294...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '3191118501'