답안 #233410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
233410 2020-05-20T12:41:08 Z anonymous 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
1273 ms 85104 KB
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#define LL long long
#define MAXN 100005
using namespace std;
int N, M, ai, bi;
LL ans;
vector <pair<int,int> > S;
class DSU {
public:
    int Set[MAXN][2];
    LL Num[MAXN]; //contribution by each clique
    set<int> To[MAXN], From[MAXN]; //cliques that i follows, cliques that follows i
    set<int> Nodes[MAXN], InClique[MAXN]; //nodes coming into clique, nodes in clique

    void Init() {
        for (int i=1; i<=N; i++) {
            InClique[i].insert(i);
            Set[i][0] = i;
            Set[i][1] = 1;
        }
    }

    int Find(int x) {
        if (Set[x][0] == x) {return(x);}
        return(Set[x][0] = Find(Set[x][0]));
    }

    void eval(int p) {
        ans -= Num[p];
        Num[p] = ((LL) Set[p][1]) * ((LL) Set[p][1] - 1) + ((LL) Nodes[p].size()) * ((LL) Set[p][1]);
        ans += Num[p];
    }

    void Union(int x, int y) { //clique ids
        x = Find(x), y = Find(y);
        if (x == y) {return;}
        if (Set[x][1] > Set[y][1]) {swap(x,y);}
        //printf("Union %d into %d\n",x,y);
        ans -= Num[x];
        for (int v: To[x]) {
            From[v].erase(x);
            if (v == y) {continue;}
            From[v].insert(y);
            To[y].insert(v);
            if (From[y].find(v) != From[y].end()) {
                S.push_back({y,v});
            }
        }
        for (int v: From[x]) {
            To[v].erase(x);
            if (v == y) {continue;}
            To[v].insert(y);
            From[y].insert(v);
            if (To[y].find(v) != To[y].end()) {
                S.push_back({y,v});
            }
        }
        for (int v: Nodes[x]) {
            if (InClique[y].find(v) != InClique[y].end()) {continue;}
            Nodes[y].insert(v);
        }
        for (int v: InClique[x]) {
            if (Nodes[y].find(v) != Nodes[y].end()) {
                Nodes[y].erase(v);
            }
            InClique[y].insert(v);
        }
        Set[y][1] = InClique[y].size();
        Set[x][0] = y;
        eval(y);
       // printf("DEBUG %d %d\n", Set[y][1], Nodes[y].size());
    }

    void addEdge(int x, int y) {
        int p1 = Find(x), p2 = Find(y);
        //printf("Add edge %d %d %d %d\n",x,y,p1,p2);
        if (Nodes[p2].find(x) != Nodes[p2].end() || p1 == p2) {
            return; //edge already exists or in same clique
        } else {
            To[p1].insert(p2);
            From[p2].insert(p1);
            Nodes[p2].insert(x);
            if (To[p2].find(p1) != To[p2].end()) {
                S.push_back({p1,p2});
            }
            eval(p1), eval(p2);
        }
    }
} DSU;

int main() {
    //freopen("joitterin.txt","r",stdin);
    scanf("%d %d",&N,&M);
    DSU.Init();
    for (int i=1; i<=M; i++) {
        scanf("%d %d",&ai,&bi);
        DSU.addEdge(ai, bi);
        while (S.size()) {
            int x = S.back().first, y = S.back().second;
            S.pop_back();
            DSU.Union(x,y);
        }
        printf("%lld\n", ans);
    }
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&N,&M);
     ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&ai,&bi);
         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 16 ms 19072 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 15 ms 19072 KB Output is correct
6 Correct 17 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 16 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19072 KB Output is correct
13 Correct 15 ms 19072 KB Output is correct
14 Correct 16 ms 19072 KB Output is correct
15 Correct 15 ms 19072 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 15 ms 19200 KB Output is correct
18 Correct 15 ms 19072 KB Output is correct
19 Correct 16 ms 19072 KB Output is correct
20 Correct 16 ms 19200 KB Output is correct
21 Correct 16 ms 19200 KB Output is correct
22 Correct 17 ms 19072 KB Output is correct
23 Correct 16 ms 19200 KB Output is correct
24 Correct 16 ms 19200 KB Output is correct
25 Correct 17 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 16 ms 19176 KB Output is correct
28 Correct 17 ms 19072 KB Output is correct
29 Correct 15 ms 19072 KB Output is correct
30 Correct 15 ms 19072 KB Output is correct
31 Correct 16 ms 19200 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 17 ms 19200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 16 ms 19072 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 15 ms 19072 KB Output is correct
6 Correct 17 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 16 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19072 KB Output is correct
13 Correct 15 ms 19072 KB Output is correct
14 Correct 16 ms 19072 KB Output is correct
15 Correct 15 ms 19072 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 15 ms 19200 KB Output is correct
18 Correct 15 ms 19072 KB Output is correct
19 Correct 16 ms 19072 KB Output is correct
20 Correct 16 ms 19200 KB Output is correct
21 Correct 16 ms 19200 KB Output is correct
22 Correct 17 ms 19072 KB Output is correct
23 Correct 16 ms 19200 KB Output is correct
24 Correct 16 ms 19200 KB Output is correct
25 Correct 17 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 16 ms 19176 KB Output is correct
28 Correct 17 ms 19072 KB Output is correct
29 Correct 15 ms 19072 KB Output is correct
30 Correct 15 ms 19072 KB Output is correct
31 Correct 16 ms 19200 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 17 ms 19200 KB Output is correct
34 Correct 20 ms 19328 KB Output is correct
35 Correct 137 ms 25100 KB Output is correct
36 Correct 167 ms 28020 KB Output is correct
37 Correct 190 ms 28040 KB Output is correct
38 Correct 160 ms 27640 KB Output is correct
39 Correct 20 ms 19712 KB Output is correct
40 Correct 21 ms 20096 KB Output is correct
41 Correct 21 ms 20096 KB Output is correct
42 Correct 22 ms 19712 KB Output is correct
43 Correct 23 ms 19712 KB Output is correct
44 Correct 23 ms 19712 KB Output is correct
45 Correct 20 ms 19712 KB Output is correct
46 Correct 20 ms 19712 KB Output is correct
47 Correct 24 ms 19968 KB Output is correct
48 Correct 22 ms 19968 KB Output is correct
49 Correct 30 ms 20608 KB Output is correct
50 Correct 176 ms 28168 KB Output is correct
51 Correct 27 ms 19968 KB Output is correct
52 Correct 151 ms 26396 KB Output is correct
53 Correct 30 ms 20480 KB Output is correct
54 Correct 165 ms 27256 KB Output is correct
55 Correct 25 ms 19968 KB Output is correct
56 Correct 32 ms 19968 KB Output is correct
57 Correct 22 ms 19968 KB Output is correct
58 Correct 22 ms 19968 KB Output is correct
59 Correct 21 ms 20096 KB Output is correct
60 Correct 130 ms 25080 KB Output is correct
61 Correct 23 ms 19968 KB Output is correct
62 Correct 158 ms 27516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19200 KB Output is correct
2 Correct 16 ms 19072 KB Output is correct
3 Correct 16 ms 19328 KB Output is correct
4 Correct 16 ms 19072 KB Output is correct
5 Correct 15 ms 19072 KB Output is correct
6 Correct 17 ms 19072 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 17 ms 19200 KB Output is correct
9 Correct 16 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 15 ms 19072 KB Output is correct
12 Correct 15 ms 19072 KB Output is correct
13 Correct 15 ms 19072 KB Output is correct
14 Correct 16 ms 19072 KB Output is correct
15 Correct 15 ms 19072 KB Output is correct
16 Correct 16 ms 19200 KB Output is correct
17 Correct 15 ms 19200 KB Output is correct
18 Correct 15 ms 19072 KB Output is correct
19 Correct 16 ms 19072 KB Output is correct
20 Correct 16 ms 19200 KB Output is correct
21 Correct 16 ms 19200 KB Output is correct
22 Correct 17 ms 19072 KB Output is correct
23 Correct 16 ms 19200 KB Output is correct
24 Correct 16 ms 19200 KB Output is correct
25 Correct 17 ms 19200 KB Output is correct
26 Correct 15 ms 19072 KB Output is correct
27 Correct 16 ms 19176 KB Output is correct
28 Correct 17 ms 19072 KB Output is correct
29 Correct 15 ms 19072 KB Output is correct
30 Correct 15 ms 19072 KB Output is correct
31 Correct 16 ms 19200 KB Output is correct
32 Correct 16 ms 19072 KB Output is correct
33 Correct 17 ms 19200 KB Output is correct
34 Correct 20 ms 19328 KB Output is correct
35 Correct 137 ms 25100 KB Output is correct
36 Correct 167 ms 28020 KB Output is correct
37 Correct 190 ms 28040 KB Output is correct
38 Correct 160 ms 27640 KB Output is correct
39 Correct 20 ms 19712 KB Output is correct
40 Correct 21 ms 20096 KB Output is correct
41 Correct 21 ms 20096 KB Output is correct
42 Correct 22 ms 19712 KB Output is correct
43 Correct 23 ms 19712 KB Output is correct
44 Correct 23 ms 19712 KB Output is correct
45 Correct 20 ms 19712 KB Output is correct
46 Correct 20 ms 19712 KB Output is correct
47 Correct 24 ms 19968 KB Output is correct
48 Correct 22 ms 19968 KB Output is correct
49 Correct 30 ms 20608 KB Output is correct
50 Correct 176 ms 28168 KB Output is correct
51 Correct 27 ms 19968 KB Output is correct
52 Correct 151 ms 26396 KB Output is correct
53 Correct 30 ms 20480 KB Output is correct
54 Correct 165 ms 27256 KB Output is correct
55 Correct 25 ms 19968 KB Output is correct
56 Correct 32 ms 19968 KB Output is correct
57 Correct 22 ms 19968 KB Output is correct
58 Correct 22 ms 19968 KB Output is correct
59 Correct 21 ms 20096 KB Output is correct
60 Correct 130 ms 25080 KB Output is correct
61 Correct 23 ms 19968 KB Output is correct
62 Correct 158 ms 27516 KB Output is correct
63 Correct 583 ms 73084 KB Output is correct
64 Correct 584 ms 73080 KB Output is correct
65 Correct 582 ms 73080 KB Output is correct
66 Correct 363 ms 48632 KB Output is correct
67 Correct 585 ms 76280 KB Output is correct
68 Correct 346 ms 48632 KB Output is correct
69 Correct 686 ms 48632 KB Output is correct
70 Correct 381 ms 48632 KB Output is correct
71 Correct 377 ms 48504 KB Output is correct
72 Correct 649 ms 61156 KB Output is correct
73 Correct 659 ms 62712 KB Output is correct
74 Correct 1273 ms 85104 KB Output is correct
75 Correct 954 ms 54896 KB Output is correct
76 Correct 1127 ms 68440 KB Output is correct
77 Correct 1099 ms 68484 KB Output is correct
78 Correct 339 ms 50040 KB Output is correct
79 Correct 595 ms 57824 KB Output is correct
80 Correct 395 ms 51064 KB Output is correct
81 Correct 736 ms 57424 KB Output is correct
82 Correct 865 ms 62584 KB Output is correct
83 Correct 864 ms 62332 KB Output is correct
84 Correct 787 ms 62180 KB Output is correct
85 Correct 786 ms 62200 KB Output is correct
86 Correct 445 ms 81656 KB Output is correct
87 Correct 505 ms 83576 KB Output is correct
88 Correct 687 ms 62048 KB Output is correct
89 Correct 1027 ms 66936 KB Output is correct