QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#666286#8941. Even or Odd Spanning Treelytqwq#TL 340ms27736kbC++142.8kb2024-10-22 17:33:092024-10-22 17:33:20

Judging History

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

  • [2024-10-22 17:33:20]
  • 评测
  • 测评结果:TL
  • 用时:340ms
  • 内存:27736kb
  • [2024-10-22 17:33:09]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mk make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fo(i,x,y) for(int i=x;i<=y;++i)
#define go(i,x,y) for(int i=x;i>=y;--i)
#define re return
#define co continue
#define sml(x,y) x=min(x,y)
#define big(x,y) x=max(x,y)
#define ll long long
using namespace std;

int read(){
    int x=0,f=1;
    int ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=x*10+(ch^48);
        ch=getchar();
    }
    re x*f;
}

const int N=5e5+5;
const ll INF=1e18;
struct Edge{
    int x,y,v;
    bool operator<(const Edge &A){re v<A.v;}
}e[N];
int n,m,ban[N];

namespace Dsu{
int f[N];
void init(){fo(i,1,n) f[i]=i;}
int fin(int x){re f[x]==x?x:fin(f[x]);}
bool merge(int x,int y){
    int fx=fin(x),fy=fin(y);
    if(fx==fy) re 0;
    f[fx]=fy;
    re 1;
}
}

namespace Tree{
const int inf=1e9;
vector<pii> e[N];
void add(int x,int y,int v){
    e[x].eb(y,v);
    e[y].eb(x,v);
}
int lg[N],fa[N][21],dep[N];
int mx[N][21][2];

void init(){
    fo(i,1,n) fa[i][0]=dep[i]=0,e[i].clear();
}

void dfs(int x){
    dep[x]=dep[fa[x][0]]+1;
    fo(i,1,lg[n]){
        fa[x][i]=fa[fa[x][i-1]][i-1];
        fo(o,0,1) mx[x][i][o]=max(mx[x][i-1][o],mx[fa[x][i-1]][i-1][o]);
    }
    for(auto i:e[x]) if(i.fi!=fa[x][0]){
        fa[i.fi][0]=x;
        mx[i.fi][0][i.se&1]=i.se;
        mx[i.fi][0][(i.se&1)^1]=0;
        dfs(i.fi);
    }
}

int ask(int x,int y,int o){
    if(dep[x]<dep[y]) swap(x,y);
    int ret=0;
    go(i,lg[n],0) if(dep[fa[x][i]]>=dep[y]) big(ret,mx[x][i][o]),x=fa[x][i];
    if(x==y) re ret;
    go(i,lg[n],0) if(fa[x][i]!=fa[y][i]){
        big(ret,mx[x][i][o]),big(ret,mx[y][i][o]);
        x=fa[x][i],y=fa[y][i];
    }
    big(ret,max(mx[x][0][o],mx[y][0][o]));
    re ret;
}
}

void solve(){
    cin>>n>>m;Dsu::init();Tree::init();
    fo(i,1,m) ban[i]=0;
    fo(i,1,m) e[i].x=read(),e[i].y=read(),e[i].v=read();
    sort(e+1,e+1+m);
    ll ans1=0,ans2=INF;
    int cnt=0;
    fo(i,1,m){
        int x=e[i].x,y=e[i].y;
        if(!Dsu::merge(x,y)) co;
        Tree::add(x,y,e[i].v);
        ans1+=e[i].v;
        ban[i]=1;cnt++;
    }
    if(cnt<n-1){puts("-1 -1");re;}
    Tree::dfs(1);
    fo(i,1,m) if(!ban[i]){
        int qwq=Tree::ask(e[i].x,e[i].y,(e[i].v&1)^1);
        if(qwq<=0) co;
        sml(ans2,ans1-qwq+e[i].v);
    }
    if(ans2<INF) assert((ans1&1)^(ans2&1));
    if(ans1&1) swap(ans1,ans2);
    cout<<(ans1<INF?ans1:-1)<<' '<<(ans2<INF?ans2:-1)<<'\n';
}

signed main(){
    fo(i,2,N-1) Tree::lg[i]=Tree::lg[i>>1]+1;
    int T=read();
    while(T--) solve();
    re 0;
}
/*
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
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 26812kb

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: 70ms
memory: 17376kb

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: 340ms
memory: 27736kb

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: -100
Time Limit Exceeded

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

result: