답안 #219687

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219687 2020-04-06T00:46:14 Z tmwilliamlin168 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
921 ms 44280 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();
				//merge
				join(u[0], u[1]);
			}
		}
		cout << ans << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 11 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 9600 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 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 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 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 10 ms 9856 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9856 KB Output is correct
26 Correct 12 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 10 ms 9728 KB Output is correct
30 Correct 12 ms 9728 KB Output is correct
31 Correct 13 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 11 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 9600 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 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 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 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 10 ms 9856 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9856 KB Output is correct
26 Correct 12 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 10 ms 9728 KB Output is correct
30 Correct 12 ms 9728 KB Output is correct
31 Correct 13 ms 9728 KB Output is correct
32 Correct 10 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 108 ms 15096 KB Output is correct
36 Correct 125 ms 17144 KB Output is correct
37 Correct 127 ms 17272 KB Output is correct
38 Correct 121 ms 17016 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 14 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 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 21 ms 10496 KB Output is correct
50 Correct 127 ms 17272 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 111 ms 16120 KB Output is correct
53 Correct 20 ms 10496 KB Output is correct
54 Correct 123 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 14 ms 10240 KB Output is correct
59 Correct 13 ms 9856 KB Output is correct
60 Correct 98 ms 15004 KB Output is correct
61 Correct 14 ms 9984 KB Output is correct
62 Correct 119 ms 16888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 11 ms 9728 KB Output is correct
3 Correct 11 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 9600 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 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 10 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 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 10 ms 9856 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9856 KB Output is correct
26 Correct 12 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 10 ms 9728 KB Output is correct
29 Correct 10 ms 9728 KB Output is correct
30 Correct 12 ms 9728 KB Output is correct
31 Correct 13 ms 9728 KB Output is correct
32 Correct 10 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 108 ms 15096 KB Output is correct
36 Correct 125 ms 17144 KB Output is correct
37 Correct 127 ms 17272 KB Output is correct
38 Correct 121 ms 17016 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 14 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 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 21 ms 10496 KB Output is correct
50 Correct 127 ms 17272 KB Output is correct
51 Correct 17 ms 10112 KB Output is correct
52 Correct 111 ms 16120 KB Output is correct
53 Correct 20 ms 10496 KB Output is correct
54 Correct 123 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 14 ms 10240 KB Output is correct
59 Correct 13 ms 9856 KB Output is correct
60 Correct 98 ms 15004 KB Output is correct
61 Correct 14 ms 9984 KB Output is correct
62 Correct 119 ms 16888 KB Output is correct
63 Correct 479 ms 44280 KB Output is correct
64 Correct 462 ms 44164 KB Output is correct
65 Correct 456 ms 44092 KB Output is correct
66 Correct 133 ms 14968 KB Output is correct
67 Correct 245 ms 19192 KB Output is correct
68 Correct 136 ms 14968 KB Output is correct
69 Correct 321 ms 19576 KB Output is correct
70 Correct 139 ms 14968 KB Output is correct
71 Correct 148 ms 14968 KB Output is correct
72 Correct 277 ms 19576 KB Output is correct
73 Correct 277 ms 19576 KB Output is correct
74 Correct 921 ms 38008 KB Output is correct
75 Correct 484 ms 24952 KB Output is correct
76 Correct 637 ms 31224 KB Output is correct
77 Correct 608 ms 31224 KB Output is correct
78 Correct 183 ms 19576 KB Output is correct
79 Correct 339 ms 23104 KB Output is correct
80 Correct 169 ms 19704 KB Output is correct
81 Correct 303 ms 23328 KB Output is correct
82 Correct 717 ms 33816 KB Output is correct
83 Correct 710 ms 33784 KB Output is correct
84 Correct 521 ms 33144 KB Output is correct
85 Correct 521 ms 33144 KB Output is correct
86 Correct 146 ms 14072 KB Output is correct
87 Correct 174 ms 16376 KB Output is correct
88 Correct 275 ms 19704 KB Output is correct
89 Correct 596 ms 30072 KB Output is correct