답안 #219688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219688 2020-04-06T00:48:32 Z tmwilliamlin168 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
917 ms 40976 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);
	
	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)];
			//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";
	}
}
# 결과 실행 시간 메모리 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 12 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 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 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 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 12 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 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 10 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 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 12 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 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 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 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 12 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 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 10 ms 9856 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 106 ms 13472 KB Output is correct
36 Correct 126 ms 14844 KB Output is correct
37 Correct 123 ms 14968 KB Output is correct
38 Correct 131 ms 14840 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 13 ms 9856 KB Output is correct
41 Correct 13 ms 9856 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 13 ms 9856 KB Output is correct
44 Correct 13 ms 9984 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 12 ms 9856 KB Output is correct
47 Correct 13 ms 9984 KB Output is correct
48 Correct 13 ms 9984 KB Output is correct
49 Correct 22 ms 10496 KB Output is correct
50 Correct 126 ms 14968 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 107 ms 13792 KB Output is correct
53 Correct 19 ms 10496 KB Output is correct
54 Correct 115 ms 14328 KB Output is correct
55 Correct 15 ms 10240 KB Output is correct
56 Correct 16 ms 10240 KB Output is correct
57 Correct 14 ms 10240 KB Output is correct
58 Correct 15 ms 10240 KB Output is correct
59 Correct 12 ms 9856 KB Output is correct
60 Correct 92 ms 12408 KB Output is correct
61 Correct 14 ms 9984 KB Output is correct
62 Correct 119 ms 14456 KB Output is correct
# 결과 실행 시간 메모리 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 12 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 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 11 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 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 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 12 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 11 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 10 ms 9856 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 106 ms 13472 KB Output is correct
36 Correct 126 ms 14844 KB Output is correct
37 Correct 123 ms 14968 KB Output is correct
38 Correct 131 ms 14840 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 13 ms 9856 KB Output is correct
41 Correct 13 ms 9856 KB Output is correct
42 Correct 12 ms 9856 KB Output is correct
43 Correct 13 ms 9856 KB Output is correct
44 Correct 13 ms 9984 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 12 ms 9856 KB Output is correct
47 Correct 13 ms 9984 KB Output is correct
48 Correct 13 ms 9984 KB Output is correct
49 Correct 22 ms 10496 KB Output is correct
50 Correct 126 ms 14968 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 107 ms 13792 KB Output is correct
53 Correct 19 ms 10496 KB Output is correct
54 Correct 115 ms 14328 KB Output is correct
55 Correct 15 ms 10240 KB Output is correct
56 Correct 16 ms 10240 KB Output is correct
57 Correct 14 ms 10240 KB Output is correct
58 Correct 15 ms 10240 KB Output is correct
59 Correct 12 ms 9856 KB Output is correct
60 Correct 92 ms 12408 KB Output is correct
61 Correct 14 ms 9984 KB Output is correct
62 Correct 119 ms 14456 KB Output is correct
63 Correct 453 ms 40976 KB Output is correct
64 Correct 453 ms 40964 KB Output is correct
65 Correct 446 ms 40956 KB Output is correct
66 Correct 135 ms 12924 KB Output is correct
67 Correct 233 ms 17276 KB Output is correct
68 Correct 131 ms 12920 KB Output is correct
69 Correct 309 ms 17528 KB Output is correct
70 Correct 144 ms 12920 KB Output is correct
71 Correct 142 ms 12920 KB Output is correct
72 Correct 279 ms 17504 KB Output is correct
73 Correct 287 ms 17656 KB Output is correct
74 Correct 917 ms 34808 KB Output is correct
75 Correct 474 ms 21752 KB Output is correct
76 Correct 620 ms 27896 KB Output is correct
77 Correct 618 ms 27932 KB Output is correct
78 Correct 178 ms 18040 KB Output is correct
79 Correct 318 ms 19832 KB Output is correct
80 Correct 173 ms 18040 KB Output is correct
81 Correct 293 ms 19832 KB Output is correct
82 Correct 748 ms 31992 KB Output is correct
83 Correct 715 ms 31864 KB Output is correct
84 Correct 511 ms 30968 KB Output is correct
85 Correct 514 ms 30968 KB Output is correct
86 Correct 145 ms 12024 KB Output is correct
87 Correct 173 ms 13048 KB Output is correct
88 Correct 277 ms 17404 KB Output is correct
89 Correct 588 ms 26756 KB Output is correct