답안 #939391

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939391 2024-03-06T10:19:42 Z WonderfulWhale 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
1 / 100
5000 ms 17664 KB
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const int MAXN = 100'009;

int cnt[MAXN];
int p[MAXN];
int sz[MAXN];
set<int> in[MAXN];
set<int> out[MAXN];
int ans;

int Find(int x) {
	return x==p[x]?x:p[x]=Find(p[x]);
}

void Union(int a, int b) {
	a = Find(a);
	b = Find(b);
	//cout << "Union: " << a << " " << b << "\n";
	if(a==b) return;
	vector<int> union_queue;
	if(sz(out[a])+sz(in[a])<sz(out[b])+sz(in[b])) swap(a, b);
	for(int x:out[b]) {
		//cerr << "trying: "<< x << "\n";
		if(in[a].count(x)) {
			//cerr << "found: " << x << "\n";
			union_queue.pb(x);
		}
		//cerr << "adding: " << x << "\n";
		out[a].insert(x);
		in[x].erase(b);
		in[x].insert(a);
	}
	out[b].clear();
	//if(sz(in[a])<sz(in[b])) swap(a, b);
	for(int x:in[b]) {
		//cerr << "trying: "<< x << "\n";
		if(out[a].count(x)) {
			//cerr << "found: " << x << "\n";
			union_queue.pb(x);
		}
		in[a].insert(x);
		//cerr << "changin:  "<< x << " from: " << b << " to "<< a << "\n";
		out[x].erase(b);
		out[x].insert(a);
	}
	in[b].clear();
	p[b] = a;
	ans -= sz[a]*(sz[a]-1);
	ans -= sz[b]*(sz[b]-1);
	sz[a] += sz[b];
	ans += sz[a]*(sz[a]-1);
	for(int x:union_queue) {
		Union(a, x);
	}
}

set<pii> S;
set<int> used[MAXN];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int n, m;
	cin >> n >> m;
	iota(p, p+n+1, 0);
	fill(sz, sz+n+1, 1);
	for(int i=0;i<m;i++) {
		int a, b;
		cin >> a >> b;
		S.insert({a, b});
		int A = Find(a);
		int B = Find(b);
		if(A==B) goto skip;
		out[A].insert(B);
		in[B].insert(A);
		if(in[A].count(B)) {
			Union(A, B);
		}
		skip:
		int cnt = 0;
		for(int i=1;i<=n;i++) {
			used[i].clear();
		}
		for(pii x:S) {
			if(Find(x.st)==Find(x.nd)) continue;
			if(used[x.st].count(Find(x.nd))) continue;
			cnt += sz[Find(x.nd)];
			used[x.st].insert(Find(x.nd));
		}
		//cerr << ans << " " << cnt << "\n";
		cout << ans+cnt << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15004 KB Output is correct
7 Correct 24 ms 14940 KB Output is correct
8 Correct 27 ms 14940 KB Output is correct
9 Correct 24 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 3 ms 14992 KB Output is correct
12 Correct 4 ms 14936 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 4 ms 14968 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14996 KB Output is correct
21 Correct 25 ms 14940 KB Output is correct
22 Correct 4 ms 14992 KB Output is correct
23 Correct 7 ms 14940 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 16 ms 14940 KB Output is correct
26 Correct 3 ms 14936 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14940 KB Output is correct
30 Correct 3 ms 14776 KB Output is correct
31 Correct 17 ms 15296 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 17 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15004 KB Output is correct
7 Correct 24 ms 14940 KB Output is correct
8 Correct 27 ms 14940 KB Output is correct
9 Correct 24 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 3 ms 14992 KB Output is correct
12 Correct 4 ms 14936 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 4 ms 14968 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14996 KB Output is correct
21 Correct 25 ms 14940 KB Output is correct
22 Correct 4 ms 14992 KB Output is correct
23 Correct 7 ms 14940 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 16 ms 14940 KB Output is correct
26 Correct 3 ms 14936 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14940 KB Output is correct
30 Correct 3 ms 14776 KB Output is correct
31 Correct 17 ms 15296 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 17 ms 14940 KB Output is correct
34 Correct 569 ms 15700 KB Output is correct
35 Execution timed out 5045 ms 17664 KB Time limit exceeded
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 4 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15004 KB Output is correct
7 Correct 24 ms 14940 KB Output is correct
8 Correct 27 ms 14940 KB Output is correct
9 Correct 24 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 3 ms 14992 KB Output is correct
12 Correct 4 ms 14936 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 4 ms 14968 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14996 KB Output is correct
21 Correct 25 ms 14940 KB Output is correct
22 Correct 4 ms 14992 KB Output is correct
23 Correct 7 ms 14940 KB Output is correct
24 Correct 4 ms 14940 KB Output is correct
25 Correct 16 ms 14940 KB Output is correct
26 Correct 3 ms 14936 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 4 ms 14940 KB Output is correct
30 Correct 3 ms 14776 KB Output is correct
31 Correct 17 ms 15296 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 17 ms 14940 KB Output is correct
34 Correct 569 ms 15700 KB Output is correct
35 Execution timed out 5045 ms 17664 KB Time limit exceeded
36 Halted 0 ms 0 KB -