답안 #1070640

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1070640 2024-08-22T16:20:49 Z TraianDanciu 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1578 ms 169420 KB
#include <stdio.h>
#include <vector>
#include <set>

#define MAXN 100000
#define MAXM 300000

struct Edge {
  int u, v;
} edges[MAXM];
int sef1[MAXN], sef2[MAXN], sz[MAXN];
std::set<int> flw[MAXN], muchii[MAXN];
std::set<std::pair<int, int>> mp;
std::vector<int> stiva;
long long rez;

static inline long long getrez(int i) {
  return 1LL * sz[i] * flw[i].size();
}

int find1(int i) {
  if(i == sef1[i]) {
    return i;
  }
  return sef1[i] = find1(sef1[i]);
}
void join1(int i, int j) {
  std::set<int>::iterator it;

  if((i = find1(i)) != (j = find1(j))) {
    if(sz[i] > sz[j]) {
      std::swap(i, j);
    }

    rez -= getrez(i) + getrez(j);
    sz[j] += sz[i];
    sef1[i] = j;

    if(flw[j].size() < flw[i].size()) {
      std::swap(flw[i], flw[j]);
    }
    it = flw[i].begin();
    while(it != flw[i].end()) {
      flw[j].insert(*it);
      it++;
    }

    rez += getrez(j);
  }
}

int find2(int i) {
  if(i == sef2[i]) {
    return i;
  }
  return sef2[i] = find2(sef2[i]);
}
void join2(int i, int j) {
  std::set<int>::iterator it;

  if((i = find2(i)) != (j = find2(j))) {
    join1(i, j);

    if(muchii[j].size() < muchii[i].size()) {
      std::swap(i, j);
    }
    it = muchii[i].begin();
    while(it != muchii[i].end()) {
      stiva.push_back(*it);
      muchii[j].insert(*it);
      it++;
    }

    sef2[i] = j;
  }
}

int main() {
  int n, m, i, u, v;

  scanf("%d%d", &n, &m);
  for(i = 0; i < n; i++) {
    sef1[i] = sef2[i] = i;
    flw[i].insert(i);
    sz[i] = 1;
  }

  rez = n;
  for(i = 0; i < m; i++) {
    scanf("%d%d", &edges[i].u, &edges[i].v);

    u = --edges[i].u;
    v = find1(--edges[i].v);
    rez -= getrez(v);
    flw[v].insert(u);
    rez += getrez(v);

    stiva.push_back(i);
    while(!stiva.empty()) {
      muchii[u = find2(edges[stiva.back()].u)].insert(stiva.back());
      muchii[v = find2(edges[stiva.back()].v)].insert(stiva.back());
      stiva.pop_back();

      mp.insert(std::make_pair(u, v));
      if(mp.count(std::make_pair(v, u))) {
        join2(u, v);
      }
    }

    printf("%lld\n", rez - n);
  }

  return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     scanf("%d%d", &edges[i].u, &edges[i].v);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11612 KB Output is correct
2 Correct 3 ms 11612 KB Output is correct
3 Correct 3 ms 11612 KB Output is correct
4 Correct 2 ms 11612 KB Output is correct
5 Correct 2 ms 11612 KB Output is correct
6 Correct 2 ms 11612 KB Output is correct
7 Correct 3 ms 11688 KB Output is correct
8 Correct 3 ms 11868 KB Output is correct
9 Correct 3 ms 11868 KB Output is correct
10 Correct 2 ms 11612 KB Output is correct
11 Correct 2 ms 11736 KB Output is correct
12 Correct 2 ms 11612 KB Output is correct
13 Correct 2 ms 11608 KB Output is correct
14 Correct 2 ms 11612 KB Output is correct
15 Correct 2 ms 11612 KB Output is correct
16 Correct 2 ms 11612 KB Output is correct
17 Correct 2 ms 11584 KB Output is correct
18 Correct 2 ms 11608 KB Output is correct
19 Correct 2 ms 11612 KB Output is correct
20 Correct 2 ms 11612 KB Output is correct
21 Correct 3 ms 11868 KB Output is correct
22 Correct 2 ms 11608 KB Output is correct
23 Correct 2 ms 11612 KB Output is correct
24 Correct 2 ms 11612 KB Output is correct
25 Correct 4 ms 11612 KB Output is correct
26 Correct 2 ms 11608 KB Output is correct
27 Correct 2 ms 11548 KB Output is correct
28 Correct 2 ms 11612 KB Output is correct
29 Correct 2 ms 11612 KB Output is correct
30 Correct 2 ms 11688 KB Output is correct
31 Correct 2 ms 11612 KB Output is correct
32 Correct 2 ms 11612 KB Output is correct
33 Correct 2 ms 11612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11612 KB Output is correct
2 Correct 3 ms 11612 KB Output is correct
3 Correct 3 ms 11612 KB Output is correct
4 Correct 2 ms 11612 KB Output is correct
5 Correct 2 ms 11612 KB Output is correct
6 Correct 2 ms 11612 KB Output is correct
7 Correct 3 ms 11688 KB Output is correct
8 Correct 3 ms 11868 KB Output is correct
9 Correct 3 ms 11868 KB Output is correct
10 Correct 2 ms 11612 KB Output is correct
11 Correct 2 ms 11736 KB Output is correct
12 Correct 2 ms 11612 KB Output is correct
13 Correct 2 ms 11608 KB Output is correct
14 Correct 2 ms 11612 KB Output is correct
15 Correct 2 ms 11612 KB Output is correct
16 Correct 2 ms 11612 KB Output is correct
17 Correct 2 ms 11584 KB Output is correct
18 Correct 2 ms 11608 KB Output is correct
19 Correct 2 ms 11612 KB Output is correct
20 Correct 2 ms 11612 KB Output is correct
21 Correct 3 ms 11868 KB Output is correct
22 Correct 2 ms 11608 KB Output is correct
23 Correct 2 ms 11612 KB Output is correct
24 Correct 2 ms 11612 KB Output is correct
25 Correct 4 ms 11612 KB Output is correct
26 Correct 2 ms 11608 KB Output is correct
27 Correct 2 ms 11548 KB Output is correct
28 Correct 2 ms 11612 KB Output is correct
29 Correct 2 ms 11612 KB Output is correct
30 Correct 2 ms 11688 KB Output is correct
31 Correct 2 ms 11612 KB Output is correct
32 Correct 2 ms 11612 KB Output is correct
33 Correct 2 ms 11612 KB Output is correct
34 Correct 6 ms 12120 KB Output is correct
35 Correct 196 ms 31408 KB Output is correct
36 Correct 238 ms 34704 KB Output is correct
37 Correct 243 ms 34900 KB Output is correct
38 Correct 246 ms 34348 KB Output is correct
39 Correct 9 ms 12380 KB Output is correct
40 Correct 10 ms 13404 KB Output is correct
41 Correct 10 ms 13404 KB Output is correct
42 Correct 5 ms 12380 KB Output is correct
43 Correct 6 ms 12380 KB Output is correct
44 Correct 6 ms 12380 KB Output is correct
45 Correct 8 ms 12380 KB Output is correct
46 Correct 6 ms 12380 KB Output is correct
47 Correct 8 ms 12892 KB Output is correct
48 Correct 9 ms 12892 KB Output is correct
49 Correct 19 ms 14124 KB Output is correct
50 Correct 239 ms 34920 KB Output is correct
51 Correct 11 ms 13144 KB Output is correct
52 Correct 217 ms 32708 KB Output is correct
53 Correct 16 ms 13916 KB Output is correct
54 Correct 236 ms 33796 KB Output is correct
55 Correct 9 ms 12892 KB Output is correct
56 Correct 9 ms 12892 KB Output is correct
57 Correct 9 ms 12892 KB Output is correct
58 Correct 8 ms 12972 KB Output is correct
59 Correct 10 ms 13676 KB Output is correct
60 Correct 189 ms 31884 KB Output is correct
61 Correct 10 ms 12888 KB Output is correct
62 Correct 231 ms 34116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11612 KB Output is correct
2 Correct 3 ms 11612 KB Output is correct
3 Correct 3 ms 11612 KB Output is correct
4 Correct 2 ms 11612 KB Output is correct
5 Correct 2 ms 11612 KB Output is correct
6 Correct 2 ms 11612 KB Output is correct
7 Correct 3 ms 11688 KB Output is correct
8 Correct 3 ms 11868 KB Output is correct
9 Correct 3 ms 11868 KB Output is correct
10 Correct 2 ms 11612 KB Output is correct
11 Correct 2 ms 11736 KB Output is correct
12 Correct 2 ms 11612 KB Output is correct
13 Correct 2 ms 11608 KB Output is correct
14 Correct 2 ms 11612 KB Output is correct
15 Correct 2 ms 11612 KB Output is correct
16 Correct 2 ms 11612 KB Output is correct
17 Correct 2 ms 11584 KB Output is correct
18 Correct 2 ms 11608 KB Output is correct
19 Correct 2 ms 11612 KB Output is correct
20 Correct 2 ms 11612 KB Output is correct
21 Correct 3 ms 11868 KB Output is correct
22 Correct 2 ms 11608 KB Output is correct
23 Correct 2 ms 11612 KB Output is correct
24 Correct 2 ms 11612 KB Output is correct
25 Correct 4 ms 11612 KB Output is correct
26 Correct 2 ms 11608 KB Output is correct
27 Correct 2 ms 11548 KB Output is correct
28 Correct 2 ms 11612 KB Output is correct
29 Correct 2 ms 11612 KB Output is correct
30 Correct 2 ms 11688 KB Output is correct
31 Correct 2 ms 11612 KB Output is correct
32 Correct 2 ms 11612 KB Output is correct
33 Correct 2 ms 11612 KB Output is correct
34 Correct 6 ms 12120 KB Output is correct
35 Correct 196 ms 31408 KB Output is correct
36 Correct 238 ms 34704 KB Output is correct
37 Correct 243 ms 34900 KB Output is correct
38 Correct 246 ms 34348 KB Output is correct
39 Correct 9 ms 12380 KB Output is correct
40 Correct 10 ms 13404 KB Output is correct
41 Correct 10 ms 13404 KB Output is correct
42 Correct 5 ms 12380 KB Output is correct
43 Correct 6 ms 12380 KB Output is correct
44 Correct 6 ms 12380 KB Output is correct
45 Correct 8 ms 12380 KB Output is correct
46 Correct 6 ms 12380 KB Output is correct
47 Correct 8 ms 12892 KB Output is correct
48 Correct 9 ms 12892 KB Output is correct
49 Correct 19 ms 14124 KB Output is correct
50 Correct 239 ms 34920 KB Output is correct
51 Correct 11 ms 13144 KB Output is correct
52 Correct 217 ms 32708 KB Output is correct
53 Correct 16 ms 13916 KB Output is correct
54 Correct 236 ms 33796 KB Output is correct
55 Correct 9 ms 12892 KB Output is correct
56 Correct 9 ms 12892 KB Output is correct
57 Correct 9 ms 12892 KB Output is correct
58 Correct 8 ms 12972 KB Output is correct
59 Correct 10 ms 13676 KB Output is correct
60 Correct 189 ms 31884 KB Output is correct
61 Correct 10 ms 12888 KB Output is correct
62 Correct 231 ms 34116 KB Output is correct
63 Correct 706 ms 76368 KB Output is correct
64 Correct 742 ms 76264 KB Output is correct
65 Correct 726 ms 76368 KB Output is correct
66 Correct 379 ms 56796 KB Output is correct
67 Correct 966 ms 134964 KB Output is correct
68 Correct 371 ms 56912 KB Output is correct
69 Correct 542 ms 56660 KB Output is correct
70 Correct 422 ms 56872 KB Output is correct
71 Correct 409 ms 56916 KB Output is correct
72 Correct 878 ms 86864 KB Output is correct
73 Correct 890 ms 91852 KB Output is correct
74 Correct 1578 ms 132048 KB Output is correct
75 Correct 805 ms 74576 KB Output is correct
76 Correct 1165 ms 100060 KB Output is correct
77 Correct 1221 ms 100432 KB Output is correct
78 Correct 287 ms 52308 KB Output is correct
79 Correct 649 ms 73612 KB Output is correct
80 Correct 303 ms 51280 KB Output is correct
81 Correct 645 ms 70740 KB Output is correct
82 Correct 902 ms 81752 KB Output is correct
83 Correct 862 ms 81696 KB Output is correct
84 Correct 787 ms 82648 KB Output is correct
85 Correct 815 ms 82584 KB Output is correct
86 Correct 846 ms 161908 KB Output is correct
87 Correct 931 ms 169420 KB Output is correct
88 Correct 949 ms 91876 KB Output is correct
89 Correct 1203 ms 97360 KB Output is correct