#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 1e9 + 7;
//Making Friends on Joitter is Fun: JOI 2020/2
//Problem: https://oj.uz/problem/view/JOI20_joitter2
int n, m;
//set<int> conTo[100005]; //ConTo[a] contains things that connect to group a
set<int> conU[100005]; //conU[a] contains a cup conTo[a]
set<int> tGraph[100005]; // graph of arrows from pA to pB
set<int> bGraph[100005];
int cSize[100005];
int up[100005];
int sumT[100005]; //sumT[a] = sum of cSize[x] for x in conTo[a]
int curAns;
queue<pii> merg;
void goIn(int a, int b) {
tGraph[a].insert(b);
bGraph[b].insert(a);
if (tGraph[b].find(a) != tGraph[b].end()) merg.push({ a,b });
}
int par(int b) {
return (b == up[b] ? b : (up[b] = par(up[b])));
}
void con(int a, int b) {//connects a to b
if (a == b) return;
curAns += conU[a].size() * cSize[b];
curAns += conU[b].size() * cSize[a];
if (conU[a].size() < conU[b].size()) swap(a, b);
for (auto itr = conU[b].begin(); itr != conU[b].end(); ++itr) {
if (conU[a].find(*itr) != conU[a].end()) curAns -= (cSize[a] + cSize[b]);
}
for (auto itr = conU[b].begin(); itr != conU[b].end(); ++itr) {
conU[a].insert(*itr);
}
up[b] = a;
cSize[a] += cSize[b];
tGraph[a].erase(b);
tGraph[b].erase(a);
bGraph[a].erase(b);
bGraph[b].erase(a);
for (int i : tGraph[b]) {
bGraph[i].erase(b);
goIn(a, i);
}
for (int i : bGraph[b]) {
tGraph[i].erase(b);
goIn(i, a);
}
}
void init() {
for (int i = 1; i <= n; ++i) {
cSize[i] = 1;
up[i] = i;
conU[i].insert(i);
}
curAns = 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
b = par(b);
if (par(a) != b && conU[b].find(a) == conU[b].end()) {
conU[b].insert(a);
curAns += cSize[b];
a = par(a);
goIn(a, b);
while (!merg.empty()) {
pii p = merg.front();
merg.pop();
con(par(p.first), par(p.second));
}
}
cout << curAns << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14316 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
11 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14344 KB |
Output is correct |
12 |
Correct |
7 ms |
14332 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14320 KB |
Output is correct |
16 |
Correct |
7 ms |
14348 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14380 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14412 KB |
Output is correct |
24 |
Correct |
9 ms |
14408 KB |
Output is correct |
25 |
Correct |
10 ms |
14360 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
7 ms |
14356 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
7 ms |
14412 KB |
Output is correct |
30 |
Correct |
7 ms |
14408 KB |
Output is correct |
31 |
Correct |
9 ms |
14416 KB |
Output is correct |
32 |
Correct |
7 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14316 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
11 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14344 KB |
Output is correct |
12 |
Correct |
7 ms |
14332 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14320 KB |
Output is correct |
16 |
Correct |
7 ms |
14348 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14380 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14412 KB |
Output is correct |
24 |
Correct |
9 ms |
14408 KB |
Output is correct |
25 |
Correct |
10 ms |
14360 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
7 ms |
14356 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
7 ms |
14412 KB |
Output is correct |
30 |
Correct |
7 ms |
14408 KB |
Output is correct |
31 |
Correct |
9 ms |
14416 KB |
Output is correct |
32 |
Correct |
7 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
20 ms |
14528 KB |
Output is correct |
35 |
Correct |
425 ms |
20256 KB |
Output is correct |
36 |
Correct |
494 ms |
23028 KB |
Output is correct |
37 |
Correct |
456 ms |
23248 KB |
Output is correct |
38 |
Correct |
426 ms |
22720 KB |
Output is correct |
39 |
Correct |
14 ms |
14668 KB |
Output is correct |
40 |
Correct |
15 ms |
15052 KB |
Output is correct |
41 |
Correct |
15 ms |
15048 KB |
Output is correct |
42 |
Correct |
13 ms |
14680 KB |
Output is correct |
43 |
Correct |
15 ms |
14796 KB |
Output is correct |
44 |
Correct |
15 ms |
14808 KB |
Output is correct |
45 |
Correct |
14 ms |
14796 KB |
Output is correct |
46 |
Correct |
15 ms |
14720 KB |
Output is correct |
47 |
Correct |
19 ms |
14920 KB |
Output is correct |
48 |
Correct |
16 ms |
14924 KB |
Output is correct |
49 |
Correct |
28 ms |
15692 KB |
Output is correct |
50 |
Correct |
445 ms |
23332 KB |
Output is correct |
51 |
Correct |
25 ms |
15052 KB |
Output is correct |
52 |
Correct |
422 ms |
21424 KB |
Output is correct |
53 |
Correct |
30 ms |
15556 KB |
Output is correct |
54 |
Correct |
433 ms |
22320 KB |
Output is correct |
55 |
Correct |
17 ms |
15156 KB |
Output is correct |
56 |
Correct |
19 ms |
15176 KB |
Output is correct |
57 |
Correct |
16 ms |
15044 KB |
Output is correct |
58 |
Correct |
16 ms |
15044 KB |
Output is correct |
59 |
Correct |
18 ms |
15172 KB |
Output is correct |
60 |
Correct |
428 ms |
20072 KB |
Output is correct |
61 |
Correct |
17 ms |
14924 KB |
Output is correct |
62 |
Correct |
432 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14316 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
6 ms |
14416 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
13 ms |
14540 KB |
Output is correct |
9 |
Correct |
11 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
9 ms |
14344 KB |
Output is correct |
12 |
Correct |
7 ms |
14332 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
7 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14320 KB |
Output is correct |
16 |
Correct |
7 ms |
14348 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14380 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14412 KB |
Output is correct |
21 |
Correct |
10 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14412 KB |
Output is correct |
24 |
Correct |
9 ms |
14408 KB |
Output is correct |
25 |
Correct |
10 ms |
14360 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
7 ms |
14356 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
7 ms |
14412 KB |
Output is correct |
30 |
Correct |
7 ms |
14408 KB |
Output is correct |
31 |
Correct |
9 ms |
14416 KB |
Output is correct |
32 |
Correct |
7 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
20 ms |
14528 KB |
Output is correct |
35 |
Correct |
425 ms |
20256 KB |
Output is correct |
36 |
Correct |
494 ms |
23028 KB |
Output is correct |
37 |
Correct |
456 ms |
23248 KB |
Output is correct |
38 |
Correct |
426 ms |
22720 KB |
Output is correct |
39 |
Correct |
14 ms |
14668 KB |
Output is correct |
40 |
Correct |
15 ms |
15052 KB |
Output is correct |
41 |
Correct |
15 ms |
15048 KB |
Output is correct |
42 |
Correct |
13 ms |
14680 KB |
Output is correct |
43 |
Correct |
15 ms |
14796 KB |
Output is correct |
44 |
Correct |
15 ms |
14808 KB |
Output is correct |
45 |
Correct |
14 ms |
14796 KB |
Output is correct |
46 |
Correct |
15 ms |
14720 KB |
Output is correct |
47 |
Correct |
19 ms |
14920 KB |
Output is correct |
48 |
Correct |
16 ms |
14924 KB |
Output is correct |
49 |
Correct |
28 ms |
15692 KB |
Output is correct |
50 |
Correct |
445 ms |
23332 KB |
Output is correct |
51 |
Correct |
25 ms |
15052 KB |
Output is correct |
52 |
Correct |
422 ms |
21424 KB |
Output is correct |
53 |
Correct |
30 ms |
15556 KB |
Output is correct |
54 |
Correct |
433 ms |
22320 KB |
Output is correct |
55 |
Correct |
17 ms |
15156 KB |
Output is correct |
56 |
Correct |
19 ms |
15176 KB |
Output is correct |
57 |
Correct |
16 ms |
15044 KB |
Output is correct |
58 |
Correct |
16 ms |
15044 KB |
Output is correct |
59 |
Correct |
18 ms |
15172 KB |
Output is correct |
60 |
Correct |
428 ms |
20072 KB |
Output is correct |
61 |
Correct |
17 ms |
14924 KB |
Output is correct |
62 |
Correct |
432 ms |
22620 KB |
Output is correct |
63 |
Correct |
898 ms |
67564 KB |
Output is correct |
64 |
Correct |
885 ms |
67600 KB |
Output is correct |
65 |
Correct |
902 ms |
67672 KB |
Output is correct |
66 |
Incorrect |
487 ms |
33616 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |