QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#699104#8941. Even or Odd Spanning TreeLaynWA 192ms67724kbC++142.2kb2024-11-02 01:15:562024-11-02 01:15:56

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 01:15:56]
  • 评测
  • 测评结果:WA
  • 用时:192ms
  • 内存:67724kb
  • [2024-11-02 01:15:56]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=5e5+10;
int T,n,m,E,lg[N],head[N],rt[N],dep[N],link[M*2],to[M*2],val[M*2],f[N][21],g[N][21][2];
long long ans[2];
bool vis[N];
struct edge{int u,v,w;}e[M];
void init() {
    E=0,ans[0]=ans[1]=-1,lg[1]=0;
    for(int i=1;i<=n;i++)head[i]=0,rt[i]=i,f[i][0]=g[i][0][0]=g[i][0][1]=0;
    for(int i=2;i<=n;i++)lg[i]=lg[i/2]+1;
    for(int i=1;i<=m;i++)vis[i]=0;
}
int root(int x) {
    if(rt[x]==x)return x;
    return rt[x]=root(rt[x]);
}
bool cmp(edge x,edge y){return x.w<y.w;}
void add(int u,int v,int w){to[++E]=v,link[E]=head[u],head[u]=E,val[E]=w;}
int query(int u,int v,int o) {
    int res=0;
    while(dep[u]!=dep[v]) {
        if(dep[u]<dep[v])swap(u,v);
        int tmp=lg[dep[u]-dep[v]];
        res=max(res,g[u][tmp][o]),u=f[u][tmp];
    }if(u==v)return res;
    for(int i=19;i>=0;i--)if(f[u][i]!=f[v][i])
        res=max(res,max(g[u][i][o],g[v][i][o])),u=f[u][i],v=f[v][i];
    return max(res,max(g[u][0][o],g[v][0][o]));
}
void dfs(int u) {
    for(int i=1;i<20;i++)f[u][i]=f[f[u][i-1]][i-1],
        g[u][i][0]=max(g[f[u][i-1]][i-1][0],g[u][i-1][0]),
        g[u][i][1]=max(g[f[u][i-1]][i-1][1],g[u][i-1][1]);
    dep[u]=dep[f[u][0]]+1;
    for(int i=head[u];i;i=link[i]) {
        int v=to[i];
        if(v==f[u][0])continue;
        f[v][0]=u,g[v][0][val[i]&1]=val[i],dfs(v);
    }
}
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        sort(e+1,e+m+1,cmp);
        long long res=0,tmp;
        for(int i=1;i<=m;i++) {
            int u=e[i].u,v=e[i].v,w=e[i].w,ru=root(u),rv=root(v);
            if(ru==rv)continue;
            rt[rv]=ru,add(u,v,w),add(v,u,w),vis[i]=1,res+=w;
        }ans[res&1]=res,tmp=res,res=LLONG_MAX;
        if(E/2!=n-1){printf("-1 -1\n");continue;}
        dfs(1);
        for(int i=1;i<=m;i++)if(!vis[i]) {
            int u=e[i].u,v=e[i].v,w=e[i].w,cur=query(u,v,(w&1)^1);
            if(!cur)continue;
            res=min(res,tmp+w-cur);
        }if(res==LLONG_MAX)res=-1;
        if(ans[0]>=0)ans[1]=res;
        else ans[0]=res;
        printf("%lld %lld\n",ans[0],ans[1]);
    }
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 16232kb

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: 104ms
memory: 16144kb

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: 140ms
memory: 18388kb

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: 0
Accepted
time: 173ms
memory: 25028kb

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
3395129351174 3395129357911
4109233311456 4109233300341
4201582590330 4201582656213
4055209304322 4055209286787
4274470524320 4274470529139
4221591172170 4221591328195
3155641613234 3155641574871
3656162030214 3656162010817

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 192ms
memory: 67724kb

input:

1
200000 500000
1 2 485160054
2 3 698975729
3 4 100346974
4 5 820234671
5 6 389996728
6 7 914128102
7 8 439507064
8 9 938065130
9 10 353829140
2 11 391113348
7 12 685484428
8 13 492562017
7 14 269259412
4 15 133636977
7 16 189855044
16 17 393115842
9 18 196148136
13 19 272676317
1 20 778859832
12 21...

output:

46934564700640 46934564707403

result:

ok 2 number(s): "46934564700640 46934564707403"

Test #6:

score: -100
Wrong Answer
time: 180ms
memory: 67048kb

input:

1
200000 500000
1 2 825868392
2 3 96775645
3 4 594837991
4 5 160657859
5 6 313806062
6 7 711860421
7 8 618363510
8 9 784016759
9 10 261473589
10 11 931365544
11 12 372625381
12 13 728196426
13 14 641630335
14 15 993361561
15 16 731849490
16 17 802998444
17 18 222098249
18 19 16513016
19 20 354524457...

output:

281474993487872 46801911175000

result:

wrong answer 1st numbers differ - expected: '46801911175000', found: '281474993487872'