//https://oj.uz/problem/view/JOI20_joitter2
// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
//#include "bits/stdc++.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"---------------------------\n"
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
a+=b;
a+=mod;
if(a>=mod) a%=mod;
return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 100005
ll n,m,ans = 0;
ll dsu[maxn];
ll sizi[maxn];
set<ll> g[maxn],rg[maxn],v[maxn];
queue<pll> tomrg;
ll root(ll x){
while(x!=dsu[x]) x = dsu[x];
return x;
}
ll dsu_siz(ll x){x = root(x);return sz(g[x])+sz(rg[x])+sz(v[x]);}
void updweak(ll x,ll y){
g[x].insert(y);
rg[y].insert(x);
if(g[y].count(x)) tomrg.push({x,y});
}
void mrg(ll x,ll y){
if(x==y) return;
if(dsu_siz(x)<dsu_siz(y)) swap(x,y);
ans+=sizi[x]*sz(v[y]) + sizi[y]*sz(v[x]);
dsu[y] = x;
sizi[x]+=sizi[y];
for(ll z : v[y]){
if(v[x].count(z)) ans-=sizi[x];
else v[x].insert(z);
}
rg[x].erase(y); g[x].erase(y);
rg[y].erase(x); g[y].erase(x);
for(ll z : g[y]){
rg[y].erase(z);
updweak(x,z);
}
for(ll z : rg[y]){
rg[x].erase(z);
updweak(z,x);
}
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> m;
iota(dsu+1,dsu+n+1,1);
fill(sizi+1,sizi+n+1,1);
for(ll i = 1;i<=n;i++) v[i].insert(i);
for(ll ii = 1;ii<=m;ii++){
ll x,y; cin >> x >> y;
y = root(y);
if(root(x)!=y&&!v[y].count(x)){
v[y].insert(x);
ans+=sizi[y];
x = root(x);
updweak(x,y);
}
while(!tomrg.empty()){
pll p = tomrg.front();
tomrg.pop();
x = p.fi;
y = p.sc;
mrg(root(x),root(y));
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
joitter2.cpp: In function 'void setIO(std::string)':
joitter2.cpp:68:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:69:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14344 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14432 KB |
Output is correct |
22 |
Correct |
7 ms |
14412 KB |
Output is correct |
23 |
Correct |
7 ms |
14412 KB |
Output is correct |
24 |
Correct |
7 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
8 ms |
14412 KB |
Output is correct |
27 |
Correct |
9 ms |
14344 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
7 ms |
14312 KB |
Output is correct |
33 |
Correct |
8 ms |
14416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14344 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14432 KB |
Output is correct |
22 |
Correct |
7 ms |
14412 KB |
Output is correct |
23 |
Correct |
7 ms |
14412 KB |
Output is correct |
24 |
Correct |
7 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
8 ms |
14412 KB |
Output is correct |
27 |
Correct |
9 ms |
14344 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
7 ms |
14312 KB |
Output is correct |
33 |
Correct |
8 ms |
14416 KB |
Output is correct |
34 |
Correct |
9 ms |
14540 KB |
Output is correct |
35 |
Correct |
76 ms |
20536 KB |
Output is correct |
36 |
Correct |
112 ms |
23956 KB |
Output is correct |
37 |
Correct |
108 ms |
23980 KB |
Output is correct |
38 |
Correct |
102 ms |
23396 KB |
Output is correct |
39 |
Correct |
9 ms |
14796 KB |
Output is correct |
40 |
Correct |
12 ms |
15600 KB |
Output is correct |
41 |
Correct |
13 ms |
15660 KB |
Output is correct |
42 |
Correct |
9 ms |
14796 KB |
Output is correct |
43 |
Correct |
10 ms |
14796 KB |
Output is correct |
44 |
Correct |
10 ms |
14788 KB |
Output is correct |
45 |
Correct |
10 ms |
14796 KB |
Output is correct |
46 |
Correct |
10 ms |
14796 KB |
Output is correct |
47 |
Correct |
11 ms |
15068 KB |
Output is correct |
48 |
Correct |
12 ms |
15180 KB |
Output is correct |
49 |
Correct |
20 ms |
16204 KB |
Output is correct |
50 |
Correct |
111 ms |
24120 KB |
Output is correct |
51 |
Correct |
17 ms |
15316 KB |
Output is correct |
52 |
Correct |
88 ms |
21992 KB |
Output is correct |
53 |
Correct |
18 ms |
15948 KB |
Output is correct |
54 |
Correct |
112 ms |
22868 KB |
Output is correct |
55 |
Correct |
14 ms |
15436 KB |
Output is correct |
56 |
Correct |
14 ms |
15364 KB |
Output is correct |
57 |
Correct |
14 ms |
15448 KB |
Output is correct |
58 |
Correct |
12 ms |
15436 KB |
Output is correct |
59 |
Correct |
10 ms |
15180 KB |
Output is correct |
60 |
Correct |
81 ms |
20064 KB |
Output is correct |
61 |
Correct |
13 ms |
15196 KB |
Output is correct |
62 |
Correct |
126 ms |
23336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14412 KB |
Output is correct |
5 |
Correct |
7 ms |
14344 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
7 ms |
14336 KB |
Output is correct |
14 |
Correct |
7 ms |
14416 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14412 KB |
Output is correct |
20 |
Correct |
7 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14432 KB |
Output is correct |
22 |
Correct |
7 ms |
14412 KB |
Output is correct |
23 |
Correct |
7 ms |
14412 KB |
Output is correct |
24 |
Correct |
7 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
8 ms |
14412 KB |
Output is correct |
27 |
Correct |
9 ms |
14344 KB |
Output is correct |
28 |
Correct |
7 ms |
14412 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
7 ms |
14312 KB |
Output is correct |
33 |
Correct |
8 ms |
14416 KB |
Output is correct |
34 |
Correct |
9 ms |
14540 KB |
Output is correct |
35 |
Correct |
76 ms |
20536 KB |
Output is correct |
36 |
Correct |
112 ms |
23956 KB |
Output is correct |
37 |
Correct |
108 ms |
23980 KB |
Output is correct |
38 |
Correct |
102 ms |
23396 KB |
Output is correct |
39 |
Correct |
9 ms |
14796 KB |
Output is correct |
40 |
Correct |
12 ms |
15600 KB |
Output is correct |
41 |
Correct |
13 ms |
15660 KB |
Output is correct |
42 |
Correct |
9 ms |
14796 KB |
Output is correct |
43 |
Correct |
10 ms |
14796 KB |
Output is correct |
44 |
Correct |
10 ms |
14788 KB |
Output is correct |
45 |
Correct |
10 ms |
14796 KB |
Output is correct |
46 |
Correct |
10 ms |
14796 KB |
Output is correct |
47 |
Correct |
11 ms |
15068 KB |
Output is correct |
48 |
Correct |
12 ms |
15180 KB |
Output is correct |
49 |
Correct |
20 ms |
16204 KB |
Output is correct |
50 |
Correct |
111 ms |
24120 KB |
Output is correct |
51 |
Correct |
17 ms |
15316 KB |
Output is correct |
52 |
Correct |
88 ms |
21992 KB |
Output is correct |
53 |
Correct |
18 ms |
15948 KB |
Output is correct |
54 |
Correct |
112 ms |
22868 KB |
Output is correct |
55 |
Correct |
14 ms |
15436 KB |
Output is correct |
56 |
Correct |
14 ms |
15364 KB |
Output is correct |
57 |
Correct |
14 ms |
15448 KB |
Output is correct |
58 |
Correct |
12 ms |
15436 KB |
Output is correct |
59 |
Correct |
10 ms |
15180 KB |
Output is correct |
60 |
Correct |
81 ms |
20064 KB |
Output is correct |
61 |
Correct |
13 ms |
15196 KB |
Output is correct |
62 |
Correct |
126 ms |
23336 KB |
Output is correct |
63 |
Correct |
498 ms |
68372 KB |
Output is correct |
64 |
Correct |
490 ms |
68420 KB |
Output is correct |
65 |
Correct |
499 ms |
68388 KB |
Output is correct |
66 |
Correct |
183 ms |
34384 KB |
Output is correct |
67 |
Correct |
690 ms |
97444 KB |
Output is correct |
68 |
Correct |
183 ms |
34368 KB |
Output is correct |
69 |
Correct |
364 ms |
35560 KB |
Output is correct |
70 |
Correct |
218 ms |
34404 KB |
Output is correct |
71 |
Correct |
213 ms |
34384 KB |
Output is correct |
72 |
Correct |
515 ms |
54272 KB |
Output is correct |
73 |
Correct |
542 ms |
56860 KB |
Output is correct |
74 |
Correct |
1547 ms |
109168 KB |
Output is correct |
75 |
Correct |
615 ms |
46788 KB |
Output is correct |
76 |
Correct |
1000 ms |
71324 KB |
Output is correct |
77 |
Correct |
1052 ms |
71592 KB |
Output is correct |
78 |
Correct |
240 ms |
41476 KB |
Output is correct |
79 |
Correct |
420 ms |
47316 KB |
Output is correct |
80 |
Correct |
215 ms |
40388 KB |
Output is correct |
81 |
Correct |
360 ms |
44612 KB |
Output is correct |
82 |
Correct |
1004 ms |
66756 KB |
Output is correct |
83 |
Correct |
976 ms |
66768 KB |
Output is correct |
84 |
Correct |
801 ms |
69640 KB |
Output is correct |
85 |
Correct |
812 ms |
69648 KB |
Output is correct |
86 |
Correct |
297 ms |
67232 KB |
Output is correct |
87 |
Correct |
329 ms |
69432 KB |
Output is correct |
88 |
Correct |
541 ms |
55460 KB |
Output is correct |
89 |
Correct |
962 ms |
68772 KB |
Output is correct |