답안 #709761

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709761 2023-03-14T11:39:38 Z khshg 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
514 ms 67664 KB
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using str = string;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second

template<class T> using V = vector<T>;
#define arr array
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;

#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()

#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)

template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }

const int maxn = 1e5;
int N, M, ans;
queue<pi> q;
int bulgem[maxn], ataman[maxn];
set<int> radj[maxn], adj[maxn], K[maxn];

int parent(int A) {
	return (A == ataman[A] ? A : ataman[A] = parent(ataman[A]));
}

void merge_comp(int A, int B) {
	A = parent(A); B = parent(B);
	if(A == B) return;
	ans += 1LL * bulgem[A] * sz(K[B]) + 1LL * bulgem[B] * sz(K[A]);
	if(sz(K[A]) < sz(K[B])) swap(A, B);
	trav(u, K[B]) {
		if(K[A].find(u) != end(K[A])) ans -= 1LL * (bulgem[A] + bulgem[B]);
		else K[A].ins(u);
	}
	adj[A].erase(B);
	adj[B].erase(A);
	radj[A].erase(B);
	radj[B].erase(A);
	trav(u, adj[B]) {
		radj[u].erase(B);
		adj[A].ins(u);
		radj[u].ins(A);
		if(adj[u].find(A) != end(adj[u])) q.push({u, A});
	}
	trav(u, radj[B]) {
		adj[u].erase(B);
		radj[A].ins(u);
		adj[u].ins(A);
		if(adj[A].find(u) != end(adj[A])) q.push({u, A});
	}
	bulgem[A] += bulgem[B];
	ataman[B] = A;
}

void add_edge(int A, int B) {
	B = parent(B);
	if(K[B].find(A) != end(K[B])) return;
	ans += bulgem[B];
	K[B].ins(A);
	A = parent(A);
	adj[A].ins(B);
	radj[B].ins(A);
	if(adj[B].count(A)) merge_comp(A, B);
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N, M; cin >> N >> M;
	F0R(i, N) {
		ataman[i] = i;
		bulgem[i] = 1;
		K[i].ins(i);
	}
	F0R(i, M) {
		int u, v; cin >> u >> v;
		--u; --v;
		add_edge(u, v);
		while(!q.empty()) {
			tie(u, v) = q.ft;
			q.pop();
			merge_comp(u, v);
		}
		cout << ans << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14364 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14384 KB Output is correct
8 Correct 8 ms 14424 KB Output is correct
9 Correct 10 ms 14428 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14368 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 8 ms 14332 KB Output is correct
16 Correct 9 ms 14520 KB Output is correct
17 Correct 8 ms 14416 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14416 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14412 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14364 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14384 KB Output is correct
8 Correct 8 ms 14424 KB Output is correct
9 Correct 10 ms 14428 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14368 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 8 ms 14332 KB Output is correct
16 Correct 9 ms 14520 KB Output is correct
17 Correct 8 ms 14416 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14416 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14412 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 11 ms 14584 KB Output is correct
35 Correct 104 ms 20252 KB Output is correct
36 Correct 129 ms 23028 KB Output is correct
37 Correct 125 ms 23112 KB Output is correct
38 Correct 129 ms 22672 KB Output is correct
39 Correct 10 ms 14676 KB Output is correct
40 Correct 11 ms 15060 KB Output is correct
41 Correct 11 ms 15064 KB Output is correct
42 Correct 10 ms 14680 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 10 ms 14804 KB Output is correct
45 Correct 10 ms 14676 KB Output is correct
46 Correct 10 ms 14676 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 13 ms 14932 KB Output is correct
49 Correct 20 ms 15644 KB Output is correct
50 Correct 137 ms 23160 KB Output is correct
51 Correct 14 ms 15060 KB Output is correct
52 Correct 158 ms 21540 KB Output is correct
53 Correct 17 ms 15572 KB Output is correct
54 Correct 126 ms 22316 KB Output is correct
55 Correct 15 ms 15188 KB Output is correct
56 Correct 13 ms 15188 KB Output is correct
57 Correct 13 ms 15064 KB Output is correct
58 Correct 12 ms 15060 KB Output is correct
59 Correct 11 ms 15188 KB Output is correct
60 Correct 105 ms 20072 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 131 ms 22576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14364 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14384 KB Output is correct
8 Correct 8 ms 14424 KB Output is correct
9 Correct 10 ms 14428 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14368 KB Output is correct
12 Correct 8 ms 14416 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14420 KB Output is correct
15 Correct 8 ms 14332 KB Output is correct
16 Correct 9 ms 14520 KB Output is correct
17 Correct 8 ms 14416 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 8 ms 14420 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 7 ms 14388 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 7 ms 14416 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14416 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14412 KB Output is correct
33 Correct 9 ms 14420 KB Output is correct
34 Correct 11 ms 14584 KB Output is correct
35 Correct 104 ms 20252 KB Output is correct
36 Correct 129 ms 23028 KB Output is correct
37 Correct 125 ms 23112 KB Output is correct
38 Correct 129 ms 22672 KB Output is correct
39 Correct 10 ms 14676 KB Output is correct
40 Correct 11 ms 15060 KB Output is correct
41 Correct 11 ms 15064 KB Output is correct
42 Correct 10 ms 14680 KB Output is correct
43 Correct 10 ms 14804 KB Output is correct
44 Correct 10 ms 14804 KB Output is correct
45 Correct 10 ms 14676 KB Output is correct
46 Correct 10 ms 14676 KB Output is correct
47 Correct 11 ms 14932 KB Output is correct
48 Correct 13 ms 14932 KB Output is correct
49 Correct 20 ms 15644 KB Output is correct
50 Correct 137 ms 23160 KB Output is correct
51 Correct 14 ms 15060 KB Output is correct
52 Correct 158 ms 21540 KB Output is correct
53 Correct 17 ms 15572 KB Output is correct
54 Correct 126 ms 22316 KB Output is correct
55 Correct 15 ms 15188 KB Output is correct
56 Correct 13 ms 15188 KB Output is correct
57 Correct 13 ms 15064 KB Output is correct
58 Correct 12 ms 15060 KB Output is correct
59 Correct 11 ms 15188 KB Output is correct
60 Correct 105 ms 20072 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 131 ms 22576 KB Output is correct
63 Correct 514 ms 67596 KB Output is correct
64 Correct 509 ms 67664 KB Output is correct
65 Correct 513 ms 67560 KB Output is correct
66 Incorrect 209 ms 33612 KB Output isn't correct
67 Halted 0 ms 0 KB -