Submission #709767

# Submission time Handle Problem Language Result Execution time Memory
709767 2023-03-14T11:45:24 Z khshg Making Friends on Joitter is Fun (JOI20_joitter2) C++14
17 / 100
3112 ms 1048576 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); }

#define int long long

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(adj[A]) + sz(radj[A]) < sz(adj[A]) + sz(radj[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;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14436 KB Output is correct
13 Correct 8 ms 14468 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 8 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 7 ms 14416 KB Output is correct
24 Correct 9 ms 14416 KB Output is correct
25 Correct 10 ms 14424 KB Output is correct
26 Correct 9 ms 14368 KB Output is correct
27 Correct 9 ms 14420 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14420 KB Output is correct
30 Correct 8 ms 14420 KB Output is correct
31 Correct 8 ms 14368 KB Output is correct
32 Correct 7 ms 14416 KB Output is correct
33 Correct 8 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14436 KB Output is correct
13 Correct 8 ms 14468 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 8 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 7 ms 14416 KB Output is correct
24 Correct 9 ms 14416 KB Output is correct
25 Correct 10 ms 14424 KB Output is correct
26 Correct 9 ms 14368 KB Output is correct
27 Correct 9 ms 14420 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14420 KB Output is correct
30 Correct 8 ms 14420 KB Output is correct
31 Correct 8 ms 14368 KB Output is correct
32 Correct 7 ms 14416 KB Output is correct
33 Correct 8 ms 14420 KB Output is correct
34 Correct 11 ms 14484 KB Output is correct
35 Correct 114 ms 19120 KB Output is correct
36 Correct 146 ms 21524 KB Output is correct
37 Correct 141 ms 21736 KB Output is correct
38 Correct 155 ms 21344 KB Output is correct
39 Correct 302 ms 108488 KB Output is correct
40 Correct 13 ms 15324 KB Output is correct
41 Correct 14 ms 15316 KB Output is correct
42 Correct 291 ms 108492 KB Output is correct
43 Correct 11 ms 14932 KB Output is correct
44 Correct 11 ms 14812 KB Output is correct
45 Correct 305 ms 108576 KB Output is correct
46 Correct 299 ms 108588 KB Output is correct
47 Correct 14 ms 15040 KB Output is correct
48 Correct 15 ms 14932 KB Output is correct
49 Correct 23 ms 15700 KB Output is correct
50 Correct 148 ms 21788 KB Output is correct
51 Correct 14 ms 15060 KB Output is correct
52 Correct 133 ms 20152 KB Output is correct
53 Correct 18 ms 15572 KB Output is correct
54 Correct 137 ms 20784 KB Output is correct
55 Correct 14 ms 15160 KB Output is correct
56 Correct 12 ms 15188 KB Output is correct
57 Correct 13 ms 15328 KB Output is correct
58 Correct 14 ms 15324 KB Output is correct
59 Correct 12 ms 15188 KB Output is correct
60 Correct 114 ms 18604 KB Output is correct
61 Correct 14 ms 15188 KB Output is correct
62 Correct 127 ms 21068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 7 ms 14412 KB Output is correct
5 Correct 8 ms 14348 KB Output is correct
6 Correct 7 ms 14424 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14436 KB Output is correct
13 Correct 8 ms 14468 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14412 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 8 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 7 ms 14416 KB Output is correct
24 Correct 9 ms 14416 KB Output is correct
25 Correct 10 ms 14424 KB Output is correct
26 Correct 9 ms 14368 KB Output is correct
27 Correct 9 ms 14420 KB Output is correct
28 Correct 9 ms 14420 KB Output is correct
29 Correct 8 ms 14420 KB Output is correct
30 Correct 8 ms 14420 KB Output is correct
31 Correct 8 ms 14368 KB Output is correct
32 Correct 7 ms 14416 KB Output is correct
33 Correct 8 ms 14420 KB Output is correct
34 Correct 11 ms 14484 KB Output is correct
35 Correct 114 ms 19120 KB Output is correct
36 Correct 146 ms 21524 KB Output is correct
37 Correct 141 ms 21736 KB Output is correct
38 Correct 155 ms 21344 KB Output is correct
39 Correct 302 ms 108488 KB Output is correct
40 Correct 13 ms 15324 KB Output is correct
41 Correct 14 ms 15316 KB Output is correct
42 Correct 291 ms 108492 KB Output is correct
43 Correct 11 ms 14932 KB Output is correct
44 Correct 11 ms 14812 KB Output is correct
45 Correct 305 ms 108576 KB Output is correct
46 Correct 299 ms 108588 KB Output is correct
47 Correct 14 ms 15040 KB Output is correct
48 Correct 15 ms 14932 KB Output is correct
49 Correct 23 ms 15700 KB Output is correct
50 Correct 148 ms 21788 KB Output is correct
51 Correct 14 ms 15060 KB Output is correct
52 Correct 133 ms 20152 KB Output is correct
53 Correct 18 ms 15572 KB Output is correct
54 Correct 137 ms 20784 KB Output is correct
55 Correct 14 ms 15160 KB Output is correct
56 Correct 12 ms 15188 KB Output is correct
57 Correct 13 ms 15328 KB Output is correct
58 Correct 14 ms 15324 KB Output is correct
59 Correct 12 ms 15188 KB Output is correct
60 Correct 114 ms 18604 KB Output is correct
61 Correct 14 ms 15188 KB Output is correct
62 Correct 127 ms 21068 KB Output is correct
63 Correct 548 ms 66032 KB Output is correct
64 Correct 523 ms 66124 KB Output is correct
65 Correct 519 ms 65620 KB Output is correct
66 Runtime error 3112 ms 1048576 KB Execution killed with signal 9
67 Halted 0 ms 0 KB -