답안 #444372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444372 2021-07-13T19:25:07 Z cheetose 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
734 ms 53256 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){
	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:98:2: note: in expansion of macro 'fup'
   98 |  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:102:2: note: in expansion of macro 'fup'
  102 |  fup(i,1,m,1){
      |  ^~~
joitter2.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   scanf("%d%d",&y,&x);
      |   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9636 KB Output is correct
14 Correct 5 ms 9696 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9692 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9676 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 5 ms 9676 KB Output is correct
28 Correct 6 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9676 KB Output is correct
32 Correct 6 ms 9676 KB Output is correct
33 Correct 6 ms 9716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9636 KB Output is correct
14 Correct 5 ms 9696 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9692 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9676 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 5 ms 9676 KB Output is correct
28 Correct 6 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9676 KB Output is correct
32 Correct 6 ms 9676 KB Output is correct
33 Correct 6 ms 9716 KB Output is correct
34 Correct 8 ms 9684 KB Output is correct
35 Correct 109 ms 12892 KB Output is correct
36 Correct 137 ms 14688 KB Output is correct
37 Correct 138 ms 14844 KB Output is correct
38 Correct 132 ms 14496 KB Output is correct
39 Correct 8 ms 9676 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 8 ms 9816 KB Output is correct
42 Correct 7 ms 9712 KB Output is correct
43 Correct 8 ms 9804 KB Output is correct
44 Correct 8 ms 9804 KB Output is correct
45 Correct 8 ms 9744 KB Output is correct
46 Correct 7 ms 9620 KB Output is correct
47 Correct 8 ms 9804 KB Output is correct
48 Correct 8 ms 9804 KB Output is correct
49 Correct 16 ms 10316 KB Output is correct
50 Correct 137 ms 14768 KB Output is correct
51 Correct 13 ms 9964 KB Output is correct
52 Correct 120 ms 13636 KB Output is correct
53 Correct 15 ms 10328 KB Output is correct
54 Correct 129 ms 14288 KB Output is correct
55 Correct 10 ms 10060 KB Output is correct
56 Correct 10 ms 10060 KB Output is correct
57 Correct 10 ms 10444 KB Output is correct
58 Correct 10 ms 10560 KB Output is correct
59 Correct 8 ms 9708 KB Output is correct
60 Correct 109 ms 12044 KB Output is correct
61 Correct 9 ms 9804 KB Output is correct
62 Correct 129 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9636 KB Output is correct
14 Correct 5 ms 9696 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 5 ms 9692 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
22 Correct 5 ms 9676 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 7 ms 9676 KB Output is correct
25 Correct 6 ms 9676 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 5 ms 9676 KB Output is correct
28 Correct 6 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 5 ms 9676 KB Output is correct
31 Correct 6 ms 9676 KB Output is correct
32 Correct 6 ms 9676 KB Output is correct
33 Correct 6 ms 9716 KB Output is correct
34 Correct 8 ms 9684 KB Output is correct
35 Correct 109 ms 12892 KB Output is correct
36 Correct 137 ms 14688 KB Output is correct
37 Correct 138 ms 14844 KB Output is correct
38 Correct 132 ms 14496 KB Output is correct
39 Correct 8 ms 9676 KB Output is correct
40 Correct 8 ms 9804 KB Output is correct
41 Correct 8 ms 9816 KB Output is correct
42 Correct 7 ms 9712 KB Output is correct
43 Correct 8 ms 9804 KB Output is correct
44 Correct 8 ms 9804 KB Output is correct
45 Correct 8 ms 9744 KB Output is correct
46 Correct 7 ms 9620 KB Output is correct
47 Correct 8 ms 9804 KB Output is correct
48 Correct 8 ms 9804 KB Output is correct
49 Correct 16 ms 10316 KB Output is correct
50 Correct 137 ms 14768 KB Output is correct
51 Correct 13 ms 9964 KB Output is correct
52 Correct 120 ms 13636 KB Output is correct
53 Correct 15 ms 10328 KB Output is correct
54 Correct 129 ms 14288 KB Output is correct
55 Correct 10 ms 10060 KB Output is correct
56 Correct 10 ms 10060 KB Output is correct
57 Correct 10 ms 10444 KB Output is correct
58 Correct 10 ms 10560 KB Output is correct
59 Correct 8 ms 9708 KB Output is correct
60 Correct 109 ms 12044 KB Output is correct
61 Correct 9 ms 9804 KB Output is correct
62 Correct 129 ms 14412 KB Output is correct
63 Correct 409 ms 40636 KB Output is correct
64 Correct 401 ms 40516 KB Output is correct
65 Correct 398 ms 40672 KB Output is correct
66 Correct 110 ms 12448 KB Output is correct
67 Correct 194 ms 16548 KB Output is correct
68 Correct 125 ms 14132 KB Output is correct
69 Correct 267 ms 19504 KB Output is correct
70 Correct 129 ms 14916 KB Output is correct
71 Correct 129 ms 14804 KB Output is correct
72 Correct 233 ms 18900 KB Output is correct
73 Correct 242 ms 18876 KB Output is correct
74 Correct 734 ms 28780 KB Output is correct
75 Correct 413 ms 24256 KB Output is correct
76 Correct 520 ms 27516 KB Output is correct
77 Correct 521 ms 27672 KB Output is correct
78 Correct 142 ms 18408 KB Output is correct
79 Correct 268 ms 22204 KB Output is correct
80 Correct 180 ms 18372 KB Output is correct
81 Correct 278 ms 21608 KB Output is correct
82 Correct 595 ms 32832 KB Output is correct
83 Correct 540 ms 32924 KB Output is correct
84 Correct 529 ms 53256 KB Output is correct
85 Correct 531 ms 53100 KB Output is correct
86 Correct 118 ms 14020 KB Output is correct
87 Correct 162 ms 16200 KB Output is correct
88 Correct 219 ms 18928 KB Output is correct
89 Correct 496 ms 26544 KB Output is correct