Submission #269766

# Submission time Handle Problem Language Result Execution time Memory
269766 2020-08-17T09:57:43 Z sjimed Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1297 ms 64504 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define em emplace
#define eb emplace_back
#define mp make_pair

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;
const ll Mod = 1e9 + 7;

int n, m;
int a, b;
int p[100010];
ll sz[100010];
set<int> in[100010], out[100010], c[100010];
ll ans;
vector<pii> q;

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

void Union(int a, int b) {
	a = Find(a);
	b = Find(b);
	if(a == b) return;

	if(sz[a] < sz[b]) swap(a, b);

	ans -= sz[a] * (sz[a]-1) + sz[b] * (sz[b] - 1);
	ans -= sz[a] * in[a].size();
	ans -= sz[b] * in[b].size();

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

	ans += sz[a] * (sz[a] - 1);

	for(auto i : out[b]) {
		if(Find(i) == a) continue;

		out[a].insert(Find(i));
		
		if(out[Find(i)].find(a) != out[Find(i)].end()) {
			q.eb(a, Find(i));
		}
	}
	out[b].clear();

	for(auto i : in[b]) {
		if(Find(i) == a) continue;

		in[a].insert(i);
		out[Find(i)].insert(a);

		if(out[a].find(Find(i)) != out[a].end()) {
			q.eb(a, Find(i));
		}
	}
	in[b].clear();

	vector<int> v;

	for(auto i : c[b]) {
		if(in[a].find(i) != in[a].end()) v.eb(i);

		c[a].insert(i);
	}

	for(auto i : v) {
		in[a].erase(i);
	}

	ans += sz[a] * in[a].size();
}

int main() {
	fast;

	cin >> n >> m;

	for(int i=1; i<=n; i++)
		p[i] = i, sz[i] = 1, c[i].insert(i);

	for(int i=1; i<=m; i++) {
		cin >> a >> b;

		if(Find(a) == Find(b) || in[Find(b)].find(a) != in[Find(b)].end()) {
			cout << ans << "\n";
			continue;
		}

		in[Find(b)].insert(a);
		out[Find(a)].insert(Find(b));
		ans += sz[Find(b)];

		if(out[Find(b)].find(Find(a)) != out[Find(b)].end()) {
			q.eb(a, b);
		}

		while(q.size()) {
			auto x = q.back();
			q.pop_back();

			Union(x.fi, x.se);
		}

		cout << ans << "\n";
	}

}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 12 ms 14592 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 11 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 10 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 11 ms 14464 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 12 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 12 ms 14592 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 11 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 10 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 11 ms 14464 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 12 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14592 KB Output is correct
35 Correct 97 ms 17668 KB Output is correct
36 Correct 124 ms 19400 KB Output is correct
37 Correct 128 ms 19544 KB Output is correct
38 Correct 115 ms 19192 KB Output is correct
39 Correct 13 ms 14720 KB Output is correct
40 Correct 14 ms 14976 KB Output is correct
41 Correct 14 ms 14976 KB Output is correct
42 Correct 13 ms 14720 KB Output is correct
43 Correct 15 ms 14720 KB Output is correct
44 Correct 15 ms 14720 KB Output is correct
45 Correct 13 ms 14720 KB Output is correct
46 Correct 14 ms 14720 KB Output is correct
47 Correct 17 ms 14848 KB Output is correct
48 Correct 14 ms 14848 KB Output is correct
49 Correct 20 ms 15232 KB Output is correct
50 Correct 117 ms 19568 KB Output is correct
51 Correct 17 ms 14848 KB Output is correct
52 Correct 103 ms 18296 KB Output is correct
53 Correct 21 ms 15232 KB Output is correct
54 Correct 107 ms 18936 KB Output is correct
55 Correct 16 ms 14976 KB Output is correct
56 Correct 15 ms 14976 KB Output is correct
57 Correct 15 ms 14952 KB Output is correct
58 Correct 15 ms 14976 KB Output is correct
59 Correct 14 ms 15104 KB Output is correct
60 Correct 107 ms 17616 KB Output is correct
61 Correct 19 ms 14848 KB Output is correct
62 Correct 110 ms 19196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14464 KB Output is correct
4 Correct 10 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 10 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14464 KB Output is correct
9 Correct 12 ms 14592 KB Output is correct
10 Correct 10 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 10 ms 14464 KB Output is correct
13 Correct 10 ms 14464 KB Output is correct
14 Correct 10 ms 14464 KB Output is correct
15 Correct 10 ms 14464 KB Output is correct
16 Correct 11 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 12 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 10 ms 14496 KB Output is correct
23 Correct 10 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 10 ms 14464 KB Output is correct
26 Correct 10 ms 14464 KB Output is correct
27 Correct 10 ms 14464 KB Output is correct
28 Correct 10 ms 14464 KB Output is correct
29 Correct 10 ms 14464 KB Output is correct
30 Correct 11 ms 14464 KB Output is correct
31 Correct 11 ms 14464 KB Output is correct
32 Correct 12 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14592 KB Output is correct
35 Correct 97 ms 17668 KB Output is correct
36 Correct 124 ms 19400 KB Output is correct
37 Correct 128 ms 19544 KB Output is correct
38 Correct 115 ms 19192 KB Output is correct
39 Correct 13 ms 14720 KB Output is correct
40 Correct 14 ms 14976 KB Output is correct
41 Correct 14 ms 14976 KB Output is correct
42 Correct 13 ms 14720 KB Output is correct
43 Correct 15 ms 14720 KB Output is correct
44 Correct 15 ms 14720 KB Output is correct
45 Correct 13 ms 14720 KB Output is correct
46 Correct 14 ms 14720 KB Output is correct
47 Correct 17 ms 14848 KB Output is correct
48 Correct 14 ms 14848 KB Output is correct
49 Correct 20 ms 15232 KB Output is correct
50 Correct 117 ms 19568 KB Output is correct
51 Correct 17 ms 14848 KB Output is correct
52 Correct 103 ms 18296 KB Output is correct
53 Correct 21 ms 15232 KB Output is correct
54 Correct 107 ms 18936 KB Output is correct
55 Correct 16 ms 14976 KB Output is correct
56 Correct 15 ms 14976 KB Output is correct
57 Correct 15 ms 14952 KB Output is correct
58 Correct 15 ms 14976 KB Output is correct
59 Correct 14 ms 15104 KB Output is correct
60 Correct 107 ms 17616 KB Output is correct
61 Correct 19 ms 14848 KB Output is correct
62 Correct 110 ms 19196 KB Output is correct
63 Correct 504 ms 50428 KB Output is correct
64 Correct 486 ms 50472 KB Output is correct
65 Correct 467 ms 50552 KB Output is correct
66 Correct 290 ms 31864 KB Output is correct
67 Correct 561 ms 49400 KB Output is correct
68 Correct 333 ms 31812 KB Output is correct
69 Correct 588 ms 33136 KB Output is correct
70 Correct 313 ms 34168 KB Output is correct
71 Correct 335 ms 34172 KB Output is correct
72 Correct 612 ms 36984 KB Output is correct
73 Correct 554 ms 38136 KB Output is correct
74 Correct 1116 ms 48368 KB Output is correct
75 Correct 660 ms 37500 KB Output is correct
76 Correct 846 ms 43000 KB Output is correct
77 Correct 922 ms 43000 KB Output is correct
78 Correct 258 ms 33100 KB Output is correct
79 Correct 504 ms 37520 KB Output is correct
80 Correct 327 ms 33016 KB Output is correct
81 Correct 631 ms 36856 KB Output is correct
82 Correct 788 ms 42744 KB Output is correct
83 Correct 833 ms 42848 KB Output is correct
84 Correct 705 ms 42744 KB Output is correct
85 Correct 784 ms 42752 KB Output is correct
86 Correct 474 ms 62072 KB Output is correct
87 Correct 504 ms 64504 KB Output is correct
88 Correct 614 ms 37368 KB Output is correct
89 Correct 1297 ms 41512 KB Output is correct