답안 #444371

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444371 2021-07-13T19:24:47 Z cheetose 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
5 ms 9676 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 p[x]=find(p[x]);
}
void merge(int x,int y){
	printf("%d %d\n",x,y);
	if(x==y)return;
	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);
	}
	it=v[x].lower_bound(mp(y,-1));
	while(it!=v[x].end() && (it->X)==y){
		ans-=sz[x];
		rev[it->X].erase(mp(x,it->Y));
		it=v[x].erase(it);
	}
	if(v[x].size()+rev[x].size()>v[y].size()+rev[y].size())swap(x,y);
	ans+=1ll*v[y].size()*sz[x]+1ll*v[x].size()*sz[y];
	sz[y]+=sz[x];
	p[x]=y;
	ans+=nP2(sz[y]);
	VPi vv;
	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));
		auto it2=v[it->X].lower_bound(mp(y,-1));
		if(it2!=v[it->X].end() && it2->X==y){
			vv.pb(mp(y,it->X));
		}
	}
	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));
		auto it2=v[y].lower_bound(mp(it->X,-1));
		if(it2!=v[y].end() && it2->X==it->X){
			vv.pb(mp(x,it->X));
		}
	}
	for(Pi P:vv){
		merge(find(P.X),find(P.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:99:2: note: in expansion of macro 'fup'
   99 |  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:103:2: note: in expansion of macro 'fup'
  103 |  fup(i,1,m,1){
      |  ^~~
joitter2.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%d%d",&y,&x);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -