Submission #212105

# Submission time Handle Problem Language Result Execution time Memory
212105 2020-03-22T09:23:51 Z kshitij_sodani Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
651 ms 53508 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
ll par[100001];
ll size2[100001];
set<pair<ll,ll>> inc[100001];
set<pair<ll,ll>> out[100001];
ll find(ll no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
set<ll> wait;
ll ans=0;
ll count(ll aa){
	return (ll)size2[aa]*(size2[aa]-1)+(ll)inc[aa].size()*size2[aa];
}
void merge(ll aa,ll bb){
	ans-=(count(aa)+count(bb));
	if((inc[aa].size()+out[aa].size())<(inc[bb].size()+out[bb].size())){
		swap(aa,bb);
	}

	for(auto no:inc[bb]){
		if(no.a==aa){
			auto it=out[aa].find({bb,no.b});
			out[aa].erase(it);
			continue;
		}
		auto it=inc[no.a].lower_bound({aa,0});
		if(it!=inc[no.a].end() and it->a==aa){
			wait.insert(no.a);
		}
		out[no.a].insert({aa,no.b});
		auto tt=out[no.a].find({bb,no.b});
		out[no.a].erase(tt);
		inc[aa].insert(no);
	}
	for(auto no:out[bb]){
		if(no.a==aa){
			auto it=inc[aa].find({bb,no.b});
			inc[aa].erase(it);
			continue;
		}
		auto it=out[no.a].lower_bound({aa,0});
		if(it!=out[no.a].end() and it->a==aa){
			wait.insert(no.a);
		}
		inc[no.a].insert({aa,no.b});
		auto tt=inc[no.a].find({bb,no.b});
		inc[no.a].erase(tt);
		out[aa].insert(no);
	}
	inc[bb].clear();
	out[bb].clear();
	par[bb]=aa;
	size2[aa]+=size2[bb];
	ans+=count(aa);
	//cout<<":: "<<ans<<" "<<aa<<" "<<bb<<" "<<inc[aa].size()<<endl;
	if(wait.size()>0){
		ll xx=*(wait.begin());
		wait.erase(wait.begin());
		merge(aa,xx);
	}


}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll tot=0;
	ll n,m;
	cin>>n>>m;
	for(ll i=0;i<n;i++){
		par[i]=i;
		size2[i]=1;
	}
	ll aa,bb;
	for(ll i=0;i<m;i++){
		cin>>aa>>bb;
		aa-=1;
		bb-=1;
		ll ac=aa;
		aa=find(aa);
		bb=find(bb);
		if(aa==bb){
			cout<<ans<<endl;
			continue;
		}
		if(out[aa].find({bb,ac})==out[aa].end()){
			auto it=inc[aa].lower_bound({bb,0});
			if(it!=inc[aa].end() and it->a==bb){
				merge(aa,bb);
			}
			else{
				inc[bb].insert({aa,ac});
				out[aa].insert({bb,ac});
				ans+=size2[bb];
			}
		}
		cout<<ans<<endl;
	}


	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:79:5: warning: unused variable 'tot' [-Wunused-variable]
  ll tot=0;
     ^~~
# 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 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 11 ms 9728 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 9776 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 11 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9728 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 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 10 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 13 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 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 11 ms 9728 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 9776 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 11 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9728 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 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 10 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 13 ms 9728 KB Output is correct
34 Correct 12 ms 9856 KB Output is correct
35 Correct 100 ms 13176 KB Output is correct
36 Correct 126 ms 15480 KB Output is correct
37 Correct 114 ms 15480 KB Output is correct
38 Correct 118 ms 15224 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 12 ms 9984 KB Output is correct
41 Correct 11 ms 9856 KB Output is correct
42 Correct 11 ms 9856 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 12 ms 9856 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 12 ms 9856 KB Output is correct
47 Correct 12 ms 9984 KB Output is correct
48 Correct 12 ms 9856 KB Output is correct
49 Correct 20 ms 10752 KB Output is correct
50 Correct 115 ms 15608 KB Output is correct
51 Correct 21 ms 10112 KB Output is correct
52 Correct 112 ms 14200 KB Output is correct
53 Correct 18 ms 10624 KB Output is correct
54 Correct 122 ms 14840 KB Output is correct
55 Correct 14 ms 10624 KB Output is correct
56 Correct 14 ms 10624 KB Output is correct
57 Correct 15 ms 10624 KB Output is correct
58 Correct 14 ms 10624 KB Output is correct
59 Correct 12 ms 9856 KB Output is correct
60 Correct 92 ms 12152 KB Output is correct
61 Correct 13 ms 9984 KB Output is correct
62 Correct 106 ms 15100 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 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 11 ms 9728 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 9776 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 11 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9728 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 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 10 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 13 ms 9728 KB Output is correct
34 Correct 12 ms 9856 KB Output is correct
35 Correct 100 ms 13176 KB Output is correct
36 Correct 126 ms 15480 KB Output is correct
37 Correct 114 ms 15480 KB Output is correct
38 Correct 118 ms 15224 KB Output is correct
39 Correct 12 ms 9856 KB Output is correct
40 Correct 12 ms 9984 KB Output is correct
41 Correct 11 ms 9856 KB Output is correct
42 Correct 11 ms 9856 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 12 ms 9856 KB Output is correct
45 Correct 12 ms 9856 KB Output is correct
46 Correct 12 ms 9856 KB Output is correct
47 Correct 12 ms 9984 KB Output is correct
48 Correct 12 ms 9856 KB Output is correct
49 Correct 20 ms 10752 KB Output is correct
50 Correct 115 ms 15608 KB Output is correct
51 Correct 21 ms 10112 KB Output is correct
52 Correct 112 ms 14200 KB Output is correct
53 Correct 18 ms 10624 KB Output is correct
54 Correct 122 ms 14840 KB Output is correct
55 Correct 14 ms 10624 KB Output is correct
56 Correct 14 ms 10624 KB Output is correct
57 Correct 15 ms 10624 KB Output is correct
58 Correct 14 ms 10624 KB Output is correct
59 Correct 12 ms 9856 KB Output is correct
60 Correct 92 ms 12152 KB Output is correct
61 Correct 13 ms 9984 KB Output is correct
62 Correct 106 ms 15100 KB Output is correct
63 Correct 363 ms 50808 KB Output is correct
64 Correct 375 ms 50936 KB Output is correct
65 Correct 375 ms 51004 KB Output is correct
66 Correct 107 ms 13432 KB Output is correct
67 Correct 173 ms 18936 KB Output is correct
68 Correct 123 ms 13592 KB Output is correct
69 Correct 258 ms 19576 KB Output is correct
70 Correct 125 ms 13432 KB Output is correct
71 Correct 129 ms 13560 KB Output is correct
72 Correct 203 ms 19064 KB Output is correct
73 Correct 196 ms 19064 KB Output is correct
74 Correct 651 ms 29432 KB Output is correct
75 Correct 430 ms 24224 KB Output is correct
76 Correct 527 ms 30680 KB Output is correct
77 Correct 471 ms 30716 KB Output is correct
78 Correct 168 ms 19064 KB Output is correct
79 Correct 290 ms 21240 KB Output is correct
80 Correct 181 ms 19192 KB Output is correct
81 Correct 248 ms 20600 KB Output is correct
82 Correct 548 ms 53508 KB Output is correct
83 Correct 514 ms 53368 KB Output is correct
84 Correct 445 ms 53368 KB Output is correct
85 Correct 453 ms 53496 KB Output is correct
86 Correct 113 ms 12664 KB Output is correct
87 Correct 138 ms 13688 KB Output is correct
88 Correct 224 ms 19080 KB Output is correct
89 Correct 448 ms 27896 KB Output is correct