답안 #444084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444084 2021-07-13T05:49:22 Z kig9981 조이터에서 친구를 만드는건 재밌어 (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) adj[a].insert(n);
	}
	for(auto n: radj[b]) {
		adj[n.first].erase({b,n.second});
		if(n.first!=a) 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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -