Submission #257820

# Submission time Handle Problem Language Result Execution time Memory
257820 2020-08-04T20:55:23 Z Bruteforceman Making Friends on Joitter is Fun (JOI20_joitter2) C++11
17 / 100
5000 ms 35204 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];

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);
}
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);
    }
    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];
  }
}
bool exists(int i, int j) {
  i = root(i); j = root(j);
  return out[i].count(j);
}
int sz(int i) {
  return cont[i].size();
}
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));
      cont[root(q)].insert(p);
      outgoing[root(p)].insert(make_pair(p, 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 = 0;
    for(int i = 1; i <= n; i++) {
      if(root(i) == i) {
        ans += 1LL * sub[i] * (sub[i] - 1);
        ans += 1LL * sub[i] * sz(i);
      }
    }
    printf("%lld\n", ans);
  }
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:61: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:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &p, &q); 
     ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23936 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 14 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 15 ms 23880 KB Output is correct
13 Correct 14 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 15 ms 23872 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 14 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 19 ms 23784 KB Output is correct
23 Correct 14 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 15 ms 23808 KB Output is correct
28 Correct 17 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 19 ms 23808 KB Output is correct
31 Correct 17 ms 23840 KB Output is correct
32 Correct 17 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23936 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 14 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 15 ms 23880 KB Output is correct
13 Correct 14 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 15 ms 23872 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 14 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 19 ms 23784 KB Output is correct
23 Correct 14 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 15 ms 23808 KB Output is correct
28 Correct 17 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 19 ms 23808 KB Output is correct
31 Correct 17 ms 23840 KB Output is correct
32 Correct 17 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
34 Correct 20 ms 23936 KB Output is correct
35 Correct 997 ms 28596 KB Output is correct
36 Correct 1835 ms 34780 KB Output is correct
37 Correct 1918 ms 35040 KB Output is correct
38 Correct 1809 ms 34552 KB Output is correct
39 Correct 49 ms 24316 KB Output is correct
40 Correct 44 ms 24592 KB Output is correct
41 Correct 48 ms 24572 KB Output is correct
42 Correct 49 ms 24360 KB Output is correct
43 Correct 44 ms 24320 KB Output is correct
44 Correct 48 ms 24440 KB Output is correct
45 Correct 47 ms 24420 KB Output is correct
46 Correct 54 ms 24312 KB Output is correct
47 Correct 51 ms 24556 KB Output is correct
48 Correct 44 ms 24568 KB Output is correct
49 Correct 90 ms 25848 KB Output is correct
50 Correct 1853 ms 35204 KB Output is correct
51 Correct 95 ms 24824 KB Output is correct
52 Correct 1844 ms 32360 KB Output is correct
53 Correct 87 ms 25592 KB Output is correct
54 Correct 1823 ms 33732 KB Output is correct
55 Correct 42 ms 24952 KB Output is correct
56 Correct 46 ms 24960 KB Output is correct
57 Correct 38 ms 24952 KB Output is correct
58 Correct 37 ms 24960 KB Output is correct
59 Correct 48 ms 24440 KB Output is correct
60 Correct 1813 ms 29560 KB Output is correct
61 Correct 52 ms 24696 KB Output is correct
62 Correct 1804 ms 34332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23936 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 14 ms 23808 KB Output is correct
11 Correct 14 ms 23808 KB Output is correct
12 Correct 15 ms 23880 KB Output is correct
13 Correct 14 ms 23808 KB Output is correct
14 Correct 16 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 15 ms 23872 KB Output is correct
18 Correct 16 ms 23808 KB Output is correct
19 Correct 16 ms 23808 KB Output is correct
20 Correct 14 ms 23808 KB Output is correct
21 Correct 15 ms 23808 KB Output is correct
22 Correct 19 ms 23784 KB Output is correct
23 Correct 14 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 14 ms 23808 KB Output is correct
27 Correct 15 ms 23808 KB Output is correct
28 Correct 17 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 19 ms 23808 KB Output is correct
31 Correct 17 ms 23840 KB Output is correct
32 Correct 17 ms 23808 KB Output is correct
33 Correct 15 ms 23808 KB Output is correct
34 Correct 20 ms 23936 KB Output is correct
35 Correct 997 ms 28596 KB Output is correct
36 Correct 1835 ms 34780 KB Output is correct
37 Correct 1918 ms 35040 KB Output is correct
38 Correct 1809 ms 34552 KB Output is correct
39 Correct 49 ms 24316 KB Output is correct
40 Correct 44 ms 24592 KB Output is correct
41 Correct 48 ms 24572 KB Output is correct
42 Correct 49 ms 24360 KB Output is correct
43 Correct 44 ms 24320 KB Output is correct
44 Correct 48 ms 24440 KB Output is correct
45 Correct 47 ms 24420 KB Output is correct
46 Correct 54 ms 24312 KB Output is correct
47 Correct 51 ms 24556 KB Output is correct
48 Correct 44 ms 24568 KB Output is correct
49 Correct 90 ms 25848 KB Output is correct
50 Correct 1853 ms 35204 KB Output is correct
51 Correct 95 ms 24824 KB Output is correct
52 Correct 1844 ms 32360 KB Output is correct
53 Correct 87 ms 25592 KB Output is correct
54 Correct 1823 ms 33732 KB Output is correct
55 Correct 42 ms 24952 KB Output is correct
56 Correct 46 ms 24960 KB Output is correct
57 Correct 38 ms 24952 KB Output is correct
58 Correct 37 ms 24960 KB Output is correct
59 Correct 48 ms 24440 KB Output is correct
60 Correct 1813 ms 29560 KB Output is correct
61 Correct 52 ms 24696 KB Output is correct
62 Correct 1804 ms 34332 KB Output is correct
63 Execution timed out 5060 ms 30712 KB Time limit exceeded
64 Halted 0 ms 0 KB -