#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define tr(it,a) for(auto it:a)
#define pob pop_back
#define pf push_front
#define pof pop_front
#define umin(x,y) (x=min(x,(y)))
#define umax(x,y) (x=max(x,(y)))
#define mid ((l+r)/2)
#define lch (idx*2+1)
#define rch (idx*2+2)
//
#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
typedef pair<int,int> pii;
#define REP(i,j,k) for(int i=(j);i<(k);++i)
#define RREP(i,j,k) for(int i=int(j)-1;i>=(k);--i)
#define ALL(a) a.begin(),a.end()
#define pb push_back
#define f first
#define s second
#define endl '\n'
// #define __db
#ifdef __db
#define IOS
#define prt(...) cerr<<__VA_ARGS__
#define ary(s,t)\
for(auto it=(s);it!=(t);++it)prt(*it<<" ");\
prt(endl);
#else
#define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)
#define prt(...)
#define ary(...)
#endif
//
const int maxn=1e5+9,maxm=3e5+9;
int n,m,res=0,fa[maxn],sz[maxn];
set<pii>*ex[maxn],*re[maxn];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void merge(int pa,int pb){
pa=find(pa),pb=find(pb);
if(pa==pb)return;
res-=sz[pa]*re[pa]->size();
res-=sz[pb]*re[pb]->size();
while(1){
auto it=ex[pa]->lower_bound({pb,0});
if(it!=ex[pa]->end()&&it->f==pb)ex[pa]->erase(it);
else break;
}
while(1){
auto it=re[pa]->lower_bound({pb,0});
if(it!=re[pa]->end()&&it->f==pb)re[pa]->erase(it);
else break;
}
while(1){
auto it=ex[pb]->lower_bound({pa,0});
if(it!=ex[pb]->end()&&it->f==pa)ex[pb]->erase(it);
else break;
}
while(1){
auto it=re[pb]->lower_bound({pa,0});
if(it!=re[pb]->end()&&it->f==pa)re[pb]->erase(it);
else break;
}
if(ex[pa]->size()+re[pa]->size()>ex[pb]->size()+re[pb]->size())swap(pa,pb);
tr(x,*ex[pa]){
re[x.f]->erase({pa,x.s});
re[x.f]->insert({pb,x.s});
}
tr(x,*re[pa]){
ex[x.f]->erase({pa,x.s});
ex[x.f]->insert({pb,x.s});
}
vector<int>nx;
tr(x,*ex[pa]){
auto it=re[pb]->lower_bound({x.f,0});
if(it!=re[pb]->end()&&it->f==x.f)nx.pb(x.f);
ex[pb]->insert(x);
}
tr(x,*re[pa]){
auto it=ex[pb]->lower_bound({x.f,0});
if(it!=ex[pb]->end()&&it->f==x.f)nx.pb(x.f);
re[pb]->insert(x);
}
res+=2*sz[pa]*sz[pb];
res+=(sz[pa]+sz[pb])*re[pb]->size();
fa[pa]=pb,sz[pb]+=sz[pa];
REP(i,0,nx.size())merge(pb,nx[i]);
}
main(){
IOS;
cin>>n>>m;
REP(i,0,n)fa[i]=i,sz[i]=1,ex[i]=new set<pii>,re[i]=new set<pii>;
while(m--){
int a,b,pa,pb;cin>>a>>b,--a,--b,pa=find(a),pb=find(b);
if(pa==pb||ex[pa]->count({pb,a})){
cout<<res<<endl;
continue;
}
ex[pa]->insert({pb,a});
re[pb]->insert({pa,a});
res+=sz[pb];
auto it=ex[pb]->lower_bound({pa,0});
if(it!=ex[pb]->end()&&it->f==pa){
merge(pa,pb);
}
// REP(i,0,n){
// cout<<i+1<<"->"<<find(i)+1<<endl;
// }
// REP(i,0,n){
// if(i==find(i)){
// cout<<i+1<<":";
// tr(it,*ex[i])cout<<it.f+1<<","<<it.s+1<<" ";cout<<endl;
// tr(it,*re[i])cout<<it.f+1<<","<<it.s+1<<" ";cout<<endl;
// }
// }
// cout<<endl;
cout<<res<<endl;
}
}
Compilation message
joitter2.cpp: In function 'void merge(int_fast64_t, int_fast64_t)':
joitter2.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define REP(i,j,k) for(int i=(j);i<(k);++i)
^
joitter2.cpp:97:2: note: in expansion of macro 'REP'
REP(i,0,nx.size())merge(pb,nx[i]);
^~~
joitter2.cpp: At global scope:
joitter2.cpp:100:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
512 KB |
Output is correct |
35 |
Correct |
87 ms |
6392 KB |
Output is correct |
36 |
Correct |
118 ms |
9372 KB |
Output is correct |
37 |
Correct |
115 ms |
9336 KB |
Output is correct |
38 |
Correct |
114 ms |
9008 KB |
Output is correct |
39 |
Correct |
6 ms |
768 KB |
Output is correct |
40 |
Correct |
7 ms |
896 KB |
Output is correct |
41 |
Correct |
7 ms |
896 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
9 ms |
896 KB |
Output is correct |
44 |
Correct |
8 ms |
896 KB |
Output is correct |
45 |
Correct |
8 ms |
768 KB |
Output is correct |
46 |
Correct |
6 ms |
800 KB |
Output is correct |
47 |
Correct |
8 ms |
896 KB |
Output is correct |
48 |
Correct |
7 ms |
896 KB |
Output is correct |
49 |
Correct |
15 ms |
1792 KB |
Output is correct |
50 |
Correct |
124 ms |
9424 KB |
Output is correct |
51 |
Correct |
12 ms |
1152 KB |
Output is correct |
52 |
Correct |
99 ms |
7940 KB |
Output is correct |
53 |
Correct |
14 ms |
1664 KB |
Output is correct |
54 |
Correct |
111 ms |
8568 KB |
Output is correct |
55 |
Correct |
9 ms |
1280 KB |
Output is correct |
56 |
Correct |
9 ms |
1280 KB |
Output is correct |
57 |
Correct |
9 ms |
1664 KB |
Output is correct |
58 |
Correct |
10 ms |
1664 KB |
Output is correct |
59 |
Correct |
7 ms |
768 KB |
Output is correct |
60 |
Correct |
87 ms |
5752 KB |
Output is correct |
61 |
Correct |
8 ms |
1024 KB |
Output is correct |
62 |
Correct |
111 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
512 KB |
Output is correct |
35 |
Correct |
87 ms |
6392 KB |
Output is correct |
36 |
Correct |
118 ms |
9372 KB |
Output is correct |
37 |
Correct |
115 ms |
9336 KB |
Output is correct |
38 |
Correct |
114 ms |
9008 KB |
Output is correct |
39 |
Correct |
6 ms |
768 KB |
Output is correct |
40 |
Correct |
7 ms |
896 KB |
Output is correct |
41 |
Correct |
7 ms |
896 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
9 ms |
896 KB |
Output is correct |
44 |
Correct |
8 ms |
896 KB |
Output is correct |
45 |
Correct |
8 ms |
768 KB |
Output is correct |
46 |
Correct |
6 ms |
800 KB |
Output is correct |
47 |
Correct |
8 ms |
896 KB |
Output is correct |
48 |
Correct |
7 ms |
896 KB |
Output is correct |
49 |
Correct |
15 ms |
1792 KB |
Output is correct |
50 |
Correct |
124 ms |
9424 KB |
Output is correct |
51 |
Correct |
12 ms |
1152 KB |
Output is correct |
52 |
Correct |
99 ms |
7940 KB |
Output is correct |
53 |
Correct |
14 ms |
1664 KB |
Output is correct |
54 |
Correct |
111 ms |
8568 KB |
Output is correct |
55 |
Correct |
9 ms |
1280 KB |
Output is correct |
56 |
Correct |
9 ms |
1280 KB |
Output is correct |
57 |
Correct |
9 ms |
1664 KB |
Output is correct |
58 |
Correct |
10 ms |
1664 KB |
Output is correct |
59 |
Correct |
7 ms |
768 KB |
Output is correct |
60 |
Correct |
87 ms |
5752 KB |
Output is correct |
61 |
Correct |
8 ms |
1024 KB |
Output is correct |
62 |
Correct |
111 ms |
8824 KB |
Output is correct |
63 |
Correct |
420 ms |
59256 KB |
Output is correct |
64 |
Correct |
425 ms |
59000 KB |
Output is correct |
65 |
Correct |
426 ms |
59000 KB |
Output is correct |
66 |
Correct |
132 ms |
20472 KB |
Output is correct |
67 |
Correct |
259 ms |
26580 KB |
Output is correct |
68 |
Correct |
135 ms |
20476 KB |
Output is correct |
69 |
Correct |
358 ms |
26616 KB |
Output is correct |
70 |
Correct |
132 ms |
20472 KB |
Output is correct |
71 |
Correct |
133 ms |
20472 KB |
Output is correct |
72 |
Correct |
311 ms |
26912 KB |
Output is correct |
73 |
Correct |
292 ms |
26872 KB |
Output is correct |
74 |
Correct |
871 ms |
52344 KB |
Output is correct |
75 |
Correct |
554 ms |
33276 KB |
Output is correct |
76 |
Correct |
633 ms |
42364 KB |
Output is correct |
77 |
Correct |
634 ms |
42360 KB |
Output is correct |
78 |
Correct |
194 ms |
27128 KB |
Output is correct |
79 |
Correct |
364 ms |
30968 KB |
Output is correct |
80 |
Correct |
185 ms |
27000 KB |
Output is correct |
81 |
Correct |
305 ms |
30328 KB |
Output is correct |
82 |
Correct |
589 ms |
45432 KB |
Output is correct |
83 |
Correct |
557 ms |
45432 KB |
Output is correct |
84 |
Correct |
538 ms |
60920 KB |
Output is correct |
85 |
Correct |
546 ms |
61048 KB |
Output is correct |
86 |
Correct |
140 ms |
19576 KB |
Output is correct |
87 |
Correct |
177 ms |
21880 KB |
Output is correct |
88 |
Correct |
310 ms |
27128 KB |
Output is correct |
89 |
Correct |
600 ms |
39928 KB |
Output is correct |