Submission #444357

# Submission time Handle Problem Language Result Execution time Memory
444357 2021-07-13T18:18:35 Z cheetose Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
7 ms 9688 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

int n,m;
ll ans;
set<Pi> v[100001],rev[100001];
int p[100001],sz[100001];
inline ll nP2(int x){return 1ll*x*(x-1);}
int find(int x){
	if(x==p[x])return x;
	return find(p[x]);
}
void merge(int x,int y){
	ans-=nP2(sz[x])+nP2(sz[y]);
	auto it=v[y].lower_bound(mp(x,-1));
	while(it!=v[y].end() && (it->X)==x){
		ans-=sz[y];
		rev[it->X].erase(mp(y,it->Y));
		it=v[y].erase(it);
	}
	if(v[x].size()+rev[x].size()>v[y].size()+rev[y].size())swap(x,y);
	ans+=1ll*v[x].size()*sz[y]+1ll*v[y].size()*sz[x];
	sz[y]+=sz[x];
	p[x]=y;
	ans+=nP2(sz[y]);
	for(it=v[x].begin();it!=v[x].end();it=v[x].erase(it)){
		if(v[y].find(*it)!=v[y].end())ans-=sz[y];
		else v[y].insert(*it);
		rev[it->X].erase(mp(x,it->Y));
		rev[it->X].insert(mp(y,it->Y));
	}
	for(it=rev[x].begin();it!=rev[x].end();it=rev[x].erase(it)){
		rev[y].insert(*it);
		v[it->X].erase(mp(x,it->Y));
		v[it->X].insert(mp(y,it->Y));
	}
}
int main(){
	scanf("%d%d",&n,&m);
	fup(i,1,n,1){
		sz[i]=1;
		p[i]=i;
	}
	fup(i,1,m,1){
		int x,y;
		scanf("%d%d",&y,&x);
		int xx=find(x),yy=find(y);
		if(xx==yy || v[xx].find(mp(yy,y))!=v[xx].end()){
			printf("%lld\n",ans);
			continue;
		}
		auto it=v[yy].lower_bound(mp(xx,-1));
		if(it==v[yy].end() || (it->X)!=xx){
			ans+=sz[xx];
			v[xx].insert(mp(yy,y));
			rev[yy].insert(mp(xx,y));
			printf("%lld\n",ans);
			continue;
		}else{
			merge(xx,yy);
			printf("%lld\n",ans);
		}
	}
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
joitter2.cpp:79:2: note: in expansion of macro 'fup'
   79 |  fup(i,1,n,1){
      |  ^~~
joitter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
      |                              ^
joitter2.cpp:83:2: note: in expansion of macro 'fup'
   83 |  fup(i,1,m,1){
      |  ^~~
joitter2.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |   scanf("%d%d",&y,&x);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9640 KB Output is correct
3 Incorrect 7 ms 9688 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 9640 KB Output is correct
3 Incorrect 7 ms 9688 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 9640 KB Output is correct
3 Incorrect 7 ms 9688 KB Output isn't correct
4 Halted 0 ms 0 KB -