Submission #882675

# Submission time Handle Problem Language Result Execution time Memory
882675 2023-12-03T12:54:06 Z 8pete8 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
33 ms 144220 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+5,lg=30,inf=1e18,minf=-1e18;
int pa[mxn+10],sz[mxn+10];
set<int>con[mxn+10],rcon[mxn+10],fol[mxn+10];
// ->, <-, all node that follow in component||component of that node
queue<pii>todo;
int find(int u){return pa[u]==u?u:pa[u]=find(pa[u]);}
int ans;
void merg(int a,int b){
	a=find(a),b=find(b);
	if(a==b)return;
	if(sz[a]>sz[b])swap(a,b);
	ans+=(fol[b].size()*sz[a])+(sz[b]*fol[a].size());
	pa[b]=a;
	sz[a]+=sz[b];
	for(auto i:fol[b]){
		if(fol[a].count(i))ans-=sz[a];
		else fol[a].insert(i);
	}
	con[b].erase(a),con[a].erase(b),rcon[a].erase(b),rcon[b].erase(a);
	for(auto i:con[b]){
		rcon[i].erase(b);
		rcon[i].insert(a);
		if(rcon[a].count(i))todo.push({i,a});
	}
	for(auto i:rcon[b]){
		con[i].erase(b);
		con[i].insert(a);
		if(con[a].count(i))todo.push({i,a});
	}
}
int32_t main(){
	fastio
	int n,m;cin>>n>>m;
	for(int i=1;i<=n;i++)pa[i]=i,sz[i]=1,fol[i].insert(i);
	while(m--){
		int a,b;cin>>a>>b;
		b=find(b);
		int g=find(a);
		if(g==b){
			cout<<ans<<'\n';
			continue;
		}
		if(!fol[b].count(a)){
			fol[b].insert(a);
			ans+=sz[b];
			con[g].insert(b);
			rcon[b].insert(g);
			if(con[b].count(g))todo.push({g,b});
		}
		while(!todo.empty())merg(todo.front().f,todo.front().s),todo.pop();
		cout<<ans<<'\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 144220 KB Output is correct
2 Correct 28 ms 144140 KB Output is correct
3 Correct 30 ms 144220 KB Output is correct
4 Correct 33 ms 144148 KB Output is correct
5 Incorrect 32 ms 144220 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 144220 KB Output is correct
2 Correct 28 ms 144140 KB Output is correct
3 Correct 30 ms 144220 KB Output is correct
4 Correct 33 ms 144148 KB Output is correct
5 Incorrect 32 ms 144220 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 144220 KB Output is correct
2 Correct 28 ms 144140 KB Output is correct
3 Correct 30 ms 144220 KB Output is correct
4 Correct 33 ms 144148 KB Output is correct
5 Incorrect 32 ms 144220 KB Output isn't correct
6 Halted 0 ms 0 KB -