답안 #709613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709613 2023-03-14T02:35:18 Z GusterGoose27 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
511 ms 58844 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1e5;
const int MAXM = 3e5;
vector<int> contained[MAXN];
vector<int> out_edges[MAXN];
vector<int> in_edges[MAXN];
int uf[MAXN];
set<int> inc[MAXN]; // specific nodes
set<int> out[MAXN]; // this is just general ccs
ll ans = 0;
int n, m;

void undo(int i) {
	ans -= (ll)contained[i].size()*inc[i].size();
	ans -= (ll)contained[i].size()*((int)contained[i].size()-1);
}

void make(int i) {
	ans += (ll)contained[i].size()*inc[i].size();
	ans += (ll)contained[i].size()*((int)contained[i].size()-1);
}

void merge(int a, int b) {
	a = uf[a];
	b = uf[b];
	if (a == b) return;
	if (contained[a].size() < contained[b].size()) swap(a, b);
	undo(a);
	undo(b);
	out[a].erase(b);
	out[b].erase(a);
	for (int v: contained[b]) {
		inc[a].erase(v);
		uf[v] = a;
		contained[a].push_back(v);
	}
	for (int w: inc[b]) {
		if (uf[w] != a) {
			inc[a].insert(w);
			out[uf[w]].insert(a);
			out[uf[w]].erase(b);
		}
	}
	for (int w: out[b]) {
		out[a].insert(w);
	}
	make(a);
	vector<int> ex;
	for (int v: inc[b]) {
		if (out[a].find(uf[v]) != out[a].end()) ex.push_back(v);
	}
	for (int v: out[b]) {
		if (out[v].find(a) != out[v].end()) ex.push_back(v);
	}
	out[b].clear();
	inc[b].clear();
	contained[b].clear();
	for (int v: ex) merge(v, a);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m;
	iota(uf, uf+n, 0);
	for (int i = 0; i < n; i++) contained[i].push_back(i);
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;
		x--; y--;
		if (uf[x] != uf[y]) {
			if (out[uf[y]].find(uf[x]) != out[uf[y]].end()) {
				merge(x, y);
			}
			else {
				undo(uf[y]);
				inc[uf[y]].insert(x);
				make(uf[y]);
				out[uf[x]].insert(uf[y]);
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 9 ms 16724 KB Output is correct
4 Correct 9 ms 16752 KB Output is correct
5 Correct 9 ms 16752 KB Output is correct
6 Correct 10 ms 16724 KB Output is correct
7 Correct 10 ms 16772 KB Output is correct
8 Correct 9 ms 16724 KB Output is correct
9 Correct 10 ms 16744 KB Output is correct
10 Correct 8 ms 16724 KB Output is correct
11 Correct 11 ms 16728 KB Output is correct
12 Correct 10 ms 16772 KB Output is correct
13 Correct 9 ms 16768 KB Output is correct
14 Correct 9 ms 16700 KB Output is correct
15 Correct 9 ms 16724 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 8 ms 16692 KB Output is correct
18 Correct 8 ms 16768 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 8 ms 16724 KB Output is correct
21 Correct 9 ms 16724 KB Output is correct
22 Correct 9 ms 16724 KB Output is correct
23 Correct 9 ms 16708 KB Output is correct
24 Correct 9 ms 16724 KB Output is correct
25 Correct 9 ms 16776 KB Output is correct
26 Correct 8 ms 16852 KB Output is correct
27 Correct 9 ms 16760 KB Output is correct
28 Correct 8 ms 16764 KB Output is correct
29 Correct 8 ms 16768 KB Output is correct
30 Correct 8 ms 16756 KB Output is correct
31 Correct 9 ms 16780 KB Output is correct
32 Correct 9 ms 16692 KB Output is correct
33 Correct 9 ms 16744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 9 ms 16724 KB Output is correct
4 Correct 9 ms 16752 KB Output is correct
5 Correct 9 ms 16752 KB Output is correct
6 Correct 10 ms 16724 KB Output is correct
7 Correct 10 ms 16772 KB Output is correct
8 Correct 9 ms 16724 KB Output is correct
9 Correct 10 ms 16744 KB Output is correct
10 Correct 8 ms 16724 KB Output is correct
11 Correct 11 ms 16728 KB Output is correct
12 Correct 10 ms 16772 KB Output is correct
13 Correct 9 ms 16768 KB Output is correct
14 Correct 9 ms 16700 KB Output is correct
15 Correct 9 ms 16724 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 8 ms 16692 KB Output is correct
18 Correct 8 ms 16768 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 8 ms 16724 KB Output is correct
21 Correct 9 ms 16724 KB Output is correct
22 Correct 9 ms 16724 KB Output is correct
23 Correct 9 ms 16708 KB Output is correct
24 Correct 9 ms 16724 KB Output is correct
25 Correct 9 ms 16776 KB Output is correct
26 Correct 8 ms 16852 KB Output is correct
27 Correct 9 ms 16760 KB Output is correct
28 Correct 8 ms 16764 KB Output is correct
29 Correct 8 ms 16768 KB Output is correct
30 Correct 8 ms 16756 KB Output is correct
31 Correct 9 ms 16780 KB Output is correct
32 Correct 9 ms 16692 KB Output is correct
33 Correct 9 ms 16744 KB Output is correct
34 Correct 12 ms 16980 KB Output is correct
35 Correct 73 ms 22088 KB Output is correct
36 Correct 96 ms 24220 KB Output is correct
37 Correct 110 ms 24300 KB Output is correct
38 Correct 93 ms 24068 KB Output is correct
39 Correct 10 ms 16852 KB Output is correct
40 Correct 11 ms 17012 KB Output is correct
41 Correct 11 ms 16980 KB Output is correct
42 Correct 10 ms 16852 KB Output is correct
43 Correct 11 ms 16908 KB Output is correct
44 Correct 11 ms 16908 KB Output is correct
45 Correct 10 ms 16904 KB Output is correct
46 Correct 11 ms 16852 KB Output is correct
47 Correct 12 ms 16980 KB Output is correct
48 Correct 11 ms 16980 KB Output is correct
49 Correct 16 ms 17544 KB Output is correct
50 Correct 89 ms 24336 KB Output is correct
51 Correct 15 ms 17104 KB Output is correct
52 Correct 106 ms 23260 KB Output is correct
53 Correct 15 ms 17524 KB Output is correct
54 Correct 89 ms 23848 KB Output is correct
55 Correct 13 ms 17260 KB Output is correct
56 Correct 13 ms 17236 KB Output is correct
57 Correct 12 ms 17544 KB Output is correct
58 Correct 12 ms 17604 KB Output is correct
59 Correct 12 ms 16980 KB Output is correct
60 Correct 72 ms 21812 KB Output is correct
61 Correct 12 ms 16980 KB Output is correct
62 Correct 93 ms 23988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16724 KB Output is correct
2 Correct 9 ms 16724 KB Output is correct
3 Correct 9 ms 16724 KB Output is correct
4 Correct 9 ms 16752 KB Output is correct
5 Correct 9 ms 16752 KB Output is correct
6 Correct 10 ms 16724 KB Output is correct
7 Correct 10 ms 16772 KB Output is correct
8 Correct 9 ms 16724 KB Output is correct
9 Correct 10 ms 16744 KB Output is correct
10 Correct 8 ms 16724 KB Output is correct
11 Correct 11 ms 16728 KB Output is correct
12 Correct 10 ms 16772 KB Output is correct
13 Correct 9 ms 16768 KB Output is correct
14 Correct 9 ms 16700 KB Output is correct
15 Correct 9 ms 16724 KB Output is correct
16 Correct 9 ms 16724 KB Output is correct
17 Correct 8 ms 16692 KB Output is correct
18 Correct 8 ms 16768 KB Output is correct
19 Correct 9 ms 16768 KB Output is correct
20 Correct 8 ms 16724 KB Output is correct
21 Correct 9 ms 16724 KB Output is correct
22 Correct 9 ms 16724 KB Output is correct
23 Correct 9 ms 16708 KB Output is correct
24 Correct 9 ms 16724 KB Output is correct
25 Correct 9 ms 16776 KB Output is correct
26 Correct 8 ms 16852 KB Output is correct
27 Correct 9 ms 16760 KB Output is correct
28 Correct 8 ms 16764 KB Output is correct
29 Correct 8 ms 16768 KB Output is correct
30 Correct 8 ms 16756 KB Output is correct
31 Correct 9 ms 16780 KB Output is correct
32 Correct 9 ms 16692 KB Output is correct
33 Correct 9 ms 16744 KB Output is correct
34 Correct 12 ms 16980 KB Output is correct
35 Correct 73 ms 22088 KB Output is correct
36 Correct 96 ms 24220 KB Output is correct
37 Correct 110 ms 24300 KB Output is correct
38 Correct 93 ms 24068 KB Output is correct
39 Correct 10 ms 16852 KB Output is correct
40 Correct 11 ms 17012 KB Output is correct
41 Correct 11 ms 16980 KB Output is correct
42 Correct 10 ms 16852 KB Output is correct
43 Correct 11 ms 16908 KB Output is correct
44 Correct 11 ms 16908 KB Output is correct
45 Correct 10 ms 16904 KB Output is correct
46 Correct 11 ms 16852 KB Output is correct
47 Correct 12 ms 16980 KB Output is correct
48 Correct 11 ms 16980 KB Output is correct
49 Correct 16 ms 17544 KB Output is correct
50 Correct 89 ms 24336 KB Output is correct
51 Correct 15 ms 17104 KB Output is correct
52 Correct 106 ms 23260 KB Output is correct
53 Correct 15 ms 17524 KB Output is correct
54 Correct 89 ms 23848 KB Output is correct
55 Correct 13 ms 17260 KB Output is correct
56 Correct 13 ms 17236 KB Output is correct
57 Correct 12 ms 17544 KB Output is correct
58 Correct 12 ms 17604 KB Output is correct
59 Correct 12 ms 16980 KB Output is correct
60 Correct 72 ms 21812 KB Output is correct
61 Correct 12 ms 16980 KB Output is correct
62 Correct 93 ms 23988 KB Output is correct
63 Correct 347 ms 53904 KB Output is correct
64 Correct 333 ms 54008 KB Output is correct
65 Correct 350 ms 53904 KB Output is correct
66 Correct 96 ms 25176 KB Output is correct
67 Correct 190 ms 30716 KB Output is correct
68 Correct 101 ms 25156 KB Output is correct
69 Correct 217 ms 29836 KB Output is correct
70 Correct 110 ms 25144 KB Output is correct
71 Correct 104 ms 25156 KB Output is correct
72 Correct 280 ms 29612 KB Output is correct
73 Correct 223 ms 29884 KB Output is correct
74 Correct 511 ms 38984 KB Output is correct
75 Correct 338 ms 34292 KB Output is correct
76 Correct 433 ms 37900 KB Output is correct
77 Correct 428 ms 37968 KB Output is correct
78 Correct 145 ms 28472 KB Output is correct
79 Correct 229 ms 31816 KB Output is correct
80 Correct 122 ms 28496 KB Output is correct
81 Correct 205 ms 31856 KB Output is correct
82 Correct 461 ms 43284 KB Output is correct
83 Correct 446 ms 43364 KB Output is correct
84 Correct 434 ms 58844 KB Output is correct
85 Correct 398 ms 58820 KB Output is correct
86 Correct 137 ms 29704 KB Output is correct
87 Correct 141 ms 31824 KB Output is correct
88 Correct 241 ms 29764 KB Output is correct
89 Correct 389 ms 37232 KB Output is correct