QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#343137#8253. Very Important EdgeElTeeran_ElHayga#TL 2152ms185680kbC++204.0kb2024-03-01 23:00:322024-03-01 23:00:34

Judging History

This is the latest submission verdict.

  • [2024-03-01 23:00:34]
  • Judged
  • Verdict: TL
  • Time: 2152ms
  • Memory: 185680kb
  • [2024-03-01 23:00:32]
  • Submitted

answer

#include<bits/stdc++.h>

#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll

using namespace std;

void Drakon() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef Clion
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}

unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25;

ll mul(const ll &a, const ll &b) {
    return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
}

ll add(const ll &a, const ll &b) {
    return (a + b + 2 * MOD) % MOD;
}

ll pw(ll x, ll y) {
    ll ret = 1;
    while (y > 0) {
        if (y % 2 == 0) {
            x = mul(x, x);
            y = y / 2;
        } else {
            ret = mul(ret, x);
            y = y - 1;
        }
    }
    return ret;
}

struct DSU {
    vector<int>par, sz;

    DSU(int n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); }

    int find(int x){
        if(x == par[x])return x;
        return par[x] = find(par[x]);
    }

    bool join(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y)return false;
        if(sz[x] < sz[y])
            swap(x, y);
        sz[x] += sz[y];
        par[y] = x;
        return true;
    }
};

vector<pair<int, pair<int, int>>>edges;
vector<pair<int, int>>adj[N];
int depth[N], up[N][LOG], n, m, timer, tin[N], tout[N];


void dfs(int u, int p) {
    tin[u] = timer++;
    for (auto v: adj[u]) {
        if (v.first == p)continue;
        depth[v.first] = depth[u] + 1;
        up[v.first][0] = u;
        dfs(v.first, u);
    }
    tout[u] = timer - 1;
}

int LCA(int u, int v) {
    if (depth[u] < depth[v])
        swap(u, v);
    int k = depth[u] - depth[v];
    for (int i = 0; i < LOG; ++i) {
        if ((1 << i) & k) {
            u = up[u][i];
        }
    }
    if (u == v)
        return u;
    for (int i = LOG - 1; i >= 0; --i) {
        if (up[u][i] != up[v][i]) {
            u = up[u][i];
            v = up[v][i];
        }
    }
    return up[u][0];
}

void build() {
    dfs(0, 0);
    for (int j = 1; j < LOG; ++j) {
        for (int i = 0; i < n; ++i) {
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
}

multiset<int>toAdd[N], toRemove[N];
ll mx;

void calc(int u, int p, int w){

    for(auto v : adj[u]){
        if(v.first == p)continue;
        calc(v.first, u, v.second);
        if(toAdd[v.first].size() > toAdd[u].size())swap(toAdd[v.first], toAdd[u]);
        for(auto x : toAdd[v.first])toAdd[u].insert(x);
        toAdd[v.first].clear();
    }

    for(auto x : toRemove[u]){
        toAdd[u].erase(toAdd[u].find(x));
        toAdd[u].erase(toAdd[u].find(x));
    }

    if(!toAdd[u].empty() && u){
        mx = max(mx, 1ll * *toAdd[u].begin() - w);
    }
}

void solve() {
    cin >> n >> m;

    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        u--,v--;
        edges.push_back({w, {u, v}});
    }

    sort(all(edges));
    DSU dsu(n);
    ll ans = 0;
    vector<bool>vis(m);
    for (int i = 0; i < m; ++i) {
        int u = edges[i].second.first, v = edges[i].second.second;
        if(dsu.join(u, v)){
            adj[u].pp({v, edges[i].first});
            adj[v].pp({u, edges[i].first});
            ans += edges[i].first;
            vis[i] = true;
        }
    }

    build();

    for (int i = 0; i < m; ++i) {
        if(vis[i])continue;
        int u = edges[i].second.first, v = edges[i].second.second, w = edges[i].first;
        toAdd[u].insert(w);
        toAdd[v].insert(w);
        toRemove[LCA(u, v)].insert(w);
    }

    calc(0, 0, 0);
    cout << ans + mx;
}

signed main() {
    Drakon();
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1804ms
memory: 100432kb

input:

1000 499500
26 715 732723
850 918 507404
276 643 372190
67 702 972051
397 777 337003
178 185 863117
61 151 943008
83 581 731622
99 501 3260
301 588 948638
17 908 147104
193 480 94512
347 415 416562
519 912 627431
70 959 86892
333 573 757685
129 197 181261
224 636 259176
335 426 567962
193 339 70097
...

output:

1121154

result:

ok single line: '1121154'

Test #2:

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

input:

3 3
1 2 1
2 3 2
1 3 2

output:

4

result:

ok single line: '4'

Test #3:

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

input:

4 5
2 3 5
1 2 2
1 3 4
1 4 2
3 4 3

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 2ms
memory: 24256kb

input:

5 7
2 5 8
1 3 19
4 5 9
1 5 15
1 2 14
3 4 16
2 4 15

output:

54

result:

ok single line: '54'

Test #5:

score: 0
Accepted
time: 1719ms
memory: 101068kb

input:

1000 499500
857 965 96030
394 688 78612
440 754 495692
48 297 76650
206 975 200873
790 854 325696
307 384 472048
94 958 278751
601 806 136479
880 988 278407
265 813 315222
114 470 208185
172 332 425333
504 669 12025
266 315 400789
61 894 120668
675 972 228141
604 855 494399
116 496 234186
116 888 21...

output:

627123

result:

ok single line: '627123'

Test #6:

score: 0
Accepted
time: 9ms
memory: 25252kb

input:

1000 10000
894 939 776049
780 873 504700
126 161 227849
221 391 589404
562 661 697593
8 495 975484
13 527 481938
711 908 92209
147 616 682518
117 849 292092
231 463 868315
296 372 308904
458 886 970257
44 415 858179
2 352 402538
340 431 276296
87 744 48795
30 146 526326
35 109 788908
551 742 146887
...

output:

64112840

result:

ok single line: '64112840'

Test #7:

score: 0
Accepted
time: 2152ms
memory: 185680kb

input:

100000 1000000
3496 42038 2
23760 54893 2
40179 73909 2
18246 58964 2
59829 97488 2
46535 89743 2
43598 88684 2
10025 15117 2
25372 39316 2
55988 99623 2
12242 94927 2
91339 99004 2
68898 82761 2
19117 49957 2
24435 85140 2
15302 78102 2
9038 89450 2
82914 88120 2
6227 67500 2
5298 26787 2
27614 518...

output:

100000

result:

ok single line: '100000'

Test #8:

score: 0
Accepted
time: 115ms
memory: 52316kb

input:

100000 150000
10397 97917 1
81023 97767 2
27616 48830 2
17714 90000 2
34151 34285 1
6030 96304 1
50786 80688 1
30193 63737 1
25856 97783 1
735 46702 2
24633 79153 1
27172 85261 1
18963 27646 1
68548 74906 1
45452 65265 2
20050 96249 1
42929 94752 1
30314 52715 2
17457 32389 1
79882 95851 1
20932 436...

output:

100000

result:

ok single line: '100000'

Test #9:

score: 0
Accepted
time: 226ms
memory: 60756kb

input:

1000 249502
53 877 1
475 560 1
559 895 1
672 838 1
68 950 1
105 805 1
636 879 1
597 991 1
601 738 1
26 194 1
169 920 1
47 787 1
470 882 1
253 734 1
454 803 1
302 998 1
523 678 1
191 415 1
279 687 1
261 595 1
373 780 1
28 977 1
393 412 1
315 676 1
6 474 1
142 246 1
164 413 1
548 960 1
316 849 1
157 8...

output:

1000

result:

ok single line: '1000'

Test #10:

score: 0
Accepted
time: 8ms
memory: 22272kb

input:

6 8
1 2 1
4 5 2
1 3 3
4 6 4
2 3 5
5 6 6
1 4 7
2 5 1000

output:

1010

result:

ok single line: '1010'

Test #11:

score: -100
Time Limit Exceeded

input:

100000 1000000
12367 40267 217042
44689 75139 285000
27281 61254 650783
64350 65848 90802
31547 46704 668855
30690 87250 123830
55229 89914 134555
43197 77248 447563
59620 64482 84718
67546 90659 107138
87307 99160 861726
36383 55308 767446
1561 33387 137971
24268 29109 347562
15682 32464 829425
175...

output:


result: