QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276568#6520. Classic Problemucup-team1321WA 760ms56828kbC++203.3kb2023-12-05 22:56:232023-12-05 22:56:24

Judging History

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

  • [2023-12-05 22:56:24]
  • 评测
  • 测评结果:WA
  • 用时:760ms
  • 内存:56828kb
  • [2023-12-05 22:56:23]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<tuple>
#include<map>
#include<algorithm>
using namespace std;
const int M=400005;
const int INF=1061109567;
int n,m;
struct Edge
{
    int u,v,w;
}e[M];
int b[M],cnt;
int pre[M],suf[M];
pair<int,int>p[M];
int tot;
int fa[M];
int getf(int v)
{
    if(v==fa[v]) return v;
    else return fa[v]=getf(fa[v]);
}
vector<pair<int,int>>G[M];
map<pair<int,int>,bool>vis;
void solve()
{
    cin>>n>>m;
    cnt=0;
    for(int i=1;i<=m;i++)
    {
        cin>>e[i].u>>e[i].v>>e[i].w;
        b[++cnt]=e[i].u,b[++cnt]=e[i].v;
    }
    if(m==0)
    {
        cout<<n-1<<"\n";
        return;
    }
    sort(b+1,b+cnt+1);
    cnt=unique(b+1,b+cnt+1)-b-1;
    b[0]=0;
    long long ans=0;
    tot=0; 
    for(int i=1;i<=cnt;i++)
    {
        if(b[i-1]+1<=b[i]-1) p[++tot]={b[i-1]+1,b[i]-1},ans+=b[i]-1-(b[i-1]+1);
        p[++tot]={b[i],b[i]};
    }
    if(b[cnt]+1<=n) p[++tot]={b[cnt]+1,n},ans+=n-(b[cnt]+1);
    vis.clear();
    for(int i=1;i<=tot;i++)
        G[i].clear();
    for(int i=1;i<=m;i++)
    {
        int u=lower_bound(p+1,p+tot+1,make_pair(e[i].u,e[i].u))-p,v=lower_bound(p+1,p+tot+1,make_pair(e[i].v,e[i].v))-p,w=e[i].w;
        G[u].emplace_back(v,w);
        G[v].emplace_back(u,w);
        vis[{u,v}]=vis[{v,u}]=true;
    }
    for(int i=1;i<=tot;i++)
    {
        pre[i]=i-1,suf[i]=i+1;
        if(p[i].first==p[i].second)
        {
            while(pre[i]>=1&&p[pre[i]].first==p[pre[i]].second&&vis.count({i,pre[i]})) pre[i]--;
            while(suf[i]<=tot&&p[suf[i]].first==p[suf[i]].second&&vis.count({i,suf[i]})) suf[i]++;
        }
    }
    for(int i=1;i<=tot;i++)
        fa[i]=i;
    for(int t=tot;t>1;)
    {
        vector<tuple<int,int,int>>edge;
        for(int u=1;u<=tot;u++)
        {
            int val=INF,to=0;
            for(auto [v,w]:G[u])
                if(getf(v)!=getf(u)&&w<=val) to=v,val=w;
            if(pre[u]>=1)
            {
                int v=pre[u];
                int w=p[u].first-p[v].second;
                if(w<=val) to=v,val=w;
            }
            if(suf[u]<=tot)
            {
                int v=suf[u];
                if(v>tot) exit(1); 
                int w=p[v].first-p[u].second;
                if(w<=val) to=v,val=w;
            }
            if(to) edge.emplace_back(u,to,val);
        }
        for(auto [u,v,w]:edge)
        {
            if(u>v) swap(u,v);
            int fu=getf(u),fv=getf(v);
            if(fu==fv) continue;
            fa[fv]=fu;
            ans+=w;
            t--;
            if(getf(pre[u])==getf(u))
            {
                int t=pre[pre[u]];
                pre[u]=t;
            }
            if(getf(suf[u])==getf(u))
            {
                int t=suf[suf[u]];
                suf[u]=t;
            }
            if(getf(pre[v])==getf(v))
            {
                int t=pre[pre[v]];
                pre[v]=t;
            }
            if(getf(suf[v])==getf(v))
            {
                int t=suf[suf[v]];
                suf[v]=t;
            }
        }
    }
    cout<<ans<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T;
    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: 12836kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: -100
Wrong Answer
time: 760ms
memory: 56828kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
1051276313
999999681
999900001
999985767
279
11
2616
2047
1624
5433
21
831
1075
8

result:

wrong answer 3rd numbers differ - expected: '732256441', found: '1051276313'