Submission #223487

# Submission time Handle Problem Language Result Execution time Memory
223487 2020-04-15T10:08:26 Z quocnguyen1012 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
980 ms 44152 KB
#include <bits/stdc++.h>
using namespace std;

#define ar array

const int mxN=1e5;
int n, m;
queue<ar<int, 2>> qu;
long long ans;

int p[mxN], s[mxN];
set<ar<int, 2>> to[mxN], from[mxN];

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

void cr(int u, int v) {
	auto it=to[u].lower_bound(ar<int, 2>{v});
	if(it!=to[u].end()&&(*it)[0]==v)
		qu.push({u, v});
}

int e1(set<ar<int, 2>> &s, int x) {
	auto it=s.lower_bound(ar<int, 2>{x});
	int r=0;
	while(it!=s.end()&&(*it)[0]==x)
		it=s.erase(it), ++r;
	return r;
}

void join(int u, int v) {
	if((u=find(u))==(v=find(v)))
		return;
	//remove self edges?
	e1(to[u], v);
	ans-=e1(from[u], v)*s[u];
	e1(to[v], u);
	ans-=e1(from[v], u)*s[v];
	//u to v
	if(to[u].size()+from[u].size()>to[v].size()+from[v].size())
		swap(u, v);
	//update ans
	ans+=(long long)2*s[u]*s[v];
	ans+=(long long)s[u]*from[v].size();
	ans+=(long long)s[v]*from[u].size();
	for(ar<int, 2> e : from[u])
		if(from[v].find(e)!=from[v].end())
			ans-=s[u]+s[v];
	//merge sets
	for(ar<int, 2> e : from[u])
		from[v].insert(e);
	for(ar<int, 2> e : to[u])
		to[v].insert(e);
	p[u]=v;
	s[v]+=s[u];
	//update references to u
	for(ar<int, 2> e : to[u]) {
		auto it=from[e[0]].lower_bound(ar<int, 2>{u});
		while(it!=from[e[0]].end()&&(*it)[0]==u) {
			from[e[0]].insert({v, (*it)[1]});
			it=from[e[0]].erase(it);
		}
	}
	for(ar<int, 2> e : from[u]) {
		auto it=to[e[0]].lower_bound(ar<int, 2>{u});
		while(it!=to[e[0]].end()&&(*it)[0]==u) {
			to[e[0]].insert({v, (*it)[1]});
			it=to[e[0]].erase(it);
		}
	}
	//check for new merges
	for(ar<int, 2> e : to[u])
		cr(e[0], v);
	for(ar<int, 2> e : from[u])
		cr(v, e[0]);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	#ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
	cin >> n >> m;
	iota(p, p+n, 0);
	fill(s, s+n, 1);
	for(int a, b; m--; ) {
		cin >> a >> b, --a, --b;
		if(find(a)!=find(b)) {
			//add edge
			to[find(a)].insert({find(b), b});
			ans+=from[find(b)].insert({find(a), a}).second*s[find(b)];
			assert(from[find(b)].insert({find(a), a}).second == 0);
			//check relation
			cr(find(b), find(a));
			while(qu.size()) {
				ar<int, 2> u=qu.front();
				qu.pop();
				join(u[0], u[1]);
			}
		}
		cout << ans << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 11 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 11 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9856 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 10 ms 9856 KB Output is correct
29 Correct 29 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9728 KB Output is correct
32 Correct 11 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 11 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 11 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9856 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 10 ms 9856 KB Output is correct
29 Correct 29 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9728 KB Output is correct
32 Correct 11 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 97 ms 15096 KB Output is correct
36 Correct 126 ms 17272 KB Output is correct
37 Correct 127 ms 17272 KB Output is correct
38 Correct 129 ms 17016 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 13 ms 9984 KB Output is correct
41 Correct 13 ms 9984 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 14 ms 9984 KB Output is correct
44 Correct 14 ms 9984 KB Output is correct
45 Correct 13 ms 9856 KB Output is correct
46 Correct 13 ms 9856 KB Output is correct
47 Correct 13 ms 9984 KB Output is correct
48 Correct 14 ms 9984 KB Output is correct
49 Correct 21 ms 10624 KB Output is correct
50 Correct 125 ms 17400 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 113 ms 16120 KB Output is correct
53 Correct 20 ms 10496 KB Output is correct
54 Correct 116 ms 16632 KB Output is correct
55 Correct 15 ms 10240 KB Output is correct
56 Correct 15 ms 10240 KB Output is correct
57 Correct 15 ms 10240 KB Output is correct
58 Correct 16 ms 10240 KB Output is correct
59 Correct 13 ms 9856 KB Output is correct
60 Correct 93 ms 14712 KB Output is correct
61 Correct 15 ms 9984 KB Output is correct
62 Correct 124 ms 16888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 11 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 11 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 11 ms 9728 KB Output is correct
21 Correct 11 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9856 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 10 ms 9856 KB Output is correct
29 Correct 29 ms 9728 KB Output is correct
30 Correct 10 ms 9728 KB Output is correct
31 Correct 11 ms 9728 KB Output is correct
32 Correct 11 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 97 ms 15096 KB Output is correct
36 Correct 126 ms 17272 KB Output is correct
37 Correct 127 ms 17272 KB Output is correct
38 Correct 129 ms 17016 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 13 ms 9984 KB Output is correct
41 Correct 13 ms 9984 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 14 ms 9984 KB Output is correct
44 Correct 14 ms 9984 KB Output is correct
45 Correct 13 ms 9856 KB Output is correct
46 Correct 13 ms 9856 KB Output is correct
47 Correct 13 ms 9984 KB Output is correct
48 Correct 14 ms 9984 KB Output is correct
49 Correct 21 ms 10624 KB Output is correct
50 Correct 125 ms 17400 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 113 ms 16120 KB Output is correct
53 Correct 20 ms 10496 KB Output is correct
54 Correct 116 ms 16632 KB Output is correct
55 Correct 15 ms 10240 KB Output is correct
56 Correct 15 ms 10240 KB Output is correct
57 Correct 15 ms 10240 KB Output is correct
58 Correct 16 ms 10240 KB Output is correct
59 Correct 13 ms 9856 KB Output is correct
60 Correct 93 ms 14712 KB Output is correct
61 Correct 15 ms 9984 KB Output is correct
62 Correct 124 ms 16888 KB Output is correct
63 Correct 502 ms 44152 KB Output is correct
64 Correct 524 ms 44152 KB Output is correct
65 Correct 511 ms 44152 KB Output is correct
66 Correct 139 ms 14968 KB Output is correct
67 Correct 266 ms 19192 KB Output is correct
68 Correct 143 ms 14968 KB Output is correct
69 Correct 346 ms 19576 KB Output is correct
70 Correct 162 ms 14968 KB Output is correct
71 Correct 154 ms 14968 KB Output is correct
72 Correct 318 ms 19704 KB Output is correct
73 Correct 303 ms 19576 KB Output is correct
74 Correct 980 ms 38264 KB Output is correct
75 Correct 532 ms 25080 KB Output is correct
76 Correct 705 ms 31228 KB Output is correct
77 Correct 664 ms 31224 KB Output is correct
78 Correct 195 ms 19576 KB Output is correct
79 Correct 363 ms 23236 KB Output is correct
80 Correct 191 ms 19704 KB Output is correct
81 Correct 338 ms 23104 KB Output is correct
82 Correct 786 ms 33912 KB Output is correct
83 Correct 804 ms 33912 KB Output is correct
84 Correct 555 ms 33144 KB Output is correct
85 Correct 553 ms 33144 KB Output is correct
86 Correct 159 ms 14072 KB Output is correct
87 Correct 185 ms 16376 KB Output is correct
88 Correct 315 ms 19704 KB Output is correct
89 Correct 617 ms 29944 KB Output is correct