Submission #212120

# Submission time Handle Problem Language Result Execution time Memory
212120 2020-03-22T09:53:24 Z abacaba Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1170 ms 129624 KB
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
 
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
#define int long long

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}
 
template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}
 
ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}
 
ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}
 
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 3e5 + 15;
int n, m, p[N], sz[N];
 
set <int> g[N], gr[N], gr_find[N];

queue <pii> q;

ll ans;

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void upd_ans(int v, int sign) {
	ans += sign * (1ll * sz[v] * (sz[v] - 1) + 1ll * sz[v] * ((int)gr[v].size() - sz[v]));
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		upd_ans(a, -1);
		upd_ans(b, -1);

		// gr_find[b].erase(a);
		// gr_find[a].erase(b);

		// g[a].erase(b);
		// g[b].erase(a);

		if(g[a].size() + gr[a].size() < gr[b].size() + gr[b].size())
			swap(a, b);

		for(int v : g[b]) {
			if(gr_find[a].count(v))
				q.push({v, a});

			gr_find[v].erase(b);
			gr_find[v].insert(a);
		}

		for(int v : gr_find[b]) {
			if(g[a].count(v))
				q.push({v, a});

			g[v].erase(b);
			g[v].insert(a);
		}

		gr[a].insert(gr[b].begin(), gr[b].end());
		gr_find[a].insert(gr_find[b].begin(), gr_find[b].end());
		g[a].insert(g[b].begin(), g[b].end());

		p[b] = a;
		sz[a] += sz[b];

		upd_ans(a, 1);
	}
}
 
main() {
	for(int i = 0; i < N; ++i)
		p[i] = i, sz[i] = 1, gr[i].insert(i);
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;

		if(gr_find[find(u)].count(find(v))) {

 			q.push({find(u), find(v)});

 			while(!q.empty()) {
 				int u = q.front().f, v = q.front().se;
 				q.pop();
 				unio(u, v);
 			}
		}
		else {
			upd_ans(find(v), -1);

			gr_find[find(v)].insert(find(u));
			g[find(u)].insert(find(v));

			gr[find(v)].insert(u);

			upd_ans(find(v), 1);
		}
		cout << ans << endl;
	}
	return 0;
}

Compilation message

joitter2.cpp:126:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# Verdict Execution time Memory Grader output
1 Correct 48 ms 61432 KB Output is correct
2 Correct 47 ms 61432 KB Output is correct
3 Correct 47 ms 61432 KB Output is correct
4 Correct 47 ms 61432 KB Output is correct
5 Correct 47 ms 61432 KB Output is correct
6 Correct 48 ms 61432 KB Output is correct
7 Correct 49 ms 61440 KB Output is correct
8 Correct 48 ms 61432 KB Output is correct
9 Correct 49 ms 61432 KB Output is correct
10 Correct 48 ms 61432 KB Output is correct
11 Correct 48 ms 61440 KB Output is correct
12 Correct 47 ms 61432 KB Output is correct
13 Correct 47 ms 61432 KB Output is correct
14 Correct 47 ms 61432 KB Output is correct
15 Correct 48 ms 61432 KB Output is correct
16 Correct 48 ms 61436 KB Output is correct
17 Correct 47 ms 61432 KB Output is correct
18 Correct 47 ms 61432 KB Output is correct
19 Correct 48 ms 61432 KB Output is correct
20 Correct 48 ms 61432 KB Output is correct
21 Correct 53 ms 61432 KB Output is correct
22 Correct 48 ms 61432 KB Output is correct
23 Correct 48 ms 61440 KB Output is correct
24 Correct 44 ms 61432 KB Output is correct
25 Correct 50 ms 61432 KB Output is correct
26 Correct 47 ms 61432 KB Output is correct
27 Correct 63 ms 61560 KB Output is correct
28 Correct 49 ms 61440 KB Output is correct
29 Correct 48 ms 61440 KB Output is correct
30 Correct 47 ms 61432 KB Output is correct
31 Correct 49 ms 61432 KB Output is correct
32 Correct 48 ms 61432 KB Output is correct
33 Correct 47 ms 61432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 61432 KB Output is correct
2 Correct 47 ms 61432 KB Output is correct
3 Correct 47 ms 61432 KB Output is correct
4 Correct 47 ms 61432 KB Output is correct
5 Correct 47 ms 61432 KB Output is correct
6 Correct 48 ms 61432 KB Output is correct
7 Correct 49 ms 61440 KB Output is correct
8 Correct 48 ms 61432 KB Output is correct
9 Correct 49 ms 61432 KB Output is correct
10 Correct 48 ms 61432 KB Output is correct
11 Correct 48 ms 61440 KB Output is correct
12 Correct 47 ms 61432 KB Output is correct
13 Correct 47 ms 61432 KB Output is correct
14 Correct 47 ms 61432 KB Output is correct
15 Correct 48 ms 61432 KB Output is correct
16 Correct 48 ms 61436 KB Output is correct
17 Correct 47 ms 61432 KB Output is correct
18 Correct 47 ms 61432 KB Output is correct
19 Correct 48 ms 61432 KB Output is correct
20 Correct 48 ms 61432 KB Output is correct
21 Correct 53 ms 61432 KB Output is correct
22 Correct 48 ms 61432 KB Output is correct
23 Correct 48 ms 61440 KB Output is correct
24 Correct 44 ms 61432 KB Output is correct
25 Correct 50 ms 61432 KB Output is correct
26 Correct 47 ms 61432 KB Output is correct
27 Correct 63 ms 61560 KB Output is correct
28 Correct 49 ms 61440 KB Output is correct
29 Correct 48 ms 61440 KB Output is correct
30 Correct 47 ms 61432 KB Output is correct
31 Correct 49 ms 61432 KB Output is correct
32 Correct 48 ms 61432 KB Output is correct
33 Correct 47 ms 61432 KB Output is correct
34 Correct 53 ms 61560 KB Output is correct
35 Correct 137 ms 65016 KB Output is correct
36 Correct 165 ms 67576 KB Output is correct
37 Correct 161 ms 67704 KB Output is correct
38 Correct 158 ms 67192 KB Output is correct
39 Correct 52 ms 61688 KB Output is correct
40 Correct 55 ms 62332 KB Output is correct
41 Correct 54 ms 62200 KB Output is correct
42 Correct 51 ms 61688 KB Output is correct
43 Correct 52 ms 61688 KB Output is correct
44 Correct 53 ms 61688 KB Output is correct
45 Correct 52 ms 61688 KB Output is correct
46 Correct 58 ms 61944 KB Output is correct
47 Correct 53 ms 61944 KB Output is correct
48 Correct 53 ms 61944 KB Output is correct
49 Correct 61 ms 62584 KB Output is correct
50 Correct 162 ms 67704 KB Output is correct
51 Correct 58 ms 62072 KB Output is correct
52 Correct 147 ms 65912 KB Output is correct
53 Correct 67 ms 62456 KB Output is correct
54 Correct 156 ms 66728 KB Output is correct
55 Correct 54 ms 62072 KB Output is correct
56 Correct 54 ms 62072 KB Output is correct
57 Correct 54 ms 62200 KB Output is correct
58 Correct 55 ms 62080 KB Output is correct
59 Correct 54 ms 62328 KB Output is correct
60 Correct 134 ms 64760 KB Output is correct
61 Correct 53 ms 61944 KB Output is correct
62 Correct 165 ms 67064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 61432 KB Output is correct
2 Correct 47 ms 61432 KB Output is correct
3 Correct 47 ms 61432 KB Output is correct
4 Correct 47 ms 61432 KB Output is correct
5 Correct 47 ms 61432 KB Output is correct
6 Correct 48 ms 61432 KB Output is correct
7 Correct 49 ms 61440 KB Output is correct
8 Correct 48 ms 61432 KB Output is correct
9 Correct 49 ms 61432 KB Output is correct
10 Correct 48 ms 61432 KB Output is correct
11 Correct 48 ms 61440 KB Output is correct
12 Correct 47 ms 61432 KB Output is correct
13 Correct 47 ms 61432 KB Output is correct
14 Correct 47 ms 61432 KB Output is correct
15 Correct 48 ms 61432 KB Output is correct
16 Correct 48 ms 61436 KB Output is correct
17 Correct 47 ms 61432 KB Output is correct
18 Correct 47 ms 61432 KB Output is correct
19 Correct 48 ms 61432 KB Output is correct
20 Correct 48 ms 61432 KB Output is correct
21 Correct 53 ms 61432 KB Output is correct
22 Correct 48 ms 61432 KB Output is correct
23 Correct 48 ms 61440 KB Output is correct
24 Correct 44 ms 61432 KB Output is correct
25 Correct 50 ms 61432 KB Output is correct
26 Correct 47 ms 61432 KB Output is correct
27 Correct 63 ms 61560 KB Output is correct
28 Correct 49 ms 61440 KB Output is correct
29 Correct 48 ms 61440 KB Output is correct
30 Correct 47 ms 61432 KB Output is correct
31 Correct 49 ms 61432 KB Output is correct
32 Correct 48 ms 61432 KB Output is correct
33 Correct 47 ms 61432 KB Output is correct
34 Correct 53 ms 61560 KB Output is correct
35 Correct 137 ms 65016 KB Output is correct
36 Correct 165 ms 67576 KB Output is correct
37 Correct 161 ms 67704 KB Output is correct
38 Correct 158 ms 67192 KB Output is correct
39 Correct 52 ms 61688 KB Output is correct
40 Correct 55 ms 62332 KB Output is correct
41 Correct 54 ms 62200 KB Output is correct
42 Correct 51 ms 61688 KB Output is correct
43 Correct 52 ms 61688 KB Output is correct
44 Correct 53 ms 61688 KB Output is correct
45 Correct 52 ms 61688 KB Output is correct
46 Correct 58 ms 61944 KB Output is correct
47 Correct 53 ms 61944 KB Output is correct
48 Correct 53 ms 61944 KB Output is correct
49 Correct 61 ms 62584 KB Output is correct
50 Correct 162 ms 67704 KB Output is correct
51 Correct 58 ms 62072 KB Output is correct
52 Correct 147 ms 65912 KB Output is correct
53 Correct 67 ms 62456 KB Output is correct
54 Correct 156 ms 66728 KB Output is correct
55 Correct 54 ms 62072 KB Output is correct
56 Correct 54 ms 62072 KB Output is correct
57 Correct 54 ms 62200 KB Output is correct
58 Correct 55 ms 62080 KB Output is correct
59 Correct 54 ms 62328 KB Output is correct
60 Correct 134 ms 64760 KB Output is correct
61 Correct 53 ms 61944 KB Output is correct
62 Correct 165 ms 67064 KB Output is correct
63 Correct 575 ms 105608 KB Output is correct
64 Correct 572 ms 105848 KB Output is correct
65 Correct 588 ms 105848 KB Output is correct
66 Correct 295 ms 77688 KB Output is correct
67 Correct 639 ms 113576 KB Output is correct
68 Correct 288 ms 77560 KB Output is correct
69 Correct 445 ms 75512 KB Output is correct
70 Correct 331 ms 77688 KB Output is correct
71 Correct 321 ms 77560 KB Output is correct
72 Correct 578 ms 90936 KB Output is correct
73 Correct 584 ms 93048 KB Output is correct
74 Correct 1170 ms 114424 KB Output is correct
75 Correct 717 ms 82144 KB Output is correct
76 Correct 876 ms 97272 KB Output is correct
77 Correct 892 ms 97528 KB Output is correct
78 Correct 302 ms 79484 KB Output is correct
79 Correct 491 ms 84088 KB Output is correct
80 Correct 321 ms 78320 KB Output is correct
81 Correct 472 ms 80248 KB Output is correct
82 Correct 897 ms 97244 KB Output is correct
83 Correct 884 ms 97144 KB Output is correct
84 Correct 724 ms 95864 KB Output is correct
85 Correct 715 ms 95864 KB Output is correct
86 Correct 574 ms 128356 KB Output is correct
87 Correct 602 ms 129624 KB Output is correct
88 Correct 614 ms 92536 KB Output is correct
89 Correct 883 ms 96108 KB Output is correct