QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619750#8941. Even or Odd Spanning Treezzpcd#WA 126ms13836kbC++203.3kb2024-10-07 15:15:412024-10-07 15:15:49

Judging History

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

  • [2024-10-07 15:15:49]
  • 评测
  • 测评结果:WA
  • 用时:126ms
  • 内存:13836kb
  • [2024-10-07 15:15:41]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fll
#define P make_pair
using namespace std;
int T;
int n,m;
ll ans[2],now;
vector<pair<int,int> > road[200011];
pair<int ,pair<int,int>>  s[2][500011];
int cnt[2];
priority_queue<pair<int ,pair<int,int>> > pq;
int bz[200011][22],mx[2][200011][22],dep[200011];
int fa[200011];
int find_(int x)
{
    return fa[x]=fa[x]==x?x:find_(fa[x]);
}
void dfs(int x,int f,int v)
{
    dep[x]=dep[f]+1;
    bz[x][0]=f;
    mx[v%2][x][0]=v;
    for(int t=1;t<=21;++t)
    {
        bz[x][t]=bz[bz[x][t-1]][t-1];
        mx[0][x][t]=max(mx[0][x][t-1],mx[0][bz[x][t-1]][t-1]);
        mx[1][x][t]=max(mx[1][x][t-1],mx[1][bz[x][t-1]][t-1]);
    }
    for(auto v:road[x])
    {
        if(v.first==f) continue;
        dfs(v.first,x,v.second);
    }
}
int check(int x,int y,int opt)
{
    if(dep[x]<dep[y]) swap(x,y);
    int ret=0;
    for(int i=21;i>=0;--i)
    {
        if(dep[bz[x][i]]>=dep[y])
        {
            ret=max(ret,mx[opt][x][i]);
            x=bz[x][i];
        }
    }
    if(x==y) return ret;
    for(int i=21;i>=0;--i)
    {
        if(bz[x][i]!=bz[y][i])
        {
            ret=max(ret,mx[opt][x][i]);
            ret=max(ret,mx[opt][y][i]);
            x=bz[x][i];
            y=bz[y][i];
        }
    }
    ret=max(ret,max(mx[opt][x][0],mx[opt][y][0]));
    return ret;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1,u,v,w;i<=m;++i)
        {
            cin>>u>>v>>w;
            pq.push(P(-w,P(u,v)));
        }
        for(int i=1;i<=n;++i) fa[i]=i,road[i].clear(),dep[i]=0;
        now=0;
        ans[0]=ans[1]=inf;
        int tot=n;
        cnt[0]=cnt[1]=0;
        for(int t=0;t<2;++t)
        {
            for(int i=1;i<=n;++i)
            {
                for(int j=0;j<=21;++j)
                {
                    bz[i][j]=mx[t][i][j]=0;
                }
            }
        }
        while(pq.size())
        {
            int a=-pq.top().first,u=pq.top().second.first,v=pq.top().second.second;
            pq.pop();
            if(find_(u)!=find_(v))
            {
                fa[find_(u)]=find_(v);
                road[u].push_back(P(v,a));
                tot--;
                now+=a;
            }
            else
            {
                s[a%2][++cnt[a%2]]=P(a,P(u,v));
                // cout<<a%2<<"     "<<a<<"  "<<u<<" "<<v<<endl;
            }
        }
        // cout<<tot<<" ada wd  "<<now<<endl;
        if(tot!=1)
        {
            cout<<-1<<" "<<-1<<endl;
        }
        else
        {
            ans[now%2]=now;
            dfs(1,1,0);
            for(int t=0;t<2;++t)
            for(int i=1;i<=cnt[t];++i)
            {
                int mx=check(s[t][i].second.first,s[t][i].second.second,t^1);
                // cout<<(t^1)<<" "<<s[t][i].second.first<<" "<<s[t][i].second.second<<" "<<"      "<<mx<<endl;
                if(mx) ans[!(now%2)]=min(ans[!(now%2)],now-mx+s[t][i].first);
            }
            if(ans[0]==inf) ans[0]=-1;
            if(ans[1]==inf) ans[1]=-1;
            cout<<ans[0]<<" "<<ans[1]<<endl;
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 126ms
memory: 13836kb

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 3249730195
1262790434 1293301117
1263932600 1408389989
-1 1180186165
2248358640 -1
-1 3738936375
1644846066 1058677117
3444549292 3402127725
1561252020 1187873655
1395482806 -1
3456885934 3714296369
3943951826 4066602781
2479987500 2494911351
2909126794 2948711665
2483103706 -1
2147279070...

result:

wrong answer 2nd numbers differ - expected: '3159441841', found: '3249730195'