#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const int MAXN = 100'009;
int cnt[MAXN];
int p[MAXN];
int sz[MAXN];
int sum[MAXN];
vector<int> V[MAXN];
map<int, int> in[MAXN];
map<int, int> out[MAXN];
set<int> In[MAXN];
set<int> Out[MAXN];
//set<int> in[MAXN];
//set<int> out[MAXN];
int ans;
int Find(int x) {
return x==p[x]?x:p[x]=Find(p[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if(a==b) return;
//cout << "before: " << sz[a] << " " << sum[a] << "\n";
//cout << "before: " << sz[b] << " " << sum[b] << "\n";
ans -= sz[a]*sz(In[a]);
ans -= sz[b]*sz(In[b]);
ans -= sz[a]*(sz[a]-1);
ans -= sz[b]*(sz[b]-1);
//cerr << "started ans: " << ans << "\n";
vector<int> union_queue;
if(sz(out[a])+sz(in[a])<sz(out[b])+sz(in[b])) swap(a, b);
//cerr << "SDOJFDSF: "<< a << " " << b << "\n";
for(int x:In[a]) {
//cerr << "? " << x << "\n";
}
for(int x:In[b]) {
//cerr << "! " << x << "\n";
}
for(pii x:out[b]) {
if(x.st==a) continue;
if(in[a].count(x.st)) {
union_queue.pb(x.st);
}
out[a][x.st]+=x.nd;
//out[a].insert(x);
in[x.st][a] += in[x.st][b];
in[x.st].erase(b);
//in[x].erase(b);
//in[x].insert(a);
}
for(int x:In[b]) {
if(Find(x)!=a) {
In[a].insert(x);
}
}
for(int x:V[b]) {
In[a].erase(x);
V[a].pb(b);
}
V[b].clear();
out[b].clear();
for(pii x:in[b]) {
if(x.st==a) continue;
if(out[a].count(x.st)) {
union_queue.pb(x.st);
}
in[a][x.st]+=x.nd;
//cerr << "some susus\n";
//cerr << x.st << " " << x.nd << "\n";
sum[a]+=x.nd;
//in[a].insert(x);
out[x.st][a] += out[x.st][b];
out[x.st].erase(b);
//out[x].erase(b);
//out[x].insert(a);
}
in[b].clear();
p[b] = a;
sz[a] += sz[b];
//cerr <<" here : " << ans << "\n";
ans += sz[a]*(sz[a]-1);
ans += sz[a]*sz(In[a]);
for(int x:In[a]) {
//cout << "-> " << x << "\n";
}
//cerr << "here: " << ans << "\n";
//cerr << "after: " << sz[a] << " " << sz(In[a]) << "\n";
for(int x:union_queue) {
Union(a, x);
}
}
set<pii> S;
set<int> used[MAXN];
int32_t main() {
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n, m;
cin >> n >> m;
iota(p, p+n+1, 0);
fill(sz, sz+n+1, 1);
for(int i=1;i<=n;i++) {
V[i].pb(i);
}
for(int i=0;i<m;i++) {
int a, b;
cin >> a >> b;
S.insert({a, b});
int A = Find(a);
int B = Find(b);
if(A==B) goto skip;
//out[A].insert(B);
out[A][B]++;
//in[B].insert(A);
if(in[A].count(B)) {
Union(A, B);
} else {
//cerr << "HERE\n";
ans -= sz[B]*sz(In[B]);
in[B][A]++;
sum[B]++;
In[B].insert(a);
Out[a].insert(B);
//cerr << "new sum: "<< B << " " << sum[B] << "\n";
ans += sz[B]*sz(In[B]);
}
skip:
//int cnt = 0;
//for(int i=1;i<=n;i++) {
//used[i].clear();
//}
//for(pii x:S) {
//if(Find(x.st)==Find(x.nd)) continue;
//if(used[x.st].count(Find(x.nd))) continue;
//cnt += sz[Find(x.nd)];
//used[x.st].insert(Find(x.nd));
//}
//cerr << ans << " " << cnt << "\n";
cout << ans << "\n";
}
}
Compilation message
joitter2.cpp: In function 'void Union(int64_t, int64_t)':
joitter2.cpp:45:10: warning: unused variable 'x' [-Wunused-variable]
45 | for(int x:In[a]) {
| ^
joitter2.cpp:48:10: warning: unused variable 'x' [-Wunused-variable]
48 | for(int x:In[b]) {
| ^
joitter2.cpp:96:10: warning: unused variable 'x' [-Wunused-variable]
96 | for(int x:In[a]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29108 KB |
Output is correct |
3 |
Correct |
6 ms |
29272 KB |
Output is correct |
4 |
Correct |
9 ms |
29272 KB |
Output is correct |
5 |
Correct |
8 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
10 ms |
29272 KB |
Output is correct |
8 |
Correct |
10 ms |
29528 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29276 KB |
Output is correct |
11 |
Incorrect |
6 ms |
29272 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29108 KB |
Output is correct |
3 |
Correct |
6 ms |
29272 KB |
Output is correct |
4 |
Correct |
9 ms |
29272 KB |
Output is correct |
5 |
Correct |
8 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
10 ms |
29272 KB |
Output is correct |
8 |
Correct |
10 ms |
29528 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29276 KB |
Output is correct |
11 |
Incorrect |
6 ms |
29272 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29108 KB |
Output is correct |
3 |
Correct |
6 ms |
29272 KB |
Output is correct |
4 |
Correct |
9 ms |
29272 KB |
Output is correct |
5 |
Correct |
8 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29272 KB |
Output is correct |
7 |
Correct |
10 ms |
29272 KB |
Output is correct |
8 |
Correct |
10 ms |
29528 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29276 KB |
Output is correct |
11 |
Incorrect |
6 ms |
29272 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |