UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#205084#3645. 单调图LJ071005308ms13804kbC++112.7kb2024-06-23 08:32:372024-06-23 13:01:50

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define pb emplace_back
#define sz(a) int(a.size())

const int N = 1e5 + 5;
int n, m, fa[N], ans[N];
array<int, 3> dis[N];
vector<pair<int, int>> G[N];
vector<int> id, disc;

inline int askd(int x) {
  return lower_bound(disc.begin(), disc.end(), x) - disc.begin() + 1;
}

int tr[N];
void add(int x, int y) {
  for (; x <= sz(disc); x += x & -x) {
    tr[x] += y;
  }
}
int ask(int x) {
  int y = 0;
  for (; x; x -= x & -x) {
    y += tr[x];
  }
  return y;
}

void dij(int s, int id) {
  for (int i = 1; i <= n; ++i) {
    dis[i][id] = 1e9;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
  Q.emplace(dis[s][id] = 0, s);
  while (sz(Q)) {
    auto i = Q.top();
    auto d = i.first;
    auto u = i.second;
    Q.pop();
    if (d != dis[u][id]) {
      continue;
    }
    for (auto j : G[u]) {
      auto v = j.first;
      auto w = j.second;
      if (dis[v][id] > d + w) {
        Q.emplace(dis[v][id] = d + w, v);
      }
    }
  }
}

#define cmp(o) [&](int u, int v) {\
  for (auto k : {o, 0, 1, 2}) {\
    if (dis[u][k] < dis[v][k]) {\
      return true;\
    }\
    if (dis[u][k] > dis[v][k]) {\
      return false;\
    }\
  }\
  return false;\
}

void cdq(int l, int r) {
  if (l == r) {
    return ;
  }
  int mid = (l + r) >> 1;
  cdq(l, mid), cdq(mid + 1, r);
  sort(id.begin() + l, id.begin() + mid + 1, cmp(1));
  sort(id.begin() + mid + 1, id.begin() + r + 1, cmp(1));
  for (int i = l, j = mid + 1; i <= mid || j <= r; ) {
    if (j > r || i <= mid && dis[id[i]][1] <= dis[id[j]][1]) {
      add(askd(dis[id[i++]][2]), 1);
    }else {
      ans[id[j]] += ask(askd(dis[id[j]][2])), ++j;
    }
  }
  for (int i = l; i <= mid; ++i) {
    add(askd(dis[id[i]][2]), -1);
  }
}

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w, G[u].pb(v, w), G[v].pb(u, w);
  }
  for (auto i : {0, 1, 2}) {
    dij(i + 1, i);
  }
  for (int i = 1; i <= n; ++i) {
    disc.pb(dis[i][2]);
  }
  sort(disc.begin(), disc.end());
  disc.erase(unique(disc.begin(), disc.end()), disc.end());
  {
    vector<int> t(n);
    iota(t.begin(), t.end(), 1);
    sort(t.begin(), t.end(), cmp(0));
    for (int i = 0, r = 0; i < sz(t); i = ++r) {
      fa[t[i]] = t[i], id.pb(t[i]);
      while (r + 1 < sz(t) && dis[t[r + 1]] == dis[t[i]]) {
        fa[t[++r]] = t[i];
      }
    }
  }
  cdq(0, sz(id) - 1);
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    res += !ans[fa[i]];
  }
  cout << res << '\n';
  return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 3664kb

input:

250 300
224 221 81
7 224 11
19 221 40
186 7 96
149 19 86
143 149 75
111 221 47
4 221 84
87 224 28
24...

output:

33

result:

ok single line: '33'

Test #2:

score: 5
Accepted
time: 0ms
memory: 3664kb

input:

250 300
15 55 47
44 15 29
36 44 39
147 15 76
114 55 20
47 15 99
5 36 44
185 55 37
224 5 67
113 147 4...

output:

27

result:

ok single line: '27'

Test #3:

score: 5
Accepted
time: 0ms
memory: 3660kb

input:

250 300
145 60 26
26 145 19
131 26 62
73 131 66
228 26 4
250 60 32
30 73 32
210 26 33
161 131 68
70 ...

output:

33

result:

ok single line: '33'

Test #4:

score: 5
Accepted
time: 0ms
memory: 3660kb

input:

250 300
212 227 26
70 227 83
122 70 31
245 122 1
162 122 18
89 212 45
233 89 78
159 162 49
87 70 89
...

output:

37

result:

ok single line: '37'

Test #5:

score: 5
Accepted
time: 3ms
memory: 3788kb

input:

1800 2000
481 73 36
1145 481 90
899 481 12
855 899 71
117 1145 87
17 855 51
1223 17 13
1708 1145 55
...

output:

43

result:

ok single line: '43'

Test #6:

score: 5
Accepted
time: 6ms
memory: 3788kb

input:

1800 2000
1533 1556 16
1551 1556 96
1076 1556 37
31 1551 83
40 1076 18
1648 1551 97
1760 1533 89
141...

output:

58

result:

ok single line: '58'

Test #7:

score: 5
Accepted
time: 3ms
memory: 3784kb

input:

1800 2000
554 498 60
442 554 18
54 498 9
1798 54 33
422 1798 14
1696 54 41
201 1798 40
297 1798 63
8...

output:

49

result:

ok single line: '49'

Test #8:

score: 5
Accepted
time: 327ms
memory: 10268kb

input:

100000 99999
5092 35397 69
59669 5092 13
66060 5092 45
45290 66060 57
86709 5092 79
37616 86709 66
7...

output:

5616

result:

ok single line: '5616'

Test #9:

score: 5
Accepted
time: 301ms
memory: 10356kb

input:

100000 99999
76853 94 59
27726 94 13
90492 76853 33
97933 90492 71
46476 97933 18
6568 90492 37
3242...

output:

3202

result:

ok single line: '3202'

Test #10:

score: 5
Accepted
time: 296ms
memory: 10324kb

input:

100000 99999
20874 85984 48
8458 20874 88
50743 8458 100
41101 8458 84
8267 20874 86
30201 8458 55
3...

output:

4659

result:

ok single line: '4659'

Test #11:

score: 5
Accepted
time: 316ms
memory: 13252kb

input:

100000 200000
53018 80768 61
5832 80768 43
2997 80768 91
66393 80768 56
81366 5832 59
85664 5832 16
...

output:

40

result:

ok single line: '40'

Test #12:

score: 5
Accepted
time: 448ms
memory: 13780kb

input:

100000 200000
28908 36036 66
91872 36036 84
1622 28908 74
60237 1622 1
17200 28908 44
89734 28908 82...

output:

39

result:

ok single line: '39'

Test #13:

score: 5
Accepted
time: 459ms
memory: 13272kb

input:

100000 200000
80169 49117 81
59533 80169 11
38687 80169 66
73361 49117 69
86848 80169 39
97609 73361...

output:

37

result:

ok single line: '37'

Test #14:

score: 5
Accepted
time: 387ms
memory: 13796kb

input:

100000 200000
8518 10111 89
80152 8518 82
28398 8518 42
64281 8518 89
79057 10111 31
67454 64281 64
...

output:

52

result:

ok single line: '52'

Test #15:

score: 5
Accepted
time: 449ms
memory: 13796kb

input:

100000 200000
63008 51968 67
14619 63008 33
38273 63008 86
30436 51968 98
95240 30436 21
44387 51968...

output:

118

result:

ok single line: '118'

Test #16:

score: 5
Accepted
time: 473ms
memory: 13792kb

input:

100000 200000
38428 6838 6
57875 38428 62
23341 6838 42
77486 57875 67
36886 57875 99
35263 6838 58
...

output:

106

result:

ok single line: '106'

Test #17:

score: 5
Accepted
time: 459ms
memory: 13804kb

input:

100000 200000
29745 3984 50
23260 3984 62
53496 23260 79
81207 53496 13
79920 81207 44
90071 53496 1...

output:

99

result:

ok single line: '99'

Test #18:

score: 5
Accepted
time: 451ms
memory: 13788kb

input:

100000 200000
46691 643 96
325 46691 74
74789 325 7
65177 46691 54
98613 46691 46
69449 643 77
60913...

output:

62

result:

ok single line: '62'

Test #19:

score: 5
Accepted
time: 468ms
memory: 13792kb

input:

100000 200000
33629 97358 73
44862 33629 12
25151 33629 46
87268 25151 34
81508 25151 83
45636 81508...

output:

160

result:

ok single line: '160'

Test #20:

score: 5
Accepted
time: 462ms
memory: 13800kb

input:

100000 200000
68252 47666 98
62764 68252 87
84831 68252 91
49616 68252 73
58627 68252 71
33313 68252...

output:

166

result:

ok single line: '166'

Extra Test:

score: 0
Extra Test Passed