QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282082#7871. 命运hos_lyric#25 15ms3896kbC++143.5kb2023-12-11 12:45:512024-07-04 03:12:17

Judging History

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

  • [2024-07-04 03:12:17]
  • 评测
  • 测评结果:25
  • 用时:15ms
  • 内存:3896kb
  • [2023-12-11 12:45:51]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


vector<int> uf;
int root(int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
  u = root(u);
  v = root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


int N, M, K, S;
vector<pair<int, pair<int, int>>> E;
vector<int> A, B, C;

vector<int> on;
bool check() {
// cerr<<"check "<<on<<endl;
  uf.assign(N, -1);
  for (int i = 0; i < M; ++i) if (on[i]) if (A[i] != S) {
    connect(A[i], B[i]);
  }
  int cnt = 0;
  vector<int> has(N, 0);
  for (int i = 0; i < M; ++i) if (on[i]) if (A[i] == S) {
    ++cnt;
    has[root(B[i])] = 1;
  }
  if (cnt < K) return false;
  int numComps = 0;
  for (int r = 0; r < N; ++r) if (uf[r] < 0) if (r != S) {
    ++numComps;
    if (!has[r]) return false;
  }
  if (numComps > K) return false;
  return true;
}

int main() {
  for (; ~scanf("%d%d%d%d", &N, &M, &K, &S); ) {
    --S;
    E.resize(M);
    for (int i = 0; i < M; ++i) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      --a;
      --b;
      if (b == S) {
        swap(a, b);
      }
      E[i] = make_pair(c, make_pair(a, b));
    }
    sort(E.begin(), E.end());
    A.resize(M);
    B.resize(M);
    C.resize(M);
    for (int i = 0; i < M; ++i) {
      A[i] = E[i].second.first;
      B[i] = E[i].second.second;
      C[i] = E[i].first;
    }
// cerr<<"E = "<<E<<endl;
    
    /*
      - no multi-edge from S
      - not in MST other than S
    */
    vector<int> ban(M, 0);
    {
      vector<int> used(N, 0);
      for (int i = 0; i < M; ++i) if (A[i] == S) {
        if (used[B[i]]) ban[i] = 1;
        used[B[i]] = 1;
      }
    }
    {
      uf.assign(N, -1);
      for (int i = 0; i < M; ++i) if (A[i] != S) {
        if (!connect(A[i], B[i])) {
          ban[i] = 1;
        }
      }
    }
    
assert(N<=10000);
    on.resize(M);
    for (int i = 0; i < M; ++i) {
      on[i] = ban[i] ^ 1;
    }
    if (check()) {
      for (int i = M; --i >= 0; ) if (!ban[i]) {
        on[i] = 0;
        if (!check()) {
          on[i] = 1;
        }
      }
      Int ans = 0;
      for (int i = 0; i < M; ++i) if (on[i]) {
        ans += C[i];
      }
      printf("%lld\n", ans);
    } else {
      puts("nonnondayo");
    }
  }
  return 0;
}

詳細信息

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

49277 49276 1 49277
1 2 2000
2 3 3000
2 4 4000
3 5 5000
3 6 6000
4 7 7000
1 8 8000
7 9 9000
1 10 10000
5 11 11000
4 12 12000
3 13 13000
13 14 14000
1 15 15000
7 16 16000
11 17 17000
2 18 18000
10 19 19000
6 20 20000
4 21 21000
17 22 22000
1 23 23000
14 24 24000
4 25 25000
16 26 26000
15 27 27000
9 2...

output:


result:


Subtask #2:

score: 10
Accepted

Test #5:

score: 10
Accepted
time: 0ms
memory: 3668kb

input:

21 20 2 21
1 2 18581
1 3 9620
2 4 4362
1 5 7702
5 6 17435
4 7 11679
3 8 14832
1 9 15073
2 10 6595
5 11 19676
11 12 16943
12 13 5268
5 14 20262
8 15 8192
7 16 12340
7 17 1437
7 18 3064
2 19 10437
12 20 2882
19 21 13483

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #6:

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

input:

10 20 4 3
1 2 18247
2 3 9041
2 4 4031
2 5 7363
3 6 17172
1 7 11014
6 8 14781
8 9 15347
8 10 6598
5 9 19331
3 10 16523
10 6 5732
2 3 20357
6 9 8827
10 3 12864
6 3 1551
5 6 3932
3 1 10223
9 3 2296
8 5 13620

output:

54418

result:

ok single line: '54418'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3708kb

input:

10 20 6 10
1 2 18401
2 3 9843
3 4 4219
4 5 7552
4 6 17751
4 7 11318
5 8 14774
8 9 15541
5 10 6928
6 10 19860
10 5 16699
5 8 5937
5 2 20611
1 8 8077
10 1 12386
9 4 1663
9 10 3910
1 9 10401
7 10 2383
2 9 13681

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #8:

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

input:

10 20 1 10
1 2 18853
2 3 9034
3 4 4069
3 5 7743
3 6 17122
6 7 11715
2 8 14814
1 9 15011
7 10 6248
6 8 19400
7 3 16354
6 8 5249
7 8 20701
5 10 8079
10 5 12570
7 10 1006
8 3 3814
5 10 10753
5 9 2310
8 6 13123

output:

59951

result:

ok single line: '59951'

Test #9:

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

input:

10 11 3 1
1 2 9407
1 3 7005
1 4 5453
1 5 4585
1 6 8278
1 7 6332
1 8 1415
1 9 3809
1 10 10519
2 6 2507
10 6 11580

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Subtask #3:

score: 15
Accepted

Dependency #2:

100%
Accepted

Test #10:

score: 15
Accepted
time: 10ms
memory: 3848kb

input:

1000 3000 100 10
1 2 2270918
1 3 871400
2 4 1242131
3 5 2307909
5 6 1391239
3 7 1431327
7 8 2501929
5 9 2377716
8 10 612146
5 11 991199
11 12 1810465
10 13 1094558
10 14 2605381
8 15 872336
10 16 2092297
5 17 619719
14 18 1161002
5 19 1828768
10 20 837691
2 21 1787203
3 22 396276
21 23 1371664
16 24...

output:

645739778

result:

ok single line: '645739778'

Test #11:

score: 0
Accepted
time: 13ms
memory: 3896kb

input:

999 3000 200 20
1 2 2801619
1 3 1075541
2 4 1533685
3 5 2847120
2 6 1716689
6 7 1766365
5 8 3087429
4 9 2933519
1 10 755477
4 11 1223969
11 12 2233807
5 13 1350595
12 14 3215789
12 15 1076145
12 16 2581528
1 17 764941
5 18 1433263
12 19 2256409
3 20 1033257
20 21 2205421
11 22 489324
22 23 1692840
2...

output:

833854746

result:

ok single line: '833854746'

Test #12:

score: 0
Accepted
time: 14ms
memory: 3868kb

input:

999 3000 300 20
1 2 9811687
2 3 3764791
2 4 5370818
3 5 9972642
3 6 6014501
1 7 6187455
6 8 10808965
2 9 10272798
9 10 2646297
1 11 4285235
1 12 7821976
2 13 4727798
13 14 11258977
11 15 3768331
1 16 9040806
16 17 2678040
6 18 5018287
5 19 7901529
14 20 3617199
20 21 7721668
20 22 1713474
21 23 5927...

output:

3113047747

result:

ok single line: '3113047747'

Test #13:

score: 0
Accepted
time: 15ms
memory: 3868kb

input:

999 3000 10 20
1 2 9812725
2 3 3765199
3 4 5368570
1 5 9970744
2 6 6012531
5 7 6184148
6 8 10808057
6 9 10272720
4 10 2647288
4 11 4284300
5 12 7824819
4 13 4731348
6 14 11256433
1 15 3771674
6 16 9042651
16 17 2677352
2 18 5019297
2 19 7900432
9 20 3619670
9 21 7725236
20 22 1713962
20 23 5927083
2...

output:

2976768482

result:

ok single line: '2976768482'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

9992 99999 500 1
1 2 96661121
1 3 21915940
1 4 110026320
1 5 37433129
1 6 38560320
1 7 83209024
1 8 80352054
1 9 88957672
1 10 65449874
1 11 38239186
1 12 90153728
1 13 30810974
1 14 96493404
1 15 112886259
1 16 87190053
1 17 91182163
1 18 107303768
1 19 101439823
1 20 120601875
1 21 122197599
1 22 ...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Dependency #3:

100%
Accepted

Test #27:

score: 0
Time Limit Exceeded

input:

9999 99999 1 20
1 2 338469852
2 3 76743614
3 4 385273039
2 5 131073403
3 6 135023236
6 7 291367753
5 8 281362462
6 9 311495139
6 10 229178582
5 11 133894978
3 12 315685120
10 13 107890835
9 14 337882826
2 15 395283746
7 16 305305887
8 17 319285996
3 18 375737069
16 19 355206723
7 20 422300256
17 21 ...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%