Submission #871663

# Submission time Handle Problem Language Result Execution time Memory
871663 2023-11-11T08:44:06 Z AmirAli_H1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
664 ms 69820 KB
// In the name of Allah
 
#include <bits/stdc++.h>
using namespace std;
 
typedef 	long long int			ll;
typedef 	long double				ld;
typedef 	pair<int, int>			pii;
typedef 	pair<ll, ll>			pll;
 
#define 	all(x)					(x).begin(),(x).end()
#define 	len(x)					((ll) (x).size())
#define 	F						first
#define 	S						second
#define 	pb						push_back
#define 	sep						' '
#define 	endl					'\n'
#define 	Mp						make_pair
#define 	kill(x)					cout << x << '\n', exit(0)
#define 	set_dec(x)				cout << fixed << setprecision(x);
#define 	file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
 
int n, q;
const int maxn = 1e5 + 4;
bool mark[maxn]; queue<int> qu;
int p[maxn]; ll val[maxn]; vector<pii> E;
vector<int> ls[maxn]; set<int> A[maxn];
set<int> adj[maxn], adjr[maxn]; ll res = 0;
 
ll cal(ll sz, ll t) {
	return (sz * t) + sz * (sz - 1);
}
 
void upd(int a) {
	res -= val[a];
	val[a] = cal(len(ls[a]), len(A[a]));
	res += val[a];
}

int get(int a) {
	return p[a];
}

bool ok(int a, int b) {
	a = get(a); b = get(b);
	if (a != b && adj[a].find(b) != adj[a].end() && adj[b].find(a) != adj[b].end()) return 1;
	return 0;
}
 
void merge(int a1, int b1) {
	qu.push(b1); mark[b1] = 1;
	
	while (!qu.empty()) {
		int b1 = qu.front(); mark[b1] = 0;
		qu.pop();
		
		int a = get(a1), b = get(b1);
		if (a == b) continue;
		
		if (len(ls[a]) > len(ls[b])) swap(a, b);
		
		for (int v : A[a]) {
			if (p[v] == a || p[v] == b) continue;
			A[b].insert(v);
		}
		A[a].clear();
		
		for (int v : ls[a]) {
			p[v] = b; ls[b].pb(v);
			A[b].erase(v);
		}
		ls[a].clear();
		
		for (int v : adj[a]) {
			if (v == a || v == b) continue;
			adj[b].insert(v);
			adjr[v].erase(a); adjr[v].insert(b);
		}
		for (int v : adjr[a]) {
			if (v == a || v == b) continue;
			adjr[b].insert(v);
			adj[v].erase(a); adj[v].insert(b);
		}
		adj[b].erase(a); adjr[b].erase(a);
		
		for (int v : adj[a]) {
			if (ok(b, v) && !mark[v]) {
				qu.push(v); mark[v] = 1;
			}
		}
		for (int v : adjr[a]) {
			if (ok(b, v) && !mark[v]) {
				qu.push(v); mark[v] = 1;
			}
		}
		adj[a].clear(); adjr[a].clear();
		
		upd(a), upd(b);
	}
}
 
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> q;
	for (int i = 0; i < n; i++) {
		p[i] = i; ls[i].pb(i);
	}
	
	for (int i = 0; i < q; i++) {
		int u1, v1;
		cin >> u1 >> v1; u1--; v1--;
		E.pb(Mp(u1, v1));
		
		int u = get(u1), v = get(v1);
		if (u == v) {
			cout << res << endl;
			continue;
		}
		
		A[v].insert(u1);
		upd(v);
		
		adj[u].insert(v); adjr[v].insert(u);
		if (ok(u, v)) merge(u, v);
		
		cout << res << endl;
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 5 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 17092 KB Output is correct
8 Correct 4 ms 16984 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 16984 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 3 ms 16988 KB Output is correct
19 Correct 3 ms 16988 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 3 ms 16984 KB Output is correct
23 Correct 3 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 4 ms 16984 KB Output is correct
26 Correct 3 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 3 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 5 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 17092 KB Output is correct
8 Correct 4 ms 16984 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 16984 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 3 ms 16988 KB Output is correct
19 Correct 3 ms 16988 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 3 ms 16984 KB Output is correct
23 Correct 3 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 4 ms 16984 KB Output is correct
26 Correct 3 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 3 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
34 Correct 5 ms 17244 KB Output is correct
35 Correct 58 ms 25296 KB Output is correct
36 Correct 77 ms 28348 KB Output is correct
37 Correct 77 ms 27328 KB Output is correct
38 Correct 73 ms 26556 KB Output is correct
39 Correct 4 ms 16984 KB Output is correct
40 Correct 5 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 16988 KB Output is correct
43 Correct 6 ms 17244 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 4 ms 17080 KB Output is correct
46 Correct 5 ms 16988 KB Output is correct
47 Correct 6 ms 17244 KB Output is correct
48 Correct 6 ms 17244 KB Output is correct
49 Correct 11 ms 18008 KB Output is correct
50 Correct 78 ms 27316 KB Output is correct
51 Correct 8 ms 17496 KB Output is correct
52 Correct 65 ms 25644 KB Output is correct
53 Correct 11 ms 18008 KB Output is correct
54 Correct 79 ms 26820 KB Output is correct
55 Correct 8 ms 17756 KB Output is correct
56 Correct 8 ms 17760 KB Output is correct
57 Correct 8 ms 17756 KB Output is correct
58 Correct 7 ms 17756 KB Output is correct
59 Correct 5 ms 17240 KB Output is correct
60 Correct 55 ms 24512 KB Output is correct
61 Correct 6 ms 17244 KB Output is correct
62 Correct 74 ms 27068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 5 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 17092 KB Output is correct
8 Correct 4 ms 16984 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 3 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 16984 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 3 ms 16988 KB Output is correct
19 Correct 3 ms 16988 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 3 ms 16984 KB Output is correct
23 Correct 3 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 4 ms 16984 KB Output is correct
26 Correct 3 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 3 ms 16988 KB Output is correct
32 Correct 4 ms 16988 KB Output is correct
33 Correct 4 ms 16988 KB Output is correct
34 Correct 5 ms 17244 KB Output is correct
35 Correct 58 ms 25296 KB Output is correct
36 Correct 77 ms 28348 KB Output is correct
37 Correct 77 ms 27328 KB Output is correct
38 Correct 73 ms 26556 KB Output is correct
39 Correct 4 ms 16984 KB Output is correct
40 Correct 5 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 16988 KB Output is correct
43 Correct 6 ms 17244 KB Output is correct
44 Correct 6 ms 17244 KB Output is correct
45 Correct 4 ms 17080 KB Output is correct
46 Correct 5 ms 16988 KB Output is correct
47 Correct 6 ms 17244 KB Output is correct
48 Correct 6 ms 17244 KB Output is correct
49 Correct 11 ms 18008 KB Output is correct
50 Correct 78 ms 27316 KB Output is correct
51 Correct 8 ms 17496 KB Output is correct
52 Correct 65 ms 25644 KB Output is correct
53 Correct 11 ms 18008 KB Output is correct
54 Correct 79 ms 26820 KB Output is correct
55 Correct 8 ms 17756 KB Output is correct
56 Correct 8 ms 17760 KB Output is correct
57 Correct 8 ms 17756 KB Output is correct
58 Correct 7 ms 17756 KB Output is correct
59 Correct 5 ms 17240 KB Output is correct
60 Correct 55 ms 24512 KB Output is correct
61 Correct 6 ms 17244 KB Output is correct
62 Correct 74 ms 27068 KB Output is correct
63 Correct 435 ms 69820 KB Output is correct
64 Correct 435 ms 67628 KB Output is correct
65 Correct 447 ms 69564 KB Output is correct
66 Correct 93 ms 25796 KB Output is correct
67 Correct 211 ms 33472 KB Output is correct
68 Correct 99 ms 25668 KB Output is correct
69 Correct 256 ms 32652 KB Output is correct
70 Correct 94 ms 25648 KB Output is correct
71 Correct 91 ms 25792 KB Output is correct
72 Correct 238 ms 32436 KB Output is correct
73 Correct 246 ms 32728 KB Output is correct
74 Correct 664 ms 45364 KB Output is correct
75 Correct 360 ms 41508 KB Output is correct
76 Correct 531 ms 44456 KB Output is correct
77 Correct 503 ms 45504 KB Output is correct
78 Correct 172 ms 34244 KB Output is correct
79 Correct 242 ms 39320 KB Output is correct
80 Correct 180 ms 34256 KB Output is correct
81 Correct 285 ms 38332 KB Output is correct
82 Correct 602 ms 53188 KB Output is correct
83 Correct 576 ms 53168 KB Output is correct
84 Correct 439 ms 53232 KB Output is correct
85 Correct 480 ms 53056 KB Output is correct
86 Correct 140 ms 30188 KB Output is correct
87 Correct 154 ms 34324 KB Output is correct
88 Correct 242 ms 32436 KB Output is correct
89 Correct 486 ms 45284 KB Output is correct