Submission #444088

# Submission time Handle Problem Language Result Execution time Memory
444088 2021-07-13T05:51:34 Z kig9981 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
6 ms 9676 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 union_(int a, int b)
{
	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];
	for(auto n: adj[b]) {
		radj[n.first].erase({b,n.second});
		if(n.first!=a) {
			radj[n.first].emplace(a,n.second);
			adj[a].insert(n);
		}
	}
	for(auto n: radj[b]) {
		adj[n.first].erase({b,n.second});
		if(n.first!=a) {
			adj[n.first].emplace(a,n.second);
			radj[a].insert(n);
		}
	}
	ans+=cnt[a]*(adj[a].size()+cnt[a]-1LL);
}

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;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Incorrect 6 ms 9676 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Incorrect 6 ms 9676 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Incorrect 6 ms 9676 KB Output isn't correct
4 Halted 0 ms 0 KB -