Submission #837918

# Submission time Handle Problem Language Result Execution time Memory
837918 2023-08-25T20:09:30 Z MohamedAhmed04 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
12 ms 21584 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e5 + 10 ;

int arr[MAX] ;
int n , m ;

int par[MAX] , sz[MAX] ;
set<int>adj1[MAX] , adj2[MAX] ; //from a clique to another clique and reverse
set<int>adj[MAX] , adjr[MAX] ; //from a node to clique and reverse edge
vector<int>v[MAX] ;
int cnt[MAX] ;

void init()
{
	for(int i = 1 ; i <= n ; ++i)
		par[i] = i , sz[i] = 1 , v[i] = {i} ;
}

int root(int node)
{
	if(par[node] == node)
		return node ;
	return (par[node] = root(par[node])) ;
}

long long ans = 0 ;

void Union(int x , int y)
{
	int a = root(x) ;
	int b = root(y) ;
	if(a == b)
		return ;
	if(sz[a] < sz[b])
		swap(a , b) ;
	for(auto &node : adj1[b]) 
		adj1[a].insert(node) , adj2[node].erase(b) , adj2[node].insert(a) ;
	for(auto &node : adj2[b])
	{
		adj1[node].erase(b) ;
		adj1[node].insert(a) ;
		adj2[a].insert(node) ;
	}
	ans += 1ll * (sz[a] + cnt[a]) * sz[b] ;
	for(auto &node : v[b])
	{
		if(adjr[a].find(node) == adjr[a].end())
			ans += sz[a] ;
		else
			ans -= sz[b] , cnt[a]-- , adjr[a].erase(node) ;
		v[a].push_back(node) ;
	}
	v[b].clear() ;
	for(auto &node : adjr[b]) //from node outside b to b
	{
		if(root(node) == a || adjr[a].find(node) != adjr[a].end())
			ans -= sz[b] , cnt[b]-- ;
		else
			ans += sz[a] , adjr[a].insert(node) ;
	}
	par[b] = a ;
	sz[a] += sz[b] ;
	cnt[a] += cnt[b] ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	init() ;
	for(int i = 0 ; i < m ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		if(root(x) != root(y))
		{
			if(adj1[root(y)].find(root(x)) != adj1[root(y)].end())
				Union(x , y) ;
			else if(adjr[root(y)].find(x) == adjr[root(y)].end())
			{
				ans += sz[root(y)] , cnt[root(y)]++ ;
				adjr[root(y)].insert(x) , adj1[root(x)].insert(root(y)) , adj2[root(y)].insert(root(x)) ;
			}
		}
		cout<<ans<<"\n" ;
	}
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21584 KB Output is correct
2 Correct 12 ms 21460 KB Output is correct
3 Incorrect 10 ms 21460 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21584 KB Output is correct
2 Correct 12 ms 21460 KB Output is correct
3 Incorrect 10 ms 21460 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21584 KB Output is correct
2 Correct 12 ms 21460 KB Output is correct
3 Incorrect 10 ms 21460 KB Output isn't correct
4 Halted 0 ms 0 KB -