Submission #618929

# Submission time Handle Problem Language Result Execution time Memory
618929 2022-08-02T08:25:07 Z errorgorn Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1183 ms 86852 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound

#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n,m;

int p[100005];
int s[100005];

int par(int i){
	if (p[i]==i) return i;
	else return p[i]=par(p[i]);
}

set<ii> al[100005]; //point out
set<int> al2[100005]; //point in
int num[100005]; //number of edges pointing into

int IDX=0;
set<int> id[300005]; //id of those guys in the equiv class who are pointing

vector<ii> proc;

int get(int A,int B){
	auto it=al[A].lb(ii(B,-1));
	if (it==al[A].end() || (*it).fi!=B) return -1;
	else return (*it).se;
}

signed main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>n>>m;
	
	rep(x,1,n+1){
		p[x]=x;
		s[x]=1;
	}
	
	int a,b;
	int ans=0;
	while (m--){
		cin>>a>>b;
		int A=par(a),B=par(b);
		
		if (A!=B){
			if (get(A,B)==-1){
				al[A].insert({B,IDX++});
				al2[B].insert(A);
			}
			
			auto it=al[A].lb(ii(B,-1));
			int idx=(*it).se;
			if (!id[idx].count(a)){
				id[idx].insert(a);
				num[B]++;
				ans+=s[B];
			}
			
			if (get(B,A)!=-1) proc.pub({A,B});
		}
		
		while (!proc.empty()){
			tie(A,B)=proc.back(); proc.pob();
			A=par(A),B=par(B);
			if (A==B) continue;
			
			if (sz(al[A])+sz(al2[A])>sz(al[B])+sz(al2[B])) swap(A,B);
			
			//update proc in case of more mergers
			for (auto [it,idx]:al[A]) if (get(it,B)!=-1) proc.pub({A,it});
			for (auto it:al2[A]) if (get(B,it)!=-1) proc.pub({A,it});
			
			ans-=num[A]*s[A]+num[B]*s[B];
			ans-=s[A]*(s[A]-1)+s[B]*(s[B]-1);
			
			//erase same edges
			int idx=get(A,B);
			al[A].erase({B,idx});
			al2[B].erase(A);
			num[B]-=sz(id[idx]);
			
			idx=get(B,A);
			al[B].erase({A,idx});
			al2[A].erase(B);
			num[A]-=sz(id[idx]);
			
			for (auto [it,idx]:al[A]){
				int idx2=get(B,it);
				al2[it].erase(A);
				
				if (idx2==-1){
					al[B].insert({it,idx});
					al2[it].insert(B);
				}
				else{
					if (sz(id[idx2])<sz(id[idx])) swap(id[idx2],id[idx]);
					for (auto it:id[idx]) id[idx2].insert(it);
				}
			}
			for (auto it:al2[A]){
				int idx=get(it,A);
				int idx2=get(it,B);
				num[B]+=sz(id[idx]);
				if (idx2==-1){
					al[it].insert({B,idx});
					al2[B].insert(it);
				}
				else{
					if (sz(id[idx2])<sz(id[idx])) swap(id[idx2],id[idx]);
					for (auto it:id[idx]){
						if (id[idx2].count(it)) num[B]--;
						else id[idx2].insert(it);
					}
				}
			}
			
			s[B]+=s[A];
			ans+=num[B]*s[B];
			ans+=s[B]*(s[B]-1);
			
			p[A]=B;
		}
		
		//rep(x,1,n+1) cout<<p[x]<<" "; cout<<endl;
		//rep(x,1,n+1) cout<<s[x]<<" "; cout<<endl;
		//rep(x,1,n+1) cout<<num[x]<<" "; cout<<endl;
		cout<<ans<<endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23748 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 14 ms 23832 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 12 ms 23780 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 11 ms 23764 KB Output is correct
12 Correct 11 ms 23836 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 13 ms 23716 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 14 ms 23796 KB Output is correct
21 Correct 12 ms 23876 KB Output is correct
22 Correct 11 ms 23736 KB Output is correct
23 Correct 11 ms 23764 KB Output is correct
24 Correct 12 ms 23868 KB Output is correct
25 Correct 13 ms 23768 KB Output is correct
26 Correct 12 ms 23788 KB Output is correct
27 Correct 12 ms 23844 KB Output is correct
28 Correct 11 ms 23764 KB Output is correct
29 Correct 12 ms 23808 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 14 ms 23764 KB Output is correct
32 Correct 12 ms 23804 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23748 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 14 ms 23832 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 12 ms 23780 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 11 ms 23764 KB Output is correct
12 Correct 11 ms 23836 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 13 ms 23716 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 14 ms 23796 KB Output is correct
21 Correct 12 ms 23876 KB Output is correct
22 Correct 11 ms 23736 KB Output is correct
23 Correct 11 ms 23764 KB Output is correct
24 Correct 12 ms 23868 KB Output is correct
25 Correct 13 ms 23768 KB Output is correct
26 Correct 12 ms 23788 KB Output is correct
27 Correct 12 ms 23844 KB Output is correct
28 Correct 11 ms 23764 KB Output is correct
29 Correct 12 ms 23808 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 14 ms 23764 KB Output is correct
32 Correct 12 ms 23804 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 17 ms 23968 KB Output is correct
35 Correct 91 ms 30028 KB Output is correct
36 Correct 115 ms 33168 KB Output is correct
37 Correct 110 ms 33440 KB Output is correct
38 Correct 110 ms 32764 KB Output is correct
39 Correct 13 ms 24020 KB Output is correct
40 Correct 15 ms 24352 KB Output is correct
41 Correct 18 ms 24444 KB Output is correct
42 Correct 13 ms 24096 KB Output is correct
43 Correct 15 ms 24064 KB Output is correct
44 Correct 19 ms 24160 KB Output is correct
45 Correct 14 ms 24020 KB Output is correct
46 Correct 16 ms 24064 KB Output is correct
47 Correct 16 ms 24220 KB Output is correct
48 Correct 15 ms 24264 KB Output is correct
49 Correct 22 ms 25244 KB Output is correct
50 Correct 113 ms 33388 KB Output is correct
51 Correct 20 ms 24540 KB Output is correct
52 Correct 91 ms 31248 KB Output is correct
53 Correct 21 ms 25188 KB Output is correct
54 Correct 100 ms 32300 KB Output is correct
55 Correct 17 ms 24532 KB Output is correct
56 Correct 18 ms 24532 KB Output is correct
57 Correct 21 ms 24596 KB Output is correct
58 Correct 20 ms 24612 KB Output is correct
59 Correct 16 ms 24020 KB Output is correct
60 Correct 83 ms 29044 KB Output is correct
61 Correct 16 ms 24356 KB Output is correct
62 Correct 107 ms 32608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23748 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 11 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 14 ms 23832 KB Output is correct
7 Correct 15 ms 23756 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 12 ms 23780 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 11 ms 23764 KB Output is correct
12 Correct 11 ms 23836 KB Output is correct
13 Correct 11 ms 23764 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 13 ms 23716 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 14 ms 23796 KB Output is correct
21 Correct 12 ms 23876 KB Output is correct
22 Correct 11 ms 23736 KB Output is correct
23 Correct 11 ms 23764 KB Output is correct
24 Correct 12 ms 23868 KB Output is correct
25 Correct 13 ms 23768 KB Output is correct
26 Correct 12 ms 23788 KB Output is correct
27 Correct 12 ms 23844 KB Output is correct
28 Correct 11 ms 23764 KB Output is correct
29 Correct 12 ms 23808 KB Output is correct
30 Correct 11 ms 23764 KB Output is correct
31 Correct 14 ms 23764 KB Output is correct
32 Correct 12 ms 23804 KB Output is correct
33 Correct 13 ms 23764 KB Output is correct
34 Correct 17 ms 23968 KB Output is correct
35 Correct 91 ms 30028 KB Output is correct
36 Correct 115 ms 33168 KB Output is correct
37 Correct 110 ms 33440 KB Output is correct
38 Correct 110 ms 32764 KB Output is correct
39 Correct 13 ms 24020 KB Output is correct
40 Correct 15 ms 24352 KB Output is correct
41 Correct 18 ms 24444 KB Output is correct
42 Correct 13 ms 24096 KB Output is correct
43 Correct 15 ms 24064 KB Output is correct
44 Correct 19 ms 24160 KB Output is correct
45 Correct 14 ms 24020 KB Output is correct
46 Correct 16 ms 24064 KB Output is correct
47 Correct 16 ms 24220 KB Output is correct
48 Correct 15 ms 24264 KB Output is correct
49 Correct 22 ms 25244 KB Output is correct
50 Correct 113 ms 33388 KB Output is correct
51 Correct 20 ms 24540 KB Output is correct
52 Correct 91 ms 31248 KB Output is correct
53 Correct 21 ms 25188 KB Output is correct
54 Correct 100 ms 32300 KB Output is correct
55 Correct 17 ms 24532 KB Output is correct
56 Correct 18 ms 24532 KB Output is correct
57 Correct 21 ms 24596 KB Output is correct
58 Correct 20 ms 24612 KB Output is correct
59 Correct 16 ms 24020 KB Output is correct
60 Correct 83 ms 29044 KB Output is correct
61 Correct 16 ms 24356 KB Output is correct
62 Correct 107 ms 32608 KB Output is correct
63 Correct 502 ms 78548 KB Output is correct
64 Correct 506 ms 78488 KB Output is correct
65 Correct 442 ms 78520 KB Output is correct
66 Correct 125 ms 39960 KB Output is correct
67 Correct 393 ms 55412 KB Output is correct
68 Correct 143 ms 39984 KB Output is correct
69 Correct 437 ms 41940 KB Output is correct
70 Correct 136 ms 39964 KB Output is correct
71 Correct 136 ms 39868 KB Output is correct
72 Correct 409 ms 46412 KB Output is correct
73 Correct 387 ms 46896 KB Output is correct
74 Correct 1183 ms 86852 KB Output is correct
75 Correct 668 ms 50248 KB Output is correct
76 Correct 733 ms 64456 KB Output is correct
77 Correct 726 ms 64628 KB Output is correct
78 Correct 223 ms 46304 KB Output is correct
79 Correct 423 ms 51204 KB Output is correct
80 Correct 174 ms 43564 KB Output is correct
81 Correct 291 ms 47344 KB Output is correct
82 Correct 636 ms 65408 KB Output is correct
83 Correct 636 ms 65296 KB Output is correct
84 Correct 578 ms 65616 KB Output is correct
85 Correct 634 ms 65652 KB Output is correct
86 Correct 182 ms 39112 KB Output is correct
87 Correct 181 ms 41368 KB Output is correct
88 Correct 381 ms 46928 KB Output is correct
89 Correct 706 ms 62200 KB Output is correct