QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276573#6520. Classic Problemucup-team1321WA 1136ms58632kbC++203.3kb2023-12-05 22:59:242023-12-05 22:59:26

Judging History

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

  • [2023-12-05 22:59:26]
  • 评测
  • 测评结果:WA
  • 用时:1136ms
  • 内存:58632kb
  • [2023-12-05 22:59:24]
  • 提交

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;
        while(pre[i]>=1&&vis.count({i,pre[i]})) pre[i]--;
        while(suf[i]<=tot&&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--;
            while(getf(pre[u])==getf(u)||vis.count({u,pre[u]}))
            {
                int t=pre[pre[u]];
                pre[u]=t;
            }
            while(getf(suf[u])==getf(u)||vis.count({u,suf[u]}))
            {
                int t=suf[suf[u]];
                suf[u]=t;
            }
            while(getf(pre[v])==getf(v)||vis.count({v,pre[v]}))
            {
                int t=pre[pre[v]];
                pre[v]=t;
            }
            while(getf(suf[v])==getf(v)||vis.count({v,suf[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: 22444kb

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: 1136ms
memory: 58632kb

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
1018159536
999999681
999900001
999985767
279
11
2616
2047
1624
5433
21
831
1075
8

result:

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