#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
typedef map<ll,ll> mi;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef pair<ld,ld> pd;
#define ff first
#define ss second
#define srt(a) sort(a.begin(),a.end());
#define fip(k, n) for (ll i = k; i < n; i++)
#define fjp(k, n) for (ll j = k; j < n; j++)
#define fin(k, n) for (ll i = k; i >= n; i--)
#define fjn(k, n) for (ll j = k; j >= n; j--)
#define fp(k, n, m) for (ll k = n; k < m; k++)
#define fn(k, n, m) for (ll k = n; k >= m; k--)
#define ordered_set tree<pi, null_type,less< pi >, rb_tree_tag,tree_order_statistics_node_update>
#define totalOne(n) __builtin_popcount(n)
#define backZero(n) __builtin_ctzll(n)
#define frontZero(n) __builtin_clzll(n)
#define fx(k) for ( auto x : k )
#define test ll t;cin >> t;while (t--)
#define nli "\n"
// ==========================(debug)============================================================================================== //
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _printn(x); cerr << nli;
#else
#define debug(x)
#endif
void _printn(ll x){ cerr<<x<<" "; }
void _printn(int x){ cerr<<x<<" "; }
void _printn(ld x){ cerr<<x<<" "; }
void _printn(double x){ cerr<<x<<" "; }
void _printn(string x){ cerr<<x<<" "; }
void _printn(char x){ cerr<<x<<" "; }
void _printn(bool x){ cerr<<x<<" "; }
template<class T,class V>void _printn(pair<T,V> vv);
template<class T> void _printn(vector<T> vv);
template<class T> void _printn(set<T> vv);
template<class T,class V> void _printn(map<T,V> vv);
template<class T> void _printn(multiset<T> vv);
template<class T,class V>void _printn(pair<T,V> vv){ cerr<<"( ";_printn(vv.ff);cerr<<",";_printn(vv.ss);cerr<<")";}
template<class T> void _printn(vector<T> vv){ cerr<<"[ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"]"; };
template<class T> void _printn(set<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T> void _printn(multiset<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T,class V> void _printn(map<T,V> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
// ==========================(debug)============================================================================================== //
ll n,m,tp,tp2,res,cnt,sum,tptp,ans;
const ll mx = 5e5+5;
const ll mod = 1e9+7;
// ==========================(MOD)=============================================================================================== //
ll mod_add(ll aa,ll bb){ return ((aa%mod)+(bb%mod))%mod; }
ll mod_minus(ll aa,ll bb){ return (((aa%mod)-(bb%mod))+10*mod)%mod; }
ll mod_mul(ll aa,ll bb){ return ((aa%mod)*(bb%mod))%mod; }
ll mod_power(ll aa,ll bb){ aa%=mod; ll empowered = 1; bb%=mod-1; while(bb > 0){ if(bb & 1) empowered = mod_mul(empowered,aa); bb = bb >> 1; aa = mod_mul(aa,aa); } return empowered; }
ll mod_divi(ll aa,ll bb){ aa=mod_mul(aa,mod_power(bb,mod-2)); return aa; }
// ==========================(MOD)=============================================================================================== //
bool f = false;
ll sr,de;
typedef struct Disjoint_Set_Union{
ll sz[mx];
ll par[mx];
set<ll> st[mx];
queue<pi> q;
set<ll> adj[mx];
set<ll> radj[mx];
pi pt;
void init(ll k){
k += 5;
fip(1,k+1){
sz[i] = 1;
par[i] = i;
st[i].insert(i);
}
return;
}
ll find(ll k){
return ((par[k]==k)?k:par[k]=find(par[k]));
}
ll get(ll x){
return st[x].size() + adj[x].size() + radj[x].size();
}
void aa(ll u,ll v){
adj[u].insert(v);
radj[v].insert(u);
if(adj[v].find(u)!=adj[v].end())
q.push({u,v});
return;
}
void bb(ll u,ll v){
u = find(u);
v = find(v);
if(u==v)
return;
if(get(u) < get(v))
swap(u,v);
ans += sz[u] * st[v].size();
ans += sz[v] * st[u].size();
sz[u] += sz[v];
par[v] = u;
fx(st[v]){
if(st[u].find(x)!=st[u].end())
ans -= sz[u];
else
st[u].insert(x);
}
adj[u].erase(v);
adj[v].erase(u);
radj[u].erase(v);
radj[v].erase(u);
fx(adj[v]){
radj[x].erase(v);
aa(u,x);
}
fx(radj[v]){
adj[x].erase(v);
aa(x,u);
}
}
void unite(ll u,ll v){
v = find(v);
if(st[v].find(u)==st[v].end() && find(u)!=v){
st[v].insert(u);
u = find(u);
ans += sz[v];
aa(u,v);
while(q.size()){
pt = q.front();
q.pop();
bb(pt.ff,pt.ss);
}
}
}
}DSU;
DSU dsu;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input1.txt", "r", stdin);
// freopen("output1.txt", "w", stdout);
// freopen("error1.txt", "w", stderr);
// #endif // ONLINE_JUDGE
cin>>n>>m;
dsu.init(n);
ans = 0;
while(m--){
cin>>sr>>de;
dsu.unite(sr,de);
cout<<ans<<nli;
}
cerr << "Time elapsed: " << setprecision(6) << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
74332 KB |
Output is correct |
2 |
Correct |
15 ms |
74332 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Correct |
15 ms |
74332 KB |
Output is correct |
5 |
Correct |
15 ms |
74332 KB |
Output is correct |
6 |
Correct |
18 ms |
74328 KB |
Output is correct |
7 |
Correct |
15 ms |
74332 KB |
Output is correct |
8 |
Correct |
15 ms |
74492 KB |
Output is correct |
9 |
Correct |
17 ms |
74332 KB |
Output is correct |
10 |
Correct |
16 ms |
74332 KB |
Output is correct |
11 |
Correct |
15 ms |
74332 KB |
Output is correct |
12 |
Correct |
15 ms |
74332 KB |
Output is correct |
13 |
Correct |
15 ms |
74424 KB |
Output is correct |
14 |
Correct |
15 ms |
74332 KB |
Output is correct |
15 |
Correct |
15 ms |
74332 KB |
Output is correct |
16 |
Correct |
15 ms |
74332 KB |
Output is correct |
17 |
Correct |
15 ms |
74332 KB |
Output is correct |
18 |
Correct |
17 ms |
74328 KB |
Output is correct |
19 |
Correct |
15 ms |
74332 KB |
Output is correct |
20 |
Correct |
16 ms |
74332 KB |
Output is correct |
21 |
Correct |
17 ms |
74332 KB |
Output is correct |
22 |
Correct |
15 ms |
74268 KB |
Output is correct |
23 |
Correct |
19 ms |
74440 KB |
Output is correct |
24 |
Correct |
15 ms |
74328 KB |
Output is correct |
25 |
Correct |
16 ms |
74328 KB |
Output is correct |
26 |
Correct |
15 ms |
74332 KB |
Output is correct |
27 |
Correct |
16 ms |
74588 KB |
Output is correct |
28 |
Correct |
15 ms |
74332 KB |
Output is correct |
29 |
Correct |
16 ms |
74332 KB |
Output is correct |
30 |
Correct |
17 ms |
74328 KB |
Output is correct |
31 |
Correct |
17 ms |
74332 KB |
Output is correct |
32 |
Correct |
15 ms |
74332 KB |
Output is correct |
33 |
Correct |
16 ms |
74332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
74332 KB |
Output is correct |
2 |
Correct |
15 ms |
74332 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Correct |
15 ms |
74332 KB |
Output is correct |
5 |
Correct |
15 ms |
74332 KB |
Output is correct |
6 |
Correct |
18 ms |
74328 KB |
Output is correct |
7 |
Correct |
15 ms |
74332 KB |
Output is correct |
8 |
Correct |
15 ms |
74492 KB |
Output is correct |
9 |
Correct |
17 ms |
74332 KB |
Output is correct |
10 |
Correct |
16 ms |
74332 KB |
Output is correct |
11 |
Correct |
15 ms |
74332 KB |
Output is correct |
12 |
Correct |
15 ms |
74332 KB |
Output is correct |
13 |
Correct |
15 ms |
74424 KB |
Output is correct |
14 |
Correct |
15 ms |
74332 KB |
Output is correct |
15 |
Correct |
15 ms |
74332 KB |
Output is correct |
16 |
Correct |
15 ms |
74332 KB |
Output is correct |
17 |
Correct |
15 ms |
74332 KB |
Output is correct |
18 |
Correct |
17 ms |
74328 KB |
Output is correct |
19 |
Correct |
15 ms |
74332 KB |
Output is correct |
20 |
Correct |
16 ms |
74332 KB |
Output is correct |
21 |
Correct |
17 ms |
74332 KB |
Output is correct |
22 |
Correct |
15 ms |
74268 KB |
Output is correct |
23 |
Correct |
19 ms |
74440 KB |
Output is correct |
24 |
Correct |
15 ms |
74328 KB |
Output is correct |
25 |
Correct |
16 ms |
74328 KB |
Output is correct |
26 |
Correct |
15 ms |
74332 KB |
Output is correct |
27 |
Correct |
16 ms |
74588 KB |
Output is correct |
28 |
Correct |
15 ms |
74332 KB |
Output is correct |
29 |
Correct |
16 ms |
74332 KB |
Output is correct |
30 |
Correct |
17 ms |
74328 KB |
Output is correct |
31 |
Correct |
17 ms |
74332 KB |
Output is correct |
32 |
Correct |
15 ms |
74332 KB |
Output is correct |
33 |
Correct |
16 ms |
74332 KB |
Output is correct |
34 |
Correct |
18 ms |
74584 KB |
Output is correct |
35 |
Correct |
98 ms |
80468 KB |
Output is correct |
36 |
Correct |
118 ms |
83024 KB |
Output is correct |
37 |
Correct |
117 ms |
83280 KB |
Output is correct |
38 |
Correct |
129 ms |
83024 KB |
Output is correct |
39 |
Correct |
17 ms |
74584 KB |
Output is correct |
40 |
Correct |
19 ms |
75036 KB |
Output is correct |
41 |
Correct |
19 ms |
75100 KB |
Output is correct |
42 |
Correct |
17 ms |
74588 KB |
Output is correct |
43 |
Correct |
18 ms |
74808 KB |
Output is correct |
44 |
Correct |
18 ms |
74844 KB |
Output is correct |
45 |
Correct |
17 ms |
74588 KB |
Output is correct |
46 |
Correct |
18 ms |
74588 KB |
Output is correct |
47 |
Correct |
19 ms |
74796 KB |
Output is correct |
48 |
Correct |
18 ms |
74844 KB |
Output is correct |
49 |
Correct |
24 ms |
75612 KB |
Output is correct |
50 |
Correct |
148 ms |
83408 KB |
Output is correct |
51 |
Correct |
21 ms |
75096 KB |
Output is correct |
52 |
Correct |
107 ms |
81388 KB |
Output is correct |
53 |
Correct |
24 ms |
75608 KB |
Output is correct |
54 |
Correct |
113 ms |
82480 KB |
Output is correct |
55 |
Correct |
21 ms |
75100 KB |
Output is correct |
56 |
Correct |
21 ms |
75100 KB |
Output is correct |
57 |
Correct |
19 ms |
75096 KB |
Output is correct |
58 |
Correct |
20 ms |
75052 KB |
Output is correct |
59 |
Correct |
22 ms |
75100 KB |
Output is correct |
60 |
Correct |
92 ms |
80088 KB |
Output is correct |
61 |
Correct |
20 ms |
74844 KB |
Output is correct |
62 |
Correct |
117 ms |
82768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
74332 KB |
Output is correct |
2 |
Correct |
15 ms |
74332 KB |
Output is correct |
3 |
Correct |
17 ms |
74332 KB |
Output is correct |
4 |
Correct |
15 ms |
74332 KB |
Output is correct |
5 |
Correct |
15 ms |
74332 KB |
Output is correct |
6 |
Correct |
18 ms |
74328 KB |
Output is correct |
7 |
Correct |
15 ms |
74332 KB |
Output is correct |
8 |
Correct |
15 ms |
74492 KB |
Output is correct |
9 |
Correct |
17 ms |
74332 KB |
Output is correct |
10 |
Correct |
16 ms |
74332 KB |
Output is correct |
11 |
Correct |
15 ms |
74332 KB |
Output is correct |
12 |
Correct |
15 ms |
74332 KB |
Output is correct |
13 |
Correct |
15 ms |
74424 KB |
Output is correct |
14 |
Correct |
15 ms |
74332 KB |
Output is correct |
15 |
Correct |
15 ms |
74332 KB |
Output is correct |
16 |
Correct |
15 ms |
74332 KB |
Output is correct |
17 |
Correct |
15 ms |
74332 KB |
Output is correct |
18 |
Correct |
17 ms |
74328 KB |
Output is correct |
19 |
Correct |
15 ms |
74332 KB |
Output is correct |
20 |
Correct |
16 ms |
74332 KB |
Output is correct |
21 |
Correct |
17 ms |
74332 KB |
Output is correct |
22 |
Correct |
15 ms |
74268 KB |
Output is correct |
23 |
Correct |
19 ms |
74440 KB |
Output is correct |
24 |
Correct |
15 ms |
74328 KB |
Output is correct |
25 |
Correct |
16 ms |
74328 KB |
Output is correct |
26 |
Correct |
15 ms |
74332 KB |
Output is correct |
27 |
Correct |
16 ms |
74588 KB |
Output is correct |
28 |
Correct |
15 ms |
74332 KB |
Output is correct |
29 |
Correct |
16 ms |
74332 KB |
Output is correct |
30 |
Correct |
17 ms |
74328 KB |
Output is correct |
31 |
Correct |
17 ms |
74332 KB |
Output is correct |
32 |
Correct |
15 ms |
74332 KB |
Output is correct |
33 |
Correct |
16 ms |
74332 KB |
Output is correct |
34 |
Correct |
18 ms |
74584 KB |
Output is correct |
35 |
Correct |
98 ms |
80468 KB |
Output is correct |
36 |
Correct |
118 ms |
83024 KB |
Output is correct |
37 |
Correct |
117 ms |
83280 KB |
Output is correct |
38 |
Correct |
129 ms |
83024 KB |
Output is correct |
39 |
Correct |
17 ms |
74584 KB |
Output is correct |
40 |
Correct |
19 ms |
75036 KB |
Output is correct |
41 |
Correct |
19 ms |
75100 KB |
Output is correct |
42 |
Correct |
17 ms |
74588 KB |
Output is correct |
43 |
Correct |
18 ms |
74808 KB |
Output is correct |
44 |
Correct |
18 ms |
74844 KB |
Output is correct |
45 |
Correct |
17 ms |
74588 KB |
Output is correct |
46 |
Correct |
18 ms |
74588 KB |
Output is correct |
47 |
Correct |
19 ms |
74796 KB |
Output is correct |
48 |
Correct |
18 ms |
74844 KB |
Output is correct |
49 |
Correct |
24 ms |
75612 KB |
Output is correct |
50 |
Correct |
148 ms |
83408 KB |
Output is correct |
51 |
Correct |
21 ms |
75096 KB |
Output is correct |
52 |
Correct |
107 ms |
81388 KB |
Output is correct |
53 |
Correct |
24 ms |
75608 KB |
Output is correct |
54 |
Correct |
113 ms |
82480 KB |
Output is correct |
55 |
Correct |
21 ms |
75100 KB |
Output is correct |
56 |
Correct |
21 ms |
75100 KB |
Output is correct |
57 |
Correct |
19 ms |
75096 KB |
Output is correct |
58 |
Correct |
20 ms |
75052 KB |
Output is correct |
59 |
Correct |
22 ms |
75100 KB |
Output is correct |
60 |
Correct |
92 ms |
80088 KB |
Output is correct |
61 |
Correct |
20 ms |
74844 KB |
Output is correct |
62 |
Correct |
117 ms |
82768 KB |
Output is correct |
63 |
Correct |
547 ms |
130908 KB |
Output is correct |
64 |
Correct |
517 ms |
130960 KB |
Output is correct |
65 |
Correct |
526 ms |
131200 KB |
Output is correct |
66 |
Correct |
183 ms |
97216 KB |
Output is correct |
67 |
Correct |
384 ms |
123984 KB |
Output is correct |
68 |
Correct |
173 ms |
97064 KB |
Output is correct |
69 |
Correct |
320 ms |
98132 KB |
Output is correct |
70 |
Correct |
196 ms |
97104 KB |
Output is correct |
71 |
Correct |
215 ms |
97104 KB |
Output is correct |
72 |
Correct |
377 ms |
108116 KB |
Output is correct |
73 |
Correct |
368 ms |
109756 KB |
Output is correct |
74 |
Correct |
950 ms |
132200 KB |
Output is correct |
75 |
Correct |
472 ms |
105044 KB |
Output is correct |
76 |
Correct |
667 ms |
117060 KB |
Output is correct |
77 |
Correct |
680 ms |
117448 KB |
Output is correct |
78 |
Correct |
227 ms |
100808 KB |
Output is correct |
79 |
Correct |
375 ms |
105204 KB |
Output is correct |
80 |
Correct |
184 ms |
99668 KB |
Output is correct |
81 |
Correct |
326 ms |
103248 KB |
Output is correct |
82 |
Correct |
730 ms |
117936 KB |
Output is correct |
83 |
Correct |
699 ms |
118092 KB |
Output is correct |
84 |
Correct |
545 ms |
116452 KB |
Output is correct |
85 |
Correct |
530 ms |
116548 KB |
Output is correct |
86 |
Correct |
328 ms |
129928 KB |
Output is correct |
87 |
Correct |
327 ms |
132276 KB |
Output is correct |
88 |
Correct |
407 ms |
108924 KB |
Output is correct |
89 |
Correct |
692 ms |
114992 KB |
Output is correct |