QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#776342 | #8941. Even or Odd Spanning Tree | ucup-team134# | WA | 121ms | 14388kb | C++14 | 2.9kb | 2024-11-23 18:17:30 | 2024-11-23 18:17:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=200050;
const int M=500050;
bool inMst[M];
vector<array<int,3>> E[N];
const int H=4*N;
const int inf=1e9+7;
int ls[H],rs[H],tsz,root[2],mx[H];
void Build(int&c,int ss,int se){
c=++tsz;
mx[c]=-inf;
if(ss==se)return;
int mid=ss+se>>1;
Build(ls[c],ss,mid);
Build(rs[c],mid+1,se);
}
void Set(int c,int ss,int se,int qi,int x){
if(ss==se){
mx[c]=x;
return;
}
int mid=ss+se>>1;
if(qi<=mid)Set(ls[c],ss,mid,qi,x);
else Set(rs[c],mid+1,se,qi,x);
mx[c]=max(mx[ls[c]],mx[rs[c]]);
}
int Get(int c,int ss,int se,int qs,int qe){
if(qs>qe||ss>qe||qs>se)return -inf;
if(qs<=ss&&qe>=se)return mx[c];
int mid=ss+se>>1;
return max(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
int n,dep[N];
int bestAdd;
void DFS(int u,int p,int w){
dep[u]=dep[p]+1;
Set(root[w%2],1,n,dep[u],w);
for(auto e:E[u]){
int v=e[0],nw=e[2];
if(inMst[e[1]]){
if(v==p)continue;
DFS(v,u,nw);
}else{
if(dep[v]<dep[u]){
int mne=Get(root[(nw%2)^1],1,n,dep[v]+1,dep[u]);
bestAdd=min(bestAdd,nw-mne);
}
}
}
Set(root[w%2],1,n,dep[u],-inf);
}
struct DSU{
int p[N];
void Init(int n){
for(int i=1;i<=n;i++)p[i]=i;
}
int Find(int x){return p[x]==x?x:p[x]=Find(p[x]);}
void Union(int x,int y){p[Find(x)]=Find(y);}
}dsu;
int main(){
int t;
scanf("%i",&t);
while(t--){
int m;
scanf("%i %i",&n,&m);
ll mst=0;
vector<array<int,4>> edges;
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%i %i %i",&u,&v,&w);
E[u].pb({v,i,w});
E[v].pb({u,i,w});
edges.pb({w,i,u,v});
inMst[i]=false;
}
sort(edges.begin(),edges.end());
dsu.Init(n);
int cnt=0;
for(auto e:edges){
int u=e[2];
int v=e[3];
if(dsu.Find(u)!=dsu.Find(v)){
mst+=e[0];
inMst[e[1]]=true;
cnt++;
dsu.Union(u,v);
}
}
if(cnt!=n-1){
printf("-1 -1\n");
}else{
bestAdd=inf;
Build(root[0],1,n);
Build(root[1],1,n);
DFS(1,0,0);
ll other=bestAdd==inf?-1:mst+bestAdd;
if(mst%2==0){
printf("%lld %lld\n",mst,other);
}else{
printf("%lld %lld\n",other,mst);
}
}
for(int i=1;i<=n;i++){
E[i].clear();
}
for(int i=1;i<=tsz;i++){
ls[i]=rs[i]=0;
mx[i]=-inf;
}
tsz=0;
root[0]=root[1]=0;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11228kb
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: 121ms
memory: 14388kb
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 1314022773 1263932600 1307926149 1189242112 1180186165 2248358640 2277656157 3776889482 3738936375 1093500444 1058677117 3433711014 3402127725 1201099898 1187873655 1395482806 1411327105 3456885934 3463206563 3943951826 4005009799 2479987500 2535532661 2909126794 279...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '3191118501'