QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416180#6520. Classic ProblemLynkcatTL 3ms41352kbC++203.5kb2024-05-21 16:53:472024-05-21 16:53:49

Judging History

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

  • [2024-05-21 16:53:49]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:41352kb
  • [2024-05-21 16:53:47]
  • 提交

answer

#include<bits/stdc++.h>
#define poly vector<int>
#define IOS ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define mt make_tuple
#define pa pair < int,int >
#define fi first
#define se second
#define inf 1e18
#define mod 998244353
#define sz(x) ((int)((x).size()))
#define int ll
// #define N 
using namespace std;
const int N=500005;
int n,m;
int fa[N];
pa all[N];
int U[N],V[N],W[N];
vector<pa>G[N];
pa mn[N];
unordered_map<int,int>ise[N];
inline int gf(int x)
{
    while (x!=fa[x]) x=fa[x]=fa[fa[x]];
    return x;
}
void BellaKira()
{
    poly g;
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        cin>>U[i]>>V[i]>>W[i];
        g.push_back(U[i]);
        g.push_back(V[i]);
    }
    sort(g.begin(),g.end());
    g.erase(unique(g.begin(),g.end()),g.end());
    int lst=0;
    int l_n=0;
    for (auto u:g)
    {
        if (u>lst+1) all[++l_n]=mp(lst+1,u-1);
        all[++l_n]=mp(u,u);
        lst=u;
    }
    if (lst!=n) 
        all[++l_n]=mp(lst+1,n);
    n=l_n;

    for (int i=1;i<=m;i++)
    {
        int x=lower_bound(all+1,all+n+1,mp(U[i],U[i]))-all;
        int y=lower_bound(all+1,all+n+1,mp(V[i],V[i]))-all;
        G[x].push_back(mp(y,i));
        G[y].push_back(mp(x,i));
        ise[x][y]=1;
        ise[y][x]=1;
    }


    int ans=0;
    for (int i=1;i<=n;i++) fa[i]=i;
    for (int i=1;i<=n;i++) ans+=all[i].se-all[i].fi;
    while (1)
    {
        bool bl=1;
        for (int i=1;i<=n;i++)
            bl&=(gf(i)==gf(1));
        if (bl) break;
        for (int i=1;i<=n;i++)
            mn[i]=mp(inf,0);
        int lst=0;
        for (int i=2;i<=n;i++)
        {
            if (gf(i)!=gf(i-1)) lst=i-1;
            if (lst)
            {
                int op=lst;
                while (lst&&ise[i].count(lst)) lst--;
                if (lst)
                {
                    pa nw=mp(all[i].fi-all[lst].se,(lst-1)*n+i);
                    mn[gf(i)]=min(mn[gf(i)],nw);
                }
                lst=op;
            }
        }
        lst=0;
        for (int i=n-1;i>=1;i--)
        {
            if (gf(i)!=gf(i+1)) lst=i+1;
            if (lst)
            {
                int op=lst;
                while (lst<=n&&ise[i].count(lst)) lst++;
                if (lst<=n)
                {
                    pa nw=mp(-all[i].se+all[lst].fi,(i-1)*n+lst);
                    mn[gf(i)]=min(mn[gf(i)],nw);
                }
                lst=op;
            }
        }
        for (int i=1;i<=n;i++)
        {
            for (auto [u,id]:G[i])
            {
                int w=W[id];
                if (gf(i)!=gf(u)) mn[gf(i)]=min(mn[gf(i)],mp(w,n*n+id));
            }
        }
        for (int i=1;i<=n;i++)
        if (gf(i)==i)
        {
            int id=mn[i].se;
            int x,y;
            if (id<=n*n) x=(id-1)/n+1,y=(id-1)%n+1;
            else x=U[id-n*n],y=V[id-n*n];
            if (gf(x)!=gf(y))
            {
                // cout<<x<<","<<y<<" "<<mn[i].fi<<" "<<mn[i].se<<endl;
                ans+=mn[i].fi;
                fa[gf(x)]=gf(y);
            }
        }
        // cout<<"-----"<<endl;
    }
    cout<<ans<<'\n';
    for (int i=1;i<=n;i++) vector<pa>().swap(G[i]),unordered_map<int,int>().swap(ise[i]);
}
signed main()
{
    IOS;
    cin.tie(0);
    int T=1;
    cin>>T;
    while (T--)
    {
        BellaKira();
    }
}
/*list:
1.mod 998244353 or 1e9+7 or ???
2.N
3.duipai shuju xingtai duoyidian
...
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 41352kb

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

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:


result: