QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#296276#7991. 最小环stcmuyWA 328ms130108kbC++203.7kb2024-01-02 17:34:572024-01-02 17:34:58

Judging History

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

  • [2024-01-02 17:34:58]
  • 评测
  • 测评结果:WA
  • 用时:328ms
  • 内存:130108kb
  • [2024-01-02 17:34:57]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define i64 long long
#define i128 __int128
#define int long long
#define endl '\n'
using namespace std;
const i64 mod = 1e18;
const int maxn = 3e5+10;
vector<int> g[maxn];
int dfn[maxn];
int low[maxn],num;
int in[maxn];
int s[maxn],top;
int cnt;
int c[maxn];
void tarjan(int u)
{
    dfn[u] = low[u] = ++num;
    s[++top] = u;
    in[u] = 1;
    for(int &v : g[u])
    {
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else if(in[v]) low[u] = min(low[u],dfn[v]);
    }
    if(dfn[u] == low[u])
    {
        cnt++;
        int v;
        do
        {
            v = s[top--];
            c[v] = cnt;
            in[v] = 0;
        }
        while(v != u);
    }
}
struct node
{
    int u,v,w;
};
vector<node> xz[maxn];
i64 ck(int d)
{
    vector<int> b;
    for(node &a : xz[d])
    {
        b.push_back(a.u);
        b.push_back(a.v);
    }
    if(b.size() == 0) return mod;
    sort(b.begin(),b.end());
    b.erase(unique(b.begin(),b.end()),b.end());
    int len = b.size();
    vector<int> f(len+1);
    for(int i = 1; i <= len; ++i) f[i] = i;
    auto get = [&](auto self,int x) -> int
    {
        if(f[x] == x) return x;
        return f[x] = self(self,f[x]);
    };
    vector<vector<pair<int,int>>> v(len+1),e(len+1);
    sort(xz[d].begin(),xz[d].end(),[&](node a,node b) {  return a.w < b.w;});
    for(node &a : xz[d])
    {
        int x = lower_bound(b.begin(),b.end(),a.u) - b.begin() + 1;
        int y = lower_bound(b.begin(),b.end(),a.v) - b.begin() + 1;
        int s1 = get(get,x);
        int s2 = get(get,y);
        if(s1 != s2)
        {
            v[x].push_back({y,a.w});
            v[y].push_back({x,a.w});
            f[s1] = s2;
        }
        else
        {
            e[x].push_back({y,a.w});
            e[y].push_back({x,a.w});
        }
    }
    vector<vector<int>> ff(len+1,vector<int> (21));
    vector<i64> dis(len+1),deep(len+1);
    auto dfs = [&](auto self,int x,int fa) -> void
    {
        ff[x][0] = fa;
        deep[x] = deep[fa] + 1;
        for(int i = 0; i < 20; ++i) ff[x][i+1] = ff[ff[x][i]][i];
        for(auto [u,val] : v[x])
        {
            if(u == fa) continue;
            dis[u] = dis[x] + val;
            self(self,u,x);
        }
    };
    dfs(dfs,1,0);
    auto lca = [&](int x,int y)
    {
        if(deep[x] < deep[y]) swap(x,y);
        for(int i = 20; i >= 0; --i)
        {
            if(deep[ff[x][i]] >= deep[y]) x = ff[x][i];
            if(x == y) return x;
        }
        for(int i = 20; i >= 0; --i)
        {
            if(ff[x][i] != ff[y][i])
            {
                x = ff[x][i];
                y = ff[y][i];
            }
        }
        return ff[x][0];
    };
    i64 ans = mod;
    for(int i = 1; i <= len; ++i)
    {
        for(auto [u,val] : e[i])
        {
            int lc = lca(i,u);
            i64 sum = dis[i] + dis[u] - 2*dis[lc];
            ans = min(ans,sum + val);
        }
    }
    return ans;
}
signed main()
{
    IOS;
    int n,m; cin >> n >> m;
    vector<node> e;
    for(int i = 1; i <= m; ++i)
    {
        int x,y,z; cin >> x >> y >> z;
        e.push_back((node){x,y,z});
        g[x].push_back(y);
    }
    for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i);
    for(node &a : e)
    {
        if(c[a.u] == c[a.v]) xz[c[a.u]].push_back(a);
    }
    i64 ans = mod;
    for(int i = 1; i <= cnt; ++i) ans = min(ans,ck(i));
    if(ans == mod) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 21476kb

input:

4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 4ms
memory: 23148kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 21168kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 94ms
memory: 62468kb

input:

258420 258419
33061 33062 767169384
212916 212917 1741339
229881 229882 896760805
173467 173468 273055172
233189 233190 800433307
10157 10158 126766550
174605 174606 552176083
224030 224031 886617880
229102 229103 783848581
67588 67589 510826095
233648 233649 879695751
214453 214454 867104578
153140...

output:

-1

result:

ok single line: '-1'

Test #5:

score: -100
Wrong Answer
time: 328ms
memory: 130108kb

input:

248016 248896
82688 82755 592665252
202847 203260 294408121
26821 28237 269132335
150967 152178 3125829
246069 247390 29492546
7470 7673 55843492
33975 35414 442802995
28451 28948 193736988
34484 34637 84441058
60168 60309 501991354
79579 79844 26854803
239672 239706 111702667
73548 73658 149840530
...

output:

7143216234

result:

wrong answer 1st lines differ - expected: '98674714245', found: '7143216234'