QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642923#8941. Even or Odd Spanning TreeWolam#WA 97ms13940kbC++202.6kb2024-10-15 17:05:262024-10-15 17:05:28

Judging History

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

  • [2024-10-15 17:05:28]
  • 评测
  • 测评结果:WA
  • 用时:97ms
  • 内存:13940kb
  • [2024-10-15 17:05:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=0x3f3f3f3f3f3f3f3f;
int n,m;
vector<pair<int,int> > e[200005];
struct ss{
    int u,v,w;
    bool operator <(const ss ot)const{
        return w<ot.w;
    }
}a[500005];
int f[200005][18],mx[2][200005][18],d[200005];
int fa[200005];
int find(int x)
{
    if(x==fa[x])return x;
    return fa[x]=find(fa[x]);
}
int ans[2],vis[500005];
bool merge(int u,int v)
{
    u=find(u);v=find(v);
    if(u==v)return 0;
    fa[u]=v;
    return 1;
}
void dfs(int x,int ffa,int w)
{
    f[x][0]=ffa;
    mx[w&1][x][0]=w;
    mx[!(w&1)][x][0]=-inf;
    d[x]=d[ffa]+1;
    for(int i=1;i<=17;i++)
    {
        f[x][i]=f[f[x][i-1]][i-1];
        mx[0][x][i]=max(mx[0][x][i-1],mx[0][f[x][i-1]][i-1]);
        mx[1][x][i]=max(mx[1][x][i-1],mx[1][f[x][i-1]][i-1]);
    }
    for(auto [y,nw]:e[x])
    {
        if(y==ffa)continue;
        dfs(y,x,nw);
    }
}
int getmx(int u,int v,int typ)
{
    if(d[u]<d[v])
        swap(u,v);
    int nmx=-inf;
    for(int i=17;i>=0;i--)
        if(d[f[u][i]]>=d[v])
        {
            nmx=max(mx[typ][u][i],nmx);
            u=f[u][i];
        }
    if(u==v)return nmx;
    for(int i=17;i>=0;i--)
    {
        if(f[u][i]!=f[v][i])
        {
            nmx=max(mx[typ][u][i],nmx);
            nmx=max(mx[typ][v][i],nmx);
            u=f[u][i];v=f[v][i];
        }
    }
    nmx=max(mx[typ][u][0],nmx);
    nmx=max(mx[typ][v][0],nmx);
    return nmx;
}
void solve(void)
{
    cin>>n>>m;
    ans[0]=ans[1]=-1;
    for(int i=1;i<=n;i++)
        e[i].clear(),fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        cin>>a[i].u>>a[i].v>>a[i].w;
        vis[i]=0;
    }
    sort(a+1,a+m+1);
    int cnt=n;
    int sum=0;
    for(int i=1;i<=m;i++)
    {
        vis[i]=merge(a[i].u,a[i].v);
        if(vis[i])
        {
            e[a[i].u].push_back(make_pair(a[i].v,a[i].w));
            e[a[i].v].push_back(make_pair(a[i].u,a[i].w));
            cnt--;sum+=a[i].w;
        }
    }
    if(cnt==1)
    {
        dfs(1,0,0);
        int t=sum&1;
        ans[t]=sum;
        for(int i=1;i<=m;i++)
        {
            if(!vis[i])
            {
                int now=getmx(a[i].u,a[i].v,(a[i].w&1)^1);
                if(now!=-inf)
                    ans[t^1]=max(ans[t^1],sum+a[i].w-now);
            }
        }
    }
    cout<<ans[0]<<" "<<ans[1]<<'\n';
}
signed main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 97ms
memory: 13940kb

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 3941554631
1262790434 2124445815
1263932600 2177809565
2128472300 1180186165
2248358640 3162318131
4696867870 3738936375
2011213264 1058677117
4144333014 3402127725
2081445350 1187873655
1395482806 2324672773
3456885934 4302070719
3943951826 4702420591
2479987500 3216859457
2909126794 388...

result:

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