Submission #313892

# Submission time Handle Problem Language Result Execution time Memory
313892 2020-10-17T08:44:45 Z georgerapeanu Making Friends on Joitter is Fun (JOI20_joitter2) C++11
17 / 100
513 ms 63736 KB
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int NMAX = 1e5;

int n,m;

int father[NMAX + 5];
set<int> in[NMAX + 5];
set<int> out[NMAX + 5];
set<int> nodes[NMAX + 5];

int find_root(int nod){
    if(father[nod] == 0){
        return nod;
    }
    return father[nod] = find_root(father[nod]);
}

int unite(int x,int y){
    x = find_root(x);
    y = find_root(y);

    if(x == y){
        return false;
    }

    if(nodes[x].size() + in[x].size() + out[x].size() > nodes[y].size() + in[y].size() + out[y].size()){
        swap(x,y);
    }

    if((out[x].count(y) && out[y].count(x)) == 0){
        return 0;
    }

    long long delta = 0;

    delta -= 1LL * nodes[x].size() * in[x].size();
    delta -= 1LL * nodes[x].size() * nodes[x].size();
    delta -= 1LL * nodes[y].size() * in[y].size();
    delta -= 1LL * nodes[y].size() * nodes[y].size();

    father[x] = y;
    for(auto it:nodes[x]){
        nodes[y].insert(it);
        if(in[y].count(it)){
            in[y].erase(it);
        }
        if(out[y].count(it)){
            out[y].erase(it);
        }
    }

    for(auto it:in[x]){
        if(nodes[y].count(it)){
            continue;
        }
        in[y].insert(it);
        if(out[find_root(it)].count(x)){
            out[find_root(it)].erase(x);
            out[find_root(it)].insert(y);
        }
    }

    for(auto it:out[x]){
        if(nodes[y].count(it)){
            continue;
        }
        out[y].insert(it);
    }
    
    delta += 1LL * nodes[y].size() * in[y].size();
    delta += 1LL * nodes[y].size() * nodes[y].size();

    for(auto it:out[x]){
        delta += unite(x,it);
    }
    
    for(auto it:in[x]){
        delta += unite(x,it);
    }
    
    return delta;
}

int add_edge(int x,int y){
    if(find_root(x) == find_root(y)){
        return 0;
    }


    long long delta = 0;

    delta -= 1LL * nodes[find_root(y)].size() * in[find_root(y)].size();
    delta -= 1LL * nodes[find_root(y)].size() * nodes[find_root(y)].size();

    out[find_root(x)].insert(find_root(y));
    in[find_root(y)].insert(x);

    delta += 1LL * nodes[find_root(y)].size() * in[find_root(y)].size();
    delta += 1LL * nodes[find_root(y)].size() * nodes[find_root(y)].size();
    delta += unite(x,y);

    return delta;
}

void afis(){
    printf("\n");
    for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
    for(int i = 1;i <= n;i++){
        if(find_root(i) == i){
            printf("root %d\n",i);
            for(auto it:nodes[i])printf("%d ",it);printf("\n");
            for(auto it:in[i])printf("%d ",it);printf("\n");
            for(auto it:out[i])printf("%d ",it);printf("\n");
        }
    }
    printf("\n");
}

int main(){

    scanf("%d %d",&n,&m);

    for(int i = 1;i <= n;i++){
        nodes[i].insert(i);
    }

    long long ans = 0;

    for(int i = 1;i <= m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        ans += add_edge(x,y);
        printf("%lld\n",ans);
    //    afis();
    }

    return 0;
}

Compilation message

joitter2.cpp: In function 'void afis()':
joitter2.cpp:112:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  112 |     for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
      |     ^~~
joitter2.cpp:112:57: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  112 |     for(int i = 1;i <= n;i++)printf("%d ",find_root(i));printf("\n");
      |                                                         ^~~~~~
joitter2.cpp:116:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  116 |             for(auto it:nodes[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:116:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  116 |             for(auto it:nodes[i])printf("%d ",it);printf("\n");
      |                                                   ^~~~~~
joitter2.cpp:117:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  117 |             for(auto it:in[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:117:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  117 |             for(auto it:in[i])printf("%d ",it);printf("\n");
      |                                                ^~~~~~
joitter2.cpp:118:13: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  118 |             for(auto it:out[i])printf("%d ",it);printf("\n");
      |             ^~~
joitter2.cpp:118:49: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  118 |             for(auto it:out[i])printf("%d ",it);printf("\n");
      |                                                 ^~~~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:126:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  126 |     scanf("%d %d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  136 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14592 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 11 ms 14592 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14368 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 11 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14592 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 11 ms 14592 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14368 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 11 ms 14464 KB Output is correct
34 Correct 14 ms 14592 KB Output is correct
35 Correct 143 ms 19960 KB Output is correct
36 Correct 161 ms 22008 KB Output is correct
37 Correct 158 ms 22044 KB Output is correct
38 Correct 181 ms 21880 KB Output is correct
39 Correct 14 ms 14848 KB Output is correct
40 Correct 15 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 14 ms 14848 KB Output is correct
43 Correct 15 ms 14848 KB Output is correct
44 Correct 15 ms 14848 KB Output is correct
45 Correct 14 ms 14848 KB Output is correct
46 Correct 14 ms 14848 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 20 ms 14976 KB Output is correct
49 Correct 22 ms 15488 KB Output is correct
50 Correct 165 ms 22136 KB Output is correct
51 Correct 19 ms 15104 KB Output is correct
52 Correct 147 ms 20984 KB Output is correct
53 Correct 22 ms 15488 KB Output is correct
54 Correct 152 ms 21624 KB Output is correct
55 Correct 17 ms 14976 KB Output is correct
56 Correct 16 ms 15104 KB Output is correct
57 Correct 16 ms 15232 KB Output is correct
58 Correct 17 ms 15232 KB Output is correct
59 Correct 15 ms 15232 KB Output is correct
60 Correct 133 ms 20216 KB Output is correct
61 Correct 17 ms 15096 KB Output is correct
62 Correct 157 ms 21688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14592 KB Output is correct
2 Correct 10 ms 14336 KB Output is correct
3 Correct 10 ms 14336 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14336 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 10 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14336 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14336 KB Output is correct
16 Correct 10 ms 14464 KB Output is correct
17 Correct 10 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 10 ms 14464 KB Output is correct
20 Correct 10 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 11 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 11 ms 14592 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 10 ms 14368 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 10 ms 14464 KB Output is correct
33 Correct 11 ms 14464 KB Output is correct
34 Correct 14 ms 14592 KB Output is correct
35 Correct 143 ms 19960 KB Output is correct
36 Correct 161 ms 22008 KB Output is correct
37 Correct 158 ms 22044 KB Output is correct
38 Correct 181 ms 21880 KB Output is correct
39 Correct 14 ms 14848 KB Output is correct
40 Correct 15 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 14 ms 14848 KB Output is correct
43 Correct 15 ms 14848 KB Output is correct
44 Correct 15 ms 14848 KB Output is correct
45 Correct 14 ms 14848 KB Output is correct
46 Correct 14 ms 14848 KB Output is correct
47 Correct 15 ms 14976 KB Output is correct
48 Correct 20 ms 14976 KB Output is correct
49 Correct 22 ms 15488 KB Output is correct
50 Correct 165 ms 22136 KB Output is correct
51 Correct 19 ms 15104 KB Output is correct
52 Correct 147 ms 20984 KB Output is correct
53 Correct 22 ms 15488 KB Output is correct
54 Correct 152 ms 21624 KB Output is correct
55 Correct 17 ms 14976 KB Output is correct
56 Correct 16 ms 15104 KB Output is correct
57 Correct 16 ms 15232 KB Output is correct
58 Correct 17 ms 15232 KB Output is correct
59 Correct 15 ms 15232 KB Output is correct
60 Correct 133 ms 20216 KB Output is correct
61 Correct 17 ms 15096 KB Output is correct
62 Correct 157 ms 21688 KB Output is correct
63 Correct 513 ms 52856 KB Output is correct
64 Correct 510 ms 52856 KB Output is correct
65 Correct 509 ms 52728 KB Output is correct
66 Correct 327 ms 38140 KB Output is correct
67 Incorrect 512 ms 63736 KB Output isn't correct
68 Halted 0 ms 0 KB -