답안 #444099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444099 2021-07-13T06:03:21 Z kig9981 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
807 ms 52260 KB
#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

set<pair<int,int>> adj[100000], radj[100000];
int color[100000], cnt[100000];
long long ans;

int find_(int a) {return color[a]=a==color[a] ? a:find_(color[a]);}

void erase_value(int a, int b, set<pair<int,int>> *adj)
{
	for(;;) {
		auto it=adj[a].lower_bound({b,0});
		if(it==adj[a].end() || it->first!=b) break;
		adj[a].erase(it);
	}
}

void union_(int a, int b)
{
	vector<int> T;
	a=find_(a); b=find_(b);
	if(a==b) return;
	if(adj[a].size()+radj[a].size()<adj[b].size()+radj[b].size()) swap(a,b);
	ans-=cnt[a]*(adj[a].size()+cnt[a]-1LL)+cnt[b]*(adj[b].size()+cnt[b]-1LL);
	color[b]=a;
	cnt[a]+=cnt[b];
	erase_value(a,b,adj); erase_value(b,a,adj);
	erase_value(a,b,radj); erase_value(b,a,radj);
	for(auto n: adj[b]) {
		radj[n.first].erase({b,n.second});
		radj[n.first].emplace(a,n.second);
		adj[a].insert(n);
		auto it=adj[n.first].lower_bound({a,0});
		if(it!=adj[n.first].end() && it->first==a) T.push_back(n.first);
	}
	for(auto n: radj[b]) {
		adj[n.first].erase({b,n.second});
		adj[n.first].emplace(a,n.second);
		radj[a].insert(n);
		auto it=adj[a].lower_bound({n.first,0});
		if(it!=adj[a].end() && it->first==n.first) T.push_back(n.first);
	}
	ans+=cnt[a]*(adj[a].size()+cnt[a]-1LL);
	for(auto t: T) union_(a,t);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, M;
	cin>>N>>M;
	for(int i=0;i<N;i++) cnt[color[i]=i]=1;
	while(M--) {
		int a, b, x, y;
		cin>>a>>b;
		if((x=find_(--a))==(y=find_(--b)) || adj[y].find({x,a})!=adj[y].end()) {
			cout<<ans<<'\n';
			continue;
		}
		adj[y].emplace(x,a);
		radj[x].emplace(y,a);
		ans+=cnt[y];
		auto it=adj[x].lower_bound({y,0});
		if(it!=adj[x].end() && it->first==y) union_(a,b);
		cout<<ans<<'\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9628 KB Output is correct
6 Correct 7 ms 9704 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 8 ms 9636 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9712 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 6 ms 9676 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9708 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 7 ms 9676 KB Output is correct
26 Correct 6 ms 9676 KB Output is correct
27 Correct 7 ms 9716 KB Output is correct
28 Correct 6 ms 9716 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 6 ms 9712 KB Output is correct
31 Correct 7 ms 9688 KB Output is correct
32 Correct 6 ms 9712 KB Output is correct
33 Correct 7 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9628 KB Output is correct
6 Correct 7 ms 9704 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 8 ms 9636 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9712 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 6 ms 9676 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9708 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 7 ms 9676 KB Output is correct
26 Correct 6 ms 9676 KB Output is correct
27 Correct 7 ms 9716 KB Output is correct
28 Correct 6 ms 9716 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 6 ms 9712 KB Output is correct
31 Correct 7 ms 9688 KB Output is correct
32 Correct 6 ms 9712 KB Output is correct
33 Correct 7 ms 9676 KB Output is correct
34 Correct 9 ms 9804 KB Output is correct
35 Correct 99 ms 15160 KB Output is correct
36 Correct 130 ms 17396 KB Output is correct
37 Correct 132 ms 17556 KB Output is correct
38 Correct 125 ms 17144 KB Output is correct
39 Correct 8 ms 9676 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 10 ms 9804 KB Output is correct
42 Correct 9 ms 9676 KB Output is correct
43 Correct 10 ms 9848 KB Output is correct
44 Correct 9 ms 9856 KB Output is correct
45 Correct 8 ms 9676 KB Output is correct
46 Correct 8 ms 9676 KB Output is correct
47 Correct 9 ms 9804 KB Output is correct
48 Correct 10 ms 9804 KB Output is correct
49 Correct 17 ms 10572 KB Output is correct
50 Correct 134 ms 17536 KB Output is correct
51 Correct 15 ms 10060 KB Output is correct
52 Correct 121 ms 16320 KB Output is correct
53 Correct 15 ms 10576 KB Output is correct
54 Correct 120 ms 16964 KB Output is correct
55 Correct 13 ms 10108 KB Output is correct
56 Correct 11 ms 10112 KB Output is correct
57 Correct 11 ms 10600 KB Output is correct
58 Correct 11 ms 10484 KB Output is correct
59 Correct 9 ms 9676 KB Output is correct
60 Correct 97 ms 14644 KB Output is correct
61 Correct 11 ms 9932 KB Output is correct
62 Correct 123 ms 17116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9628 KB Output is correct
6 Correct 7 ms 9704 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 7 ms 9724 KB Output is correct
9 Correct 8 ms 9636 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9712 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 6 ms 9676 KB Output is correct
16 Correct 7 ms 9708 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9708 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 7 ms 9676 KB Output is correct
26 Correct 6 ms 9676 KB Output is correct
27 Correct 7 ms 9716 KB Output is correct
28 Correct 6 ms 9716 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 6 ms 9712 KB Output is correct
31 Correct 7 ms 9688 KB Output is correct
32 Correct 6 ms 9712 KB Output is correct
33 Correct 7 ms 9676 KB Output is correct
34 Correct 9 ms 9804 KB Output is correct
35 Correct 99 ms 15160 KB Output is correct
36 Correct 130 ms 17396 KB Output is correct
37 Correct 132 ms 17556 KB Output is correct
38 Correct 125 ms 17144 KB Output is correct
39 Correct 8 ms 9676 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 10 ms 9804 KB Output is correct
42 Correct 9 ms 9676 KB Output is correct
43 Correct 10 ms 9848 KB Output is correct
44 Correct 9 ms 9856 KB Output is correct
45 Correct 8 ms 9676 KB Output is correct
46 Correct 8 ms 9676 KB Output is correct
47 Correct 9 ms 9804 KB Output is correct
48 Correct 10 ms 9804 KB Output is correct
49 Correct 17 ms 10572 KB Output is correct
50 Correct 134 ms 17536 KB Output is correct
51 Correct 15 ms 10060 KB Output is correct
52 Correct 121 ms 16320 KB Output is correct
53 Correct 15 ms 10576 KB Output is correct
54 Correct 120 ms 16964 KB Output is correct
55 Correct 13 ms 10108 KB Output is correct
56 Correct 11 ms 10112 KB Output is correct
57 Correct 11 ms 10600 KB Output is correct
58 Correct 11 ms 10484 KB Output is correct
59 Correct 9 ms 9676 KB Output is correct
60 Correct 97 ms 14644 KB Output is correct
61 Correct 11 ms 9932 KB Output is correct
62 Correct 123 ms 17116 KB Output is correct
63 Correct 471 ms 44032 KB Output is correct
64 Correct 442 ms 44108 KB Output is correct
65 Correct 460 ms 44056 KB Output is correct
66 Correct 115 ms 14888 KB Output is correct
67 Correct 212 ms 19148 KB Output is correct
68 Correct 111 ms 14916 KB Output is correct
69 Correct 321 ms 19628 KB Output is correct
70 Correct 159 ms 14780 KB Output is correct
71 Correct 136 ms 14776 KB Output is correct
72 Correct 268 ms 19464 KB Output is correct
73 Correct 243 ms 19500 KB Output is correct
74 Correct 807 ms 39492 KB Output is correct
75 Correct 490 ms 24896 KB Output is correct
76 Correct 553 ms 31812 KB Output is correct
77 Correct 594 ms 31872 KB Output is correct
78 Correct 176 ms 19524 KB Output is correct
79 Correct 294 ms 23336 KB Output is correct
80 Correct 193 ms 19552 KB Output is correct
81 Correct 284 ms 22756 KB Output is correct
82 Correct 544 ms 33392 KB Output is correct
83 Correct 568 ms 33376 KB Output is correct
84 Correct 525 ms 52260 KB Output is correct
85 Correct 518 ms 52196 KB Output is correct
86 Correct 117 ms 14016 KB Output is correct
87 Correct 155 ms 16308 KB Output is correct
88 Correct 249 ms 19524 KB Output is correct
89 Correct 566 ms 29956 KB Output is correct