Submission #766732

# Submission time Handle Problem Language Result Execution time Memory
766732 2023-06-26T05:45:49 Z qwerasdfzxcl Making Friends on Joitter is Fun (JOI20_joitter2) C++17
1 / 100
5000 ms 14688 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

set<int> G[100100];
ll ans;

struct DSU{
	ll path[100100], sz[100100];
	set<int> st[100100], elem[100100];

	void init(int n){
		for (int i=1;i<=n;i++){
			path[i] = i;
			st[i].clear();
			elem[i].clear();
			elem[i].insert(i);
			sz[i] = 1;
		}
	}

	int find(int s){
		if (s==path[s]) return s;
		return path[s] = find(path[s]);
	}

	void merge(int x, int y){
		// printf(" merge %d %d\n", x, y);
		x = find(x), y = find(y);
		assert(x!=y);

		if (st[x].size() + elem[x].size() < st[y].size() + elem[y].size()) swap(x, y);

		ans -= (ll)st[x].size() * sz[x];
		ans -= (ll)st[y].size() * sz[y];
		ans -= sz[x] * (sz[x]-1);
		ans -= sz[y] * (sz[y]-1);
		// printf(" delete -> %lld\n", ans);

		for (auto p:st[y]) if (find(p)!=x){
			st[x].insert(p);
			G[find(p)].insert(x);
		} 

		for (auto p:elem[y]){
			auto iter = st[x].find(p);
			if (iter!=st[x].end()) st[x].erase(iter);
			elem[x].insert(p);
		}

		sz[x] += sz[y];
		ans += (ll)st[x].size() * sz[x];
		ans += sz[x] * (sz[x]-1);
		// printf(" add -> %lld (%d %d)\n", ans, (int)st[x].size(), sz[x]);

		path[y] = x;
	}

	void insert(int x, int y){
		y = find(y);
		if (st[y].find(x)!=st[y].end()) return;

		ans += sz[y];
		st[y].insert(x);
		G[find(x)].insert(y);
	}
}dsu;

void update(int x, int y){
	int cx = dsu.find(x), cy = dsu.find(y);
	if (cx==cy) return;
	
	if (G[cy].find(cx)!=G[cy].end()) dsu.merge(cx, cy);
	
	else dsu.insert(x, cy);
}

int adj[55][55];
int naive(int n, int x, int y){
	adj[x][y] = 1;

	while(true){
		int flag = 0;
		for (int i=1;i<=n;i++){
			for (int j=1;j<=n;j++){
				for (int k=1;k<=n;k++) if (i!=j && j!=k && i!=k){
					if (flag) break;
					if (adj[i][j] && adj[j][k] && adj[k][j] && !adj[i][k]){
						adj[i][k] = 1;
						flag = 1;
					}
				}
				if (flag) break;
			}
			if (flag) break;
		}

		if (!flag) break;
	}

	int ret = 0;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++) ret += adj[i][j];
	}

	return ret;
}

int main(){
	int n, m;
	scanf("%d %d", &n, &m);

	dsu.init(n);

	for (int i=1;i<=m;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		update(x, y);
		printf("%d\n", naive(n, x, y));
		// printf("%lld\n", ans);
	}
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:112:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 1091 ms 14436 KB Output is correct
8 Correct 1144 ms 14464 KB Output is correct
9 Correct 1045 ms 14444 KB Output is correct
10 Correct 387 ms 14428 KB Output is correct
11 Correct 392 ms 14420 KB Output is correct
12 Correct 398 ms 14340 KB Output is correct
13 Correct 381 ms 14428 KB Output is correct
14 Correct 420 ms 14436 KB Output is correct
15 Correct 389 ms 14424 KB Output is correct
16 Correct 389 ms 14432 KB Output is correct
17 Correct 369 ms 14420 KB Output is correct
18 Correct 389 ms 14432 KB Output is correct
19 Correct 382 ms 14424 KB Output is correct
20 Correct 464 ms 14548 KB Output is correct
21 Correct 1160 ms 14576 KB Output is correct
22 Correct 444 ms 14436 KB Output is correct
23 Correct 715 ms 14456 KB Output is correct
24 Correct 515 ms 14436 KB Output is correct
25 Correct 944 ms 14456 KB Output is correct
26 Correct 403 ms 14548 KB Output is correct
27 Correct 394 ms 14428 KB Output is correct
28 Correct 393 ms 14440 KB Output is correct
29 Correct 408 ms 14436 KB Output is correct
30 Correct 430 ms 14420 KB Output is correct
31 Correct 985 ms 14440 KB Output is correct
32 Correct 405 ms 14428 KB Output is correct
33 Correct 921 ms 14688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 1091 ms 14436 KB Output is correct
8 Correct 1144 ms 14464 KB Output is correct
9 Correct 1045 ms 14444 KB Output is correct
10 Correct 387 ms 14428 KB Output is correct
11 Correct 392 ms 14420 KB Output is correct
12 Correct 398 ms 14340 KB Output is correct
13 Correct 381 ms 14428 KB Output is correct
14 Correct 420 ms 14436 KB Output is correct
15 Correct 389 ms 14424 KB Output is correct
16 Correct 389 ms 14432 KB Output is correct
17 Correct 369 ms 14420 KB Output is correct
18 Correct 389 ms 14432 KB Output is correct
19 Correct 382 ms 14424 KB Output is correct
20 Correct 464 ms 14548 KB Output is correct
21 Correct 1160 ms 14576 KB Output is correct
22 Correct 444 ms 14436 KB Output is correct
23 Correct 715 ms 14456 KB Output is correct
24 Correct 515 ms 14436 KB Output is correct
25 Correct 944 ms 14456 KB Output is correct
26 Correct 403 ms 14548 KB Output is correct
27 Correct 394 ms 14428 KB Output is correct
28 Correct 393 ms 14440 KB Output is correct
29 Correct 408 ms 14436 KB Output is correct
30 Correct 430 ms 14420 KB Output is correct
31 Correct 985 ms 14440 KB Output is correct
32 Correct 405 ms 14428 KB Output is correct
33 Correct 921 ms 14688 KB Output is correct
34 Execution timed out 5032 ms 14476 KB Time limit exceeded
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14412 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 1091 ms 14436 KB Output is correct
8 Correct 1144 ms 14464 KB Output is correct
9 Correct 1045 ms 14444 KB Output is correct
10 Correct 387 ms 14428 KB Output is correct
11 Correct 392 ms 14420 KB Output is correct
12 Correct 398 ms 14340 KB Output is correct
13 Correct 381 ms 14428 KB Output is correct
14 Correct 420 ms 14436 KB Output is correct
15 Correct 389 ms 14424 KB Output is correct
16 Correct 389 ms 14432 KB Output is correct
17 Correct 369 ms 14420 KB Output is correct
18 Correct 389 ms 14432 KB Output is correct
19 Correct 382 ms 14424 KB Output is correct
20 Correct 464 ms 14548 KB Output is correct
21 Correct 1160 ms 14576 KB Output is correct
22 Correct 444 ms 14436 KB Output is correct
23 Correct 715 ms 14456 KB Output is correct
24 Correct 515 ms 14436 KB Output is correct
25 Correct 944 ms 14456 KB Output is correct
26 Correct 403 ms 14548 KB Output is correct
27 Correct 394 ms 14428 KB Output is correct
28 Correct 393 ms 14440 KB Output is correct
29 Correct 408 ms 14436 KB Output is correct
30 Correct 430 ms 14420 KB Output is correct
31 Correct 985 ms 14440 KB Output is correct
32 Correct 405 ms 14428 KB Output is correct
33 Correct 921 ms 14688 KB Output is correct
34 Execution timed out 5032 ms 14476 KB Time limit exceeded
35 Halted 0 ms 0 KB -