답안 #257823

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257823 2020-08-04T21:06:19 Z Bruteforceman 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++11
100 / 100
1876 ms 106248 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
set <int> in[maxn], out[maxn];
set <pair <int, int>> incoming[maxn], outgoing[maxn];
set <int> cont[maxn];
long long edgeCnt;

queue <pair <int, int>> Q;
int par[maxn];
int sub[maxn];
int root(int x) {
  if(par[x] == x) return x;
  return par[x] = root(par[x]);
}
bool change(set <int> &t, set <int> &r, int i, int j) {
  t.erase(i); t.insert(j);
  return r.count(j);
}
long long totalCont(int i) {
  return 1LL * cont[i].size() * sub[i] + 1LL * sub[i] * (sub[i] - 1);
}
void join(int x, int y) {
  x = root(x);
  y = root(y);
  if(incoming[x].size() + outgoing[x].size() > incoming[y].size() + outgoing[y].size()) swap(x, y); 
  if(x != y) {
    for(int i : in[x]) {
      in[y].insert(i);
      if(change(out[i], in[i], x, y)) Q.emplace(i, y);
    }
    for(int i : out[x]) {
      out[y].insert(i);
      if(change(in[i], out[i], x, y)) Q.emplace(i, y);
    }
    edgeCnt -= totalCont(x) + totalCont(y);
    for(auto i : incoming[x]) {
      if(root(i.second) == y) {
        outgoing[y].erase(make_pair(i.second, i.first));
        continue;
      }
      incoming[y].insert(i);
      cont[y].insert(i.second);
    }
    for(auto i : outgoing[x]) {
      if(root(i.second) == y) {
        incoming[y].erase(make_pair(i.second, i.first));
        cont[y].erase(i.first);
        continue;
      }
      outgoing[y].insert(i);
    }
    par[x] = y;
    sub[y] += sub[x];
    edgeCnt += totalCont(y);
  }
}
bool exists(int i, int j) {
  i = root(i); j = root(j);
  return out[i].count(j);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i = 1; i <= n; i++) {
    par[i] = i;
    sub[i] = 1;
  }
  vector <pair <int, int>> edge;
  for(int x = 1; x <= m; x++) {
    int p, q;
    scanf("%d %d", &p, &q); 
    if(root(p) != root(q)) {
      incoming[root(q)].insert(make_pair(q, p));
      outgoing[root(p)].insert(make_pair(p, q));
      edgeCnt -= totalCont(root(q));
      cont[root(q)].insert(p);
      edgeCnt += totalCont(root(q));
    }
    p = root(p);
    q = root(q);
    out[p].insert(q);
    in[q].insert(p);
    if(exists(p, q) && exists(q, p)) Q.emplace(root(p), root(q));
    while(!Q.empty()) {
      int p = Q.front().first;
      int q = Q.front().second;
      Q.pop();
      join(p, q);
    }
    long long ans = edgeCnt;
    printf("%lld\n", ans);
  }
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &p, &q); 
     ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 14 ms 23808 KB Output is correct
13 Correct 13 ms 23808 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 14 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23936 KB Output is correct
18 Correct 13 ms 23808 KB Output is correct
19 Correct 14 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 14 ms 23808 KB Output is correct
23 Correct 15 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 21 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 14 ms 23808 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 14 ms 23808 KB Output is correct
30 Correct 17 ms 23812 KB Output is correct
31 Correct 15 ms 23808 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 14 ms 23808 KB Output is correct
13 Correct 13 ms 23808 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 14 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23936 KB Output is correct
18 Correct 13 ms 23808 KB Output is correct
19 Correct 14 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 14 ms 23808 KB Output is correct
23 Correct 15 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 21 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 14 ms 23808 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 14 ms 23808 KB Output is correct
30 Correct 17 ms 23812 KB Output is correct
31 Correct 15 ms 23808 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
34 Correct 24 ms 23936 KB Output is correct
35 Correct 164 ms 28280 KB Output is correct
36 Correct 222 ms 32376 KB Output is correct
37 Correct 225 ms 32504 KB Output is correct
38 Correct 205 ms 31864 KB Output is correct
39 Correct 19 ms 24320 KB Output is correct
40 Correct 20 ms 24576 KB Output is correct
41 Correct 20 ms 24448 KB Output is correct
42 Correct 18 ms 24320 KB Output is correct
43 Correct 24 ms 24320 KB Output is correct
44 Correct 23 ms 24320 KB Output is correct
45 Correct 21 ms 24320 KB Output is correct
46 Correct 18 ms 24320 KB Output is correct
47 Correct 21 ms 24576 KB Output is correct
48 Correct 21 ms 24448 KB Output is correct
49 Correct 33 ms 25728 KB Output is correct
50 Correct 209 ms 32632 KB Output is correct
51 Correct 27 ms 24704 KB Output is correct
52 Correct 175 ms 29676 KB Output is correct
53 Correct 35 ms 25592 KB Output is correct
54 Correct 196 ms 31224 KB Output is correct
55 Correct 26 ms 24960 KB Output is correct
56 Correct 23 ms 24960 KB Output is correct
57 Correct 26 ms 24952 KB Output is correct
58 Correct 26 ms 24832 KB Output is correct
59 Correct 19 ms 24320 KB Output is correct
60 Correct 152 ms 26744 KB Output is correct
61 Correct 23 ms 24704 KB Output is correct
62 Correct 209 ms 31480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
3 Correct 13 ms 23808 KB Output is correct
4 Correct 14 ms 23808 KB Output is correct
5 Correct 14 ms 23808 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 14 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 14 ms 23808 KB Output is correct
13 Correct 13 ms 23808 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 14 ms 23808 KB Output is correct
16 Correct 16 ms 23808 KB Output is correct
17 Correct 16 ms 23936 KB Output is correct
18 Correct 13 ms 23808 KB Output is correct
19 Correct 14 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 14 ms 23808 KB Output is correct
23 Correct 15 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 21 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 14 ms 23808 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 14 ms 23808 KB Output is correct
30 Correct 17 ms 23812 KB Output is correct
31 Correct 15 ms 23808 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
34 Correct 24 ms 23936 KB Output is correct
35 Correct 164 ms 28280 KB Output is correct
36 Correct 222 ms 32376 KB Output is correct
37 Correct 225 ms 32504 KB Output is correct
38 Correct 205 ms 31864 KB Output is correct
39 Correct 19 ms 24320 KB Output is correct
40 Correct 20 ms 24576 KB Output is correct
41 Correct 20 ms 24448 KB Output is correct
42 Correct 18 ms 24320 KB Output is correct
43 Correct 24 ms 24320 KB Output is correct
44 Correct 23 ms 24320 KB Output is correct
45 Correct 21 ms 24320 KB Output is correct
46 Correct 18 ms 24320 KB Output is correct
47 Correct 21 ms 24576 KB Output is correct
48 Correct 21 ms 24448 KB Output is correct
49 Correct 33 ms 25728 KB Output is correct
50 Correct 209 ms 32632 KB Output is correct
51 Correct 27 ms 24704 KB Output is correct
52 Correct 175 ms 29676 KB Output is correct
53 Correct 35 ms 25592 KB Output is correct
54 Correct 196 ms 31224 KB Output is correct
55 Correct 26 ms 24960 KB Output is correct
56 Correct 23 ms 24960 KB Output is correct
57 Correct 26 ms 24952 KB Output is correct
58 Correct 26 ms 24832 KB Output is correct
59 Correct 19 ms 24320 KB Output is correct
60 Correct 152 ms 26744 KB Output is correct
61 Correct 23 ms 24704 KB Output is correct
62 Correct 209 ms 31480 KB Output is correct
63 Correct 906 ms 100216 KB Output is correct
64 Correct 905 ms 100680 KB Output is correct
65 Correct 995 ms 100728 KB Output is correct
66 Correct 360 ms 52472 KB Output is correct
67 Correct 603 ms 61688 KB Output is correct
68 Correct 341 ms 52604 KB Output is correct
69 Correct 936 ms 52472 KB Output is correct
70 Correct 363 ms 52472 KB Output is correct
71 Correct 311 ms 52632 KB Output is correct
72 Correct 665 ms 60024 KB Output is correct
73 Correct 741 ms 60280 KB Output is correct
74 Correct 1876 ms 106248 KB Output is correct
75 Correct 1251 ms 63096 KB Output is correct
76 Correct 1393 ms 80888 KB Output is correct
77 Correct 1342 ms 81400 KB Output is correct
78 Correct 467 ms 55288 KB Output is correct
79 Correct 848 ms 64480 KB Output is correct
80 Correct 449 ms 54008 KB Output is correct
81 Correct 703 ms 61816 KB Output is correct
82 Correct 1308 ms 81920 KB Output is correct
83 Correct 1170 ms 81840 KB Output is correct
84 Correct 1053 ms 79440 KB Output is correct
85 Correct 940 ms 79864 KB Output is correct
86 Correct 361 ms 54132 KB Output is correct
87 Correct 431 ms 56440 KB Output is correct
88 Correct 725 ms 60664 KB Output is correct
89 Correct 1269 ms 78472 KB Output is correct