답안 #771823

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
771823 2023-07-03T09:54:55 Z Mohammad_Parsa 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
602 ms 42684 KB
/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
typedef long long ll;

const int N=1e5+7;
int n,m,a[N],vis[N];
ll ans,sz[N];
vector<int>st[N];
set<pair<int,int>>v[N];
vector<int>vec[N],cev[N];
vector<pair<int,int>>can;

void upd(int i,int x){
	for(auto u:vec[i]){
		v[a[u]].erase(mk(a[i],i));
		if(a[u]==x || a[u]==a[i])continue;
		v[a[u]].insert(mk(x,i));
		
		auto it=v[x].lower_bound(mk(a[u],0));
		if(it!=v[x].end() && it->F==a[u])can.pb(mk(x,a[u]));

	}
	for(auto u:cev[i]){
		v[a[i]].erase(mk(a[u],u));
		if(a[u]==x || a[u]==a[i])continue;
		v[x].insert(mk(a[u],u));

		auto it=v[a[u]].lower_bound(mk(x,0));
		if(it!=v[a[u]].end() && it->F==x)can.pb(mk(x,a[u]));
	}
	a[i]=x;
}

void merge(int x,int y){
	ans-=sz[x]*v[x].size();
	ans-=sz[y]*v[y].size();
	ans-=sz[x]*(sz[x]-1);
	ans-=sz[y]*(sz[y]-1);
	
	if(st[x].size()<st[y].size())swap(x,y);
	for(auto i:st[y]){
		upd(i,x);
		st[x].pb(i);
	}
	vis[y]=1;
	sz[x]+=sz[y];
	ans+=sz[x]*(sz[x]-1);
	ans+=sz[x]*v[x].size();
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		a[i]=i;
		sz[i]=1;
		st[i].pb(i);
	}
	int u,V;
	while(m--){
		cin>>u>>V;
		vec[u].pb(V);
		cev[V].pb(u);
		int x=a[u],y=a[V];
		if(x==y){
			cout<<ans<<endl;
			continue;
		}
		if(!v[y].count(mk(x,u)))ans+=1ll*(sz[y]);
		v[y].insert(mk(x,u));

		auto it=v[x].lower_bound(mk(y,0));
		if(it!=v[x].end() && it->F==y)can.pb(mk(x,y));;
		while(can.size()){
			int f=can.back().F,s=can.back().S;
			can.pop_back();
			if(vis[f] || vis[s])continue;
			merge(f,s);
		}
		cout<<ans<<endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12076 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11972 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 6 ms 12084 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 6 ms 12080 KB Output is correct
11 Correct 8 ms 12152 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12072 KB Output is correct
16 Correct 5 ms 11976 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 7 ms 12116 KB Output is correct
22 Correct 7 ms 12076 KB Output is correct
23 Correct 6 ms 12116 KB Output is correct
24 Correct 6 ms 12104 KB Output is correct
25 Correct 6 ms 12116 KB Output is correct
26 Correct 6 ms 12072 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 11988 KB Output is correct
29 Correct 6 ms 12072 KB Output is correct
30 Correct 6 ms 12076 KB Output is correct
31 Correct 6 ms 12116 KB Output is correct
32 Correct 6 ms 12044 KB Output is correct
33 Correct 6 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12076 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11972 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 6 ms 12084 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 6 ms 12080 KB Output is correct
11 Correct 8 ms 12152 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12072 KB Output is correct
16 Correct 5 ms 11976 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 7 ms 12116 KB Output is correct
22 Correct 7 ms 12076 KB Output is correct
23 Correct 6 ms 12116 KB Output is correct
24 Correct 6 ms 12104 KB Output is correct
25 Correct 6 ms 12116 KB Output is correct
26 Correct 6 ms 12072 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 11988 KB Output is correct
29 Correct 6 ms 12072 KB Output is correct
30 Correct 6 ms 12076 KB Output is correct
31 Correct 6 ms 12116 KB Output is correct
32 Correct 6 ms 12044 KB Output is correct
33 Correct 6 ms 12152 KB Output is correct
34 Correct 7 ms 12244 KB Output is correct
35 Correct 63 ms 21020 KB Output is correct
36 Correct 75 ms 21724 KB Output is correct
37 Correct 77 ms 21720 KB Output is correct
38 Correct 79 ms 21740 KB Output is correct
39 Correct 9 ms 12504 KB Output is correct
40 Correct 8 ms 12340 KB Output is correct
41 Correct 8 ms 12372 KB Output is correct
42 Correct 7 ms 12372 KB Output is correct
43 Correct 7 ms 12372 KB Output is correct
44 Correct 8 ms 12372 KB Output is correct
45 Correct 7 ms 12324 KB Output is correct
46 Correct 7 ms 12372 KB Output is correct
47 Correct 8 ms 12372 KB Output is correct
48 Correct 8 ms 12372 KB Output is correct
49 Correct 11 ms 12628 KB Output is correct
50 Correct 76 ms 21768 KB Output is correct
51 Correct 11 ms 12468 KB Output is correct
52 Correct 70 ms 21580 KB Output is correct
53 Correct 11 ms 12628 KB Output is correct
54 Correct 74 ms 21776 KB Output is correct
55 Correct 8 ms 12500 KB Output is correct
56 Correct 10 ms 12556 KB Output is correct
57 Correct 8 ms 12472 KB Output is correct
58 Correct 8 ms 12472 KB Output is correct
59 Correct 8 ms 12380 KB Output is correct
60 Correct 64 ms 21724 KB Output is correct
61 Correct 9 ms 12336 KB Output is correct
62 Correct 78 ms 21664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12076 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11972 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 6 ms 12084 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 6 ms 12080 KB Output is correct
11 Correct 8 ms 12152 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12072 KB Output is correct
16 Correct 5 ms 11976 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 7 ms 12116 KB Output is correct
22 Correct 7 ms 12076 KB Output is correct
23 Correct 6 ms 12116 KB Output is correct
24 Correct 6 ms 12104 KB Output is correct
25 Correct 6 ms 12116 KB Output is correct
26 Correct 6 ms 12072 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 11988 KB Output is correct
29 Correct 6 ms 12072 KB Output is correct
30 Correct 6 ms 12076 KB Output is correct
31 Correct 6 ms 12116 KB Output is correct
32 Correct 6 ms 12044 KB Output is correct
33 Correct 6 ms 12152 KB Output is correct
34 Correct 7 ms 12244 KB Output is correct
35 Correct 63 ms 21020 KB Output is correct
36 Correct 75 ms 21724 KB Output is correct
37 Correct 77 ms 21720 KB Output is correct
38 Correct 79 ms 21740 KB Output is correct
39 Correct 9 ms 12504 KB Output is correct
40 Correct 8 ms 12340 KB Output is correct
41 Correct 8 ms 12372 KB Output is correct
42 Correct 7 ms 12372 KB Output is correct
43 Correct 7 ms 12372 KB Output is correct
44 Correct 8 ms 12372 KB Output is correct
45 Correct 7 ms 12324 KB Output is correct
46 Correct 7 ms 12372 KB Output is correct
47 Correct 8 ms 12372 KB Output is correct
48 Correct 8 ms 12372 KB Output is correct
49 Correct 11 ms 12628 KB Output is correct
50 Correct 76 ms 21768 KB Output is correct
51 Correct 11 ms 12468 KB Output is correct
52 Correct 70 ms 21580 KB Output is correct
53 Correct 11 ms 12628 KB Output is correct
54 Correct 74 ms 21776 KB Output is correct
55 Correct 8 ms 12500 KB Output is correct
56 Correct 10 ms 12556 KB Output is correct
57 Correct 8 ms 12472 KB Output is correct
58 Correct 8 ms 12472 KB Output is correct
59 Correct 8 ms 12380 KB Output is correct
60 Correct 64 ms 21724 KB Output is correct
61 Correct 9 ms 12336 KB Output is correct
62 Correct 78 ms 21664 KB Output is correct
63 Correct 303 ms 42660 KB Output is correct
64 Correct 292 ms 42684 KB Output is correct
65 Correct 302 ms 42676 KB Output is correct
66 Correct 115 ms 27832 KB Output is correct
67 Correct 340 ms 31740 KB Output is correct
68 Correct 115 ms 28604 KB Output is correct
69 Correct 226 ms 29424 KB Output is correct
70 Correct 140 ms 27972 KB Output is correct
71 Correct 128 ms 27948 KB Output is correct
72 Correct 286 ms 29088 KB Output is correct
73 Correct 274 ms 29388 KB Output is correct
74 Correct 602 ms 35988 KB Output is correct
75 Correct 328 ms 32580 KB Output is correct
76 Correct 456 ms 34320 KB Output is correct
77 Correct 411 ms 34336 KB Output is correct
78 Correct 96 ms 27712 KB Output is correct
79 Correct 211 ms 32184 KB Output is correct
80 Correct 144 ms 27680 KB Output is correct
81 Correct 292 ms 31792 KB Output is correct
82 Correct 328 ms 36868 KB Output is correct
83 Correct 411 ms 36860 KB Output is correct
84 Correct 321 ms 36696 KB Output is correct
85 Correct 300 ms 36732 KB Output is correct
86 Correct 209 ms 32652 KB Output is correct
87 Correct 297 ms 34752 KB Output is correct
88 Correct 270 ms 29128 KB Output is correct
89 Correct 424 ms 34012 KB Output is correct