Submission #513907

# Submission time Handle Problem Language Result Execution time Memory
513907 2022-01-17T23:39:39 Z Eyed Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1347 ms 69956 KB
#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<ll, ll> pii;
const ll MOD = 1e9 + 7;

//Making Friends on Joitter is Fun: JOI 2020/2
//Problem: https://oj.uz/problem/view/JOI20_joitter2

ll n, m;
//set<ll> conTo[100005]; //ConTo[a] contains things that connect to group a
set<ll> conU[100005]; //conU[a] contains a cup conTo[a]
set<ll> tGraph[100005]; // graph of arrows from pA to pB
set<ll> bGraph[100005];
ll cSize[100005];
ll up[100005];
ll sumT[100005]; //sumT[a] = sum of cSize[x] for x in conTo[a] 
ll curAns;
queue<pii> merg;

void goIn(ll a, ll b) {
	tGraph[a].insert(b);
	bGraph[b].insert(a);
	if (tGraph[b].find(a) != tGraph[b].end()) merg.push({ a,b });
}

ll par(ll b) {
	return (b == up[b] ? b : (up[b] = par(up[b])));
}

void con(ll a, ll 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 (ll i : tGraph[b]) {
		bGraph[i].erase(b);
		goIn(a, i);
	}
	for (ll i : bGraph[b]) {
		tGraph[i].erase(b);
		goIn(i, a);
	}

}
void init() {
	for (ll 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 (ll i = 0; i < m; ++i) {
		ll 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;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 7 ms 14404 KB Output is correct
4 Correct 6 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 8 ms 14412 KB Output is correct
7 Correct 11 ms 14420 KB Output is correct
8 Correct 10 ms 14396 KB Output is correct
9 Correct 13 ms 14420 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 7 ms 14408 KB Output is correct
12 Correct 7 ms 14412 KB Output is correct
13 Correct 7 ms 14408 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14412 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14408 KB Output is correct
20 Correct 10 ms 14408 KB Output is correct
21 Correct 10 ms 14476 KB Output is correct
22 Correct 7 ms 14408 KB Output is correct
23 Correct 8 ms 14336 KB Output is correct
24 Correct 7 ms 14388 KB Output is correct
25 Correct 9 ms 14416 KB Output is correct
26 Correct 7 ms 14408 KB Output is correct
27 Correct 7 ms 14408 KB Output is correct
28 Correct 9 ms 14392 KB Output is correct
29 Correct 7 ms 14412 KB Output is correct
30 Correct 7 ms 14400 KB Output is correct
31 Correct 10 ms 14388 KB Output is correct
32 Correct 7 ms 14308 KB Output is correct
33 Correct 9 ms 14416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 7 ms 14404 KB Output is correct
4 Correct 6 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 8 ms 14412 KB Output is correct
7 Correct 11 ms 14420 KB Output is correct
8 Correct 10 ms 14396 KB Output is correct
9 Correct 13 ms 14420 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 7 ms 14408 KB Output is correct
12 Correct 7 ms 14412 KB Output is correct
13 Correct 7 ms 14408 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14412 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14408 KB Output is correct
20 Correct 10 ms 14408 KB Output is correct
21 Correct 10 ms 14476 KB Output is correct
22 Correct 7 ms 14408 KB Output is correct
23 Correct 8 ms 14336 KB Output is correct
24 Correct 7 ms 14388 KB Output is correct
25 Correct 9 ms 14416 KB Output is correct
26 Correct 7 ms 14408 KB Output is correct
27 Correct 7 ms 14408 KB Output is correct
28 Correct 9 ms 14392 KB Output is correct
29 Correct 7 ms 14412 KB Output is correct
30 Correct 7 ms 14400 KB Output is correct
31 Correct 10 ms 14388 KB Output is correct
32 Correct 7 ms 14308 KB Output is correct
33 Correct 9 ms 14416 KB Output is correct
34 Correct 19 ms 14564 KB Output is correct
35 Correct 416 ms 18488 KB Output is correct
36 Correct 424 ms 21024 KB Output is correct
37 Correct 441 ms 20964 KB Output is correct
38 Correct 447 ms 20648 KB Output is correct
39 Correct 14 ms 14672 KB Output is correct
40 Correct 16 ms 15052 KB Output is correct
41 Correct 17 ms 15068 KB Output is correct
42 Correct 15 ms 14680 KB Output is correct
43 Correct 16 ms 14820 KB Output is correct
44 Correct 15 ms 14796 KB Output is correct
45 Correct 14 ms 14796 KB Output is correct
46 Correct 16 ms 14664 KB Output is correct
47 Correct 17 ms 15004 KB Output is correct
48 Correct 16 ms 14952 KB Output is correct
49 Correct 28 ms 15724 KB Output is correct
50 Correct 426 ms 21024 KB Output is correct
51 Correct 25 ms 15052 KB Output is correct
52 Correct 425 ms 19320 KB Output is correct
53 Correct 29 ms 15564 KB Output is correct
54 Correct 416 ms 20016 KB Output is correct
55 Correct 16 ms 15160 KB Output is correct
56 Correct 18 ms 15188 KB Output is correct
57 Correct 17 ms 15152 KB Output is correct
58 Correct 16 ms 15104 KB Output is correct
59 Correct 16 ms 15180 KB Output is correct
60 Correct 395 ms 17744 KB Output is correct
61 Correct 17 ms 14932 KB Output is correct
62 Correct 417 ms 20420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14404 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 7 ms 14404 KB Output is correct
4 Correct 6 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 8 ms 14412 KB Output is correct
7 Correct 11 ms 14420 KB Output is correct
8 Correct 10 ms 14396 KB Output is correct
9 Correct 13 ms 14420 KB Output is correct
10 Correct 8 ms 14408 KB Output is correct
11 Correct 7 ms 14408 KB Output is correct
12 Correct 7 ms 14412 KB Output is correct
13 Correct 7 ms 14408 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14412 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14408 KB Output is correct
20 Correct 10 ms 14408 KB Output is correct
21 Correct 10 ms 14476 KB Output is correct
22 Correct 7 ms 14408 KB Output is correct
23 Correct 8 ms 14336 KB Output is correct
24 Correct 7 ms 14388 KB Output is correct
25 Correct 9 ms 14416 KB Output is correct
26 Correct 7 ms 14408 KB Output is correct
27 Correct 7 ms 14408 KB Output is correct
28 Correct 9 ms 14392 KB Output is correct
29 Correct 7 ms 14412 KB Output is correct
30 Correct 7 ms 14400 KB Output is correct
31 Correct 10 ms 14388 KB Output is correct
32 Correct 7 ms 14308 KB Output is correct
33 Correct 9 ms 14416 KB Output is correct
34 Correct 19 ms 14564 KB Output is correct
35 Correct 416 ms 18488 KB Output is correct
36 Correct 424 ms 21024 KB Output is correct
37 Correct 441 ms 20964 KB Output is correct
38 Correct 447 ms 20648 KB Output is correct
39 Correct 14 ms 14672 KB Output is correct
40 Correct 16 ms 15052 KB Output is correct
41 Correct 17 ms 15068 KB Output is correct
42 Correct 15 ms 14680 KB Output is correct
43 Correct 16 ms 14820 KB Output is correct
44 Correct 15 ms 14796 KB Output is correct
45 Correct 14 ms 14796 KB Output is correct
46 Correct 16 ms 14664 KB Output is correct
47 Correct 17 ms 15004 KB Output is correct
48 Correct 16 ms 14952 KB Output is correct
49 Correct 28 ms 15724 KB Output is correct
50 Correct 426 ms 21024 KB Output is correct
51 Correct 25 ms 15052 KB Output is correct
52 Correct 425 ms 19320 KB Output is correct
53 Correct 29 ms 15564 KB Output is correct
54 Correct 416 ms 20016 KB Output is correct
55 Correct 16 ms 15160 KB Output is correct
56 Correct 18 ms 15188 KB Output is correct
57 Correct 17 ms 15152 KB Output is correct
58 Correct 16 ms 15104 KB Output is correct
59 Correct 16 ms 15180 KB Output is correct
60 Correct 395 ms 17744 KB Output is correct
61 Correct 17 ms 14932 KB Output is correct
62 Correct 417 ms 20420 KB Output is correct
63 Correct 860 ms 65180 KB Output is correct
64 Correct 895 ms 65088 KB Output is correct
65 Correct 871 ms 64828 KB Output is correct
66 Correct 458 ms 32108 KB Output is correct
67 Correct 700 ms 61540 KB Output is correct
68 Correct 450 ms 34476 KB Output is correct
69 Correct 703 ms 35680 KB Output is correct
70 Correct 478 ms 34372 KB Output is correct
71 Correct 472 ms 34480 KB Output is correct
72 Correct 704 ms 45752 KB Output is correct
73 Correct 700 ms 47400 KB Output is correct
74 Correct 1347 ms 69956 KB Output is correct
75 Correct 967 ms 42432 KB Output is correct
76 Correct 1113 ms 54584 KB Output is correct
77 Correct 1107 ms 54780 KB Output is correct
78 Correct 449 ms 40564 KB Output is correct
79 Correct 796 ms 44856 KB Output is correct
80 Correct 407 ms 36900 KB Output is correct
81 Correct 734 ms 40432 KB Output is correct
82 Correct 1063 ms 55216 KB Output is correct
83 Correct 1041 ms 55212 KB Output is correct
84 Correct 915 ms 53744 KB Output is correct
85 Correct 880 ms 53660 KB Output is correct
86 Correct 589 ms 67256 KB Output is correct
87 Correct 705 ms 69428 KB Output is correct
88 Correct 726 ms 46716 KB Output is correct
89 Correct 1061 ms 52588 KB Output is correct