CSES - NOI 2024 - Results
Submission details
Task:Anime Shops
Sender:
Submission time:2024-03-06 17:35:35 +0200
Language:C++17
Status:READY
Result:16
Feedback
groupverdictscore
#10
#2ACCEPTED16
#30
Test results
testverdicttimegroup
#10.06 s1, 3details
#20.03 s1, 3details
#30.03 s1, 3details
#40.03 s1, 3details
#50.50 s3details
#60.51 s3details
#70.51 s3details
#80.52 s3details
#9ACCEPTED0.05 s2, 3details
#10ACCEPTED0.05 s2, 3details
#11ACCEPTED0.06 s2, 3details
#120.05 s3details
#130.05 s3details
#140.05 s3details
#15--3details
#16--3details

Code

#include <bits/stdc++.h>
#include <iterator>
#include <queue>
using namespace std;

#define MAXN 100001
#define INF 1e6

typedef pair<int, int> pii;
priority_queue<pii> q;
map<int, vector<vector<int>>> adj;
vector<int> dist;
vector<int> parent;
int srank[MAXN];

int find_set(int u) {
  if (parent[u] == -1)
    return u;

  return parent[u] = find_set(parent[u]);
}

void unite(int a, int b) {
  a = find_set(a);
  b = find_set(b);

  if (a != b) {
    if (srank[b] > srank[a])
      swap(a, b);
    parent[b] = a;
    if (srank[a] == srank[b])
      srank[a]++;

    if (adj.count(b)) {
      adj[a].insert(adj[a].end(), make_move_iterator(adj[b].begin()),
                    make_move_iterator(adj[b].end()));
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int n, m, k;
  cin >> n >> m >> k;

  vector<bool> shop(n + 1, false);

  int c;
  for (int i = 0; i < k; ++i) {
    cin >> c;
    shop[c] = true;
  }

  // vector<pii> edges(m);
  parent.assign(n + 1, -1);

  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;

    unite(a, b);
    auto s = find_set(a);
    if (!adj.count(s)) {
      vector<vector<int>> v(n + 1);
      adj[s] = v;
      // adj[s].assign(n + 1, {});
    }
    adj[s][a].emplace_back(b);
    adj[s][b].emplace_back(a);
    // edges[i] = {a, b};
  }

  // Group 2
  if (m == n - 1) {
    dist.assign(n + 1, INF);
    int seen = INF;
    for (int i = 1; i < n + 1; ++i) {
      dist[i] = seen + 1;
      if (shop[i]) {
        seen = 0;
      } else {
        seen++;
      }
    }
    seen = INF;
    for (int i = n; i > 0; --i) {
      dist[i] = min(seen + 1, dist[i]);
      if (shop[i]) {
        seen = 0;
      } else {
        seen++;
      }
    }

    for (int i = 1; i < n + 1; ++i) {
      cout << ((dist[i] < INF) ? dist[i] : -1) << ' ';
    }
    return 0;
  }

  for (int x = 1; x < n + 1; ++x) {
    dist.assign(n + 1, INF);
    dist[x] = 0;

    q = {};
    q.push({0, x});

    auto g = find_set(x);
    int target = -1;

    if (adj.count(g))
      while (!q.empty()) {
        int a = q.top().second;

        // Found the shortest path to an anime shop
        if (shop[a] && a != x) {
          target = a;

          // printf("shortest %d -> %d dist: %d\n", x, a, dist[a]);
          break;
        }

        q.pop();
        for (auto b : adj[g][a]) {
          if (dist[a] + 1 < dist[b]) {
            dist[b] = dist[a] + 1;
            q.push({-dist[b], b});
          }
        }
      }

    cout << (target == -1 ? target : dist[target]) << ' ';
  }
  cout << '\n';
}

Test details

Test 1

Group: 1, 3

Verdict:

input
1000 2000 1
816
1 868
976 995
377 536
...

correct output
4 3 3 4 6 4 -1 4 5 2 2 5 5 3 5...

user output
4 3 3 6 6 4 -1 6 5 8 2 7 6 5 6...
Truncated

Test 2

Group: 1, 3

Verdict:

input
1000 2000 20
578 955 222 813 494 962 753 71...

correct output
5 6 4 3 4 2 -1 3 3 3 4 3 2 3 -...

user output
5 -1 6 4 4 2 -1 3 4 3 4 -1 -1 ...
Truncated

Test 3

Group: 1, 3

Verdict:

input
1000 2000 100
945 230 119 680 975 520 490 28...

correct output
2 2 3 -1 2 -1 3 2 2 1 2 -1 3 2...

user output
2 2 4 -1 3 -1 5 -1 2 1 2 -1 3 ...
Truncated

Test 4

Group: 1, 3

Verdict:

input
1000 2000 1000
150 443 960 545 218 487 896 38...

correct output
1 -1 1 1 -1 1 1 1 -1 1 1 1 -1 ...

user output
1 -1 1 1 -1 1 1 -1 -1 1 1 -1 -...
Truncated

Test 5

Group: 3

Verdict:

input
100000 200000 1
77222
59470 61811
43092 48939
14395 19964
...

correct output
8 10 8 8 8 8 8 8 9 7 7 8 8 8 6...

user output
(empty)

Test 6

Group: 3

Verdict:

input
100000 200000 20
70440 82302 64483 96767 51485 ...

correct output
-1 8 6 5 5 7 6 7 6 6 8 5 6 4 5...

user output
(empty)

Test 7

Group: 3

Verdict:

input
100000 200000 100
68738 37820 55519 77758 46482 ...

correct output
4 5 5 5 5 4 6 -1 5 4 5 6 4 5 5...

user output
(empty)

Test 8

Group: 3

Verdict:

input
100000 200000 100000
47137 80137 73347 78145 9205 6...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 9

Group: 2, 3

Verdict: ACCEPTED

input
100000 99999 1
44158
1 2
2 3
3 4
...

correct output
44157 44156 44155 44154 44153 ...

user output
44157 44156 44155 44154 44153 ...
Truncated

Test 10

Group: 2, 3

Verdict: ACCEPTED

input
100000 99999 20
44158 25720 84658 90057 99607 ...

correct output
361 360 359 358 357 356 355 35...

user output
361 360 359 358 357 356 355 35...
Truncated

Test 11

Group: 2, 3

Verdict: ACCEPTED

input
100000 99999 100000
44158 25720 84658 90057 99607 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
Truncated

Test 12

Group: 3

Verdict:

input
100000 99999 3
99998 99999 100000
1 2
1 3
1 4
...

correct output
33333 33332 33332 33332 33331 ...

user output
99997 99996 99995 99994 99993 ...
Truncated

Test 13

Group: 3

Verdict:

input
100000 99999 300
99701 99702 99703 99704 99705 ...

correct output
333 333 333 333 333 333 333 33...

user output
99700 99699 99698 99697 99696 ...
Truncated

Test 14

Group: 3

Verdict:

input
100000 99999 30000
70001 70002 70003 70004 70005 ...

correct output
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...

user output
70000 69999 69998 69997 69996 ...
Truncated

Test 15

Group: 3

Verdict:

input
100000 0 100000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...

user output
(empty)

Test 16

Group: 3

Verdict:

input
100000 100000 2
1 50001
1 2
2 3
3 4
...

correct output
50000 1 2 3 4 5 6 7 8 9 10 11 ...

user output
(empty)