Submission #766868

# Submission time Handle Problem Language Result Execution time Memory
766868 2023-06-26T08:20:24 Z onjo0127 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
9 ms 19104 KB
#include <bits/stdc++.h>
#define sz(V) ((int)V.size())
using namespace std;
using pii = pair<int, int>;
using ll = long long;

int P[100009];
set<int> I[100009], A[100009], II[100009], OO[100009];
ll ans;

int root(int x) {
	if(P[x] == x) return x;
	return P[x] = root(P[x]);
}

ll f(int x) {
	return 1LL * sz(A[x]) * (sz(I[x]) + sz(A[x]) - 1);
}

void merg(int u, int v) {
	if(sz(A[u]) + sz(I[u]) + sz(II[u]) + sz(OO[u]) > sz(A[v]) + sz(I[v]) + sz(II[v]) + sz(OO[v])) swap(u, v);
	ans -= f(u) + f(v);
	for(auto& it: A[u]) if(I[v].find(it) != I[v].end()) I[v].erase(it);
	for(auto& it: I[u]) if(A[v].find(it) == A[v].end()) I[v].insert(it);
	for(auto& it: A[u]) A[v].insert(it);
	for(auto& it: II[u]) if(it != u && it != v) {
		OO[it].erase(u);
		OO[it].insert(v);
		II[v].insert(it);
	}
	for(auto& it: OO[u]) if(it != u && it != v) {
		II[it].erase(u);
		II[it].insert(v);
		OO[v].insert(it);
	}
	ans += f(v);
	P[u] = v;
}

int main() {
	int N, M; scanf("%d%d", &N, &M);
	for(int i=1; i<=N; i++) {
		P[i] = i;
		A[i] = {i};
	}
	while(M--) {
		int u, v; scanf("%d%d", &u, &v);
		int U = root(u), V = root(v);
		if(U != V) {
			ans -= f(V);
			I[V].insert(u);
			II[V].insert(U);
			OO[U].insert(V);
			ans += f(V);
			if(II[U].find(V) != II[U].end()) merg(U, V);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:41:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  int N, M; scanf("%d%d", &N, &M);
      |            ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:47:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |   int u, v; scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19012 KB Output is correct
3 Incorrect 9 ms 19104 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19012 KB Output is correct
3 Incorrect 9 ms 19104 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB Output is correct
2 Correct 9 ms 19012 KB Output is correct
3 Incorrect 9 ms 19104 KB Output isn't correct
4 Halted 0 ms 0 KB -