#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const int MAX_N = 1e5 + 10;
ll par[MAX_N], sz[MAX_N];
set<pair<int, int>> perr[MAX_N];
set<pair<int, int> > between;
set<int> toClique[MAX_N];
ll ans, in[MAX_N];
int find(int x) {
if(x == par[x]) {
return x;
} else {
return par[x] = find(par[x]);
}
}
void remove(int x, int y) {
ll compx = find(x), compy = find(y);
if(compx == compy) {return;}
//cout << "Remove " << x << " " << y << " " << compx << " " << compy << endl;
if(between.find({compx, compy}) == between.end()) {
between.erase({compx, compy});
}
perr[compx].erase({x, y});
perr[compy].erase({x, y});
ans -= sz[compy];
in[compy] --;
toClique[x].erase(compy);
}
void add(int x, int y) {
ll compx = find(x), compy = find(y);
if(compx == compy) {return;}
if(toClique[x].find(compy) != toClique[x].end()) {return;}
between.insert({compx, compy});
perr[compx].insert({x, y});
perr[compy].insert({x, y});
ans += sz[compy];
in[compy] ++;
toClique[x].insert(compy);
}
stack<pair<int, int> > st;
void merge(int x, int y) {
if(x == y) {return;}
ans -= (sz[x] * sz[x] - sz[x]);
ans -= (sz[y] * sz[y] - sz[y]);
if(perr[x].size() + sz[x] < perr[y].size() + sz[y]) {swap(x, y);}
vector<pair<int, int> > toMove;
for(auto it : perr[y]) {toMove.push_back(it);}
perr[y].clear();
for(auto it : toMove) {
remove(it.first, it.second);
st.push(it);
}
ans += in[x] * sz[y];
par[y] = x;
sz[x] += sz[y];
ans += (sz[x] * sz[x] - sz[x]);
}
void fixStack() {
while(!st.empty()) {
auto curr = st.top(); st.pop();
pair<int, int> comp = {find(curr.first), find(curr.second)};
if(comp.first == comp.second) {continue;}
if(between.find({comp.second, comp.first}) != between.end()) {
merge(comp.first, comp.second);
} else {
add(curr.first, curr.second);
}
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
for(int i = 0; i < MAX_N; i ++) {par[i] = i; sz[i] = 1;}
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i ++) {
int a, b;
cin >> a >> b;
st.push({a, b});
fixStack();
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11244 KB |
Output is correct |
2 |
Correct |
7 ms |
11244 KB |
Output is correct |
3 |
Correct |
7 ms |
11244 KB |
Output is correct |
4 |
Correct |
7 ms |
11244 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11392 KB |
Output is correct |
7 |
Correct |
8 ms |
11372 KB |
Output is correct |
8 |
Correct |
8 ms |
11372 KB |
Output is correct |
9 |
Correct |
8 ms |
11372 KB |
Output is correct |
10 |
Correct |
7 ms |
11244 KB |
Output is correct |
11 |
Correct |
7 ms |
11244 KB |
Output is correct |
12 |
Correct |
7 ms |
11244 KB |
Output is correct |
13 |
Correct |
7 ms |
11244 KB |
Output is correct |
14 |
Correct |
7 ms |
11244 KB |
Output is correct |
15 |
Correct |
8 ms |
11244 KB |
Output is correct |
16 |
Correct |
7 ms |
11244 KB |
Output is correct |
17 |
Correct |
7 ms |
11244 KB |
Output is correct |
18 |
Correct |
8 ms |
11244 KB |
Output is correct |
19 |
Correct |
7 ms |
11244 KB |
Output is correct |
20 |
Correct |
8 ms |
11392 KB |
Output is correct |
21 |
Correct |
9 ms |
11516 KB |
Output is correct |
22 |
Correct |
7 ms |
11372 KB |
Output is correct |
23 |
Correct |
8 ms |
11500 KB |
Output is correct |
24 |
Correct |
9 ms |
11372 KB |
Output is correct |
25 |
Correct |
8 ms |
11372 KB |
Output is correct |
26 |
Correct |
8 ms |
11372 KB |
Output is correct |
27 |
Correct |
8 ms |
11372 KB |
Output is correct |
28 |
Correct |
7 ms |
11372 KB |
Output is correct |
29 |
Correct |
8 ms |
11372 KB |
Output is correct |
30 |
Correct |
7 ms |
11244 KB |
Output is correct |
31 |
Correct |
9 ms |
11372 KB |
Output is correct |
32 |
Correct |
8 ms |
11244 KB |
Output is correct |
33 |
Correct |
8 ms |
11500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11244 KB |
Output is correct |
2 |
Correct |
7 ms |
11244 KB |
Output is correct |
3 |
Correct |
7 ms |
11244 KB |
Output is correct |
4 |
Correct |
7 ms |
11244 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11392 KB |
Output is correct |
7 |
Correct |
8 ms |
11372 KB |
Output is correct |
8 |
Correct |
8 ms |
11372 KB |
Output is correct |
9 |
Correct |
8 ms |
11372 KB |
Output is correct |
10 |
Correct |
7 ms |
11244 KB |
Output is correct |
11 |
Correct |
7 ms |
11244 KB |
Output is correct |
12 |
Correct |
7 ms |
11244 KB |
Output is correct |
13 |
Correct |
7 ms |
11244 KB |
Output is correct |
14 |
Correct |
7 ms |
11244 KB |
Output is correct |
15 |
Correct |
8 ms |
11244 KB |
Output is correct |
16 |
Correct |
7 ms |
11244 KB |
Output is correct |
17 |
Correct |
7 ms |
11244 KB |
Output is correct |
18 |
Correct |
8 ms |
11244 KB |
Output is correct |
19 |
Correct |
7 ms |
11244 KB |
Output is correct |
20 |
Correct |
8 ms |
11392 KB |
Output is correct |
21 |
Correct |
9 ms |
11516 KB |
Output is correct |
22 |
Correct |
7 ms |
11372 KB |
Output is correct |
23 |
Correct |
8 ms |
11500 KB |
Output is correct |
24 |
Correct |
9 ms |
11372 KB |
Output is correct |
25 |
Correct |
8 ms |
11372 KB |
Output is correct |
26 |
Correct |
8 ms |
11372 KB |
Output is correct |
27 |
Correct |
8 ms |
11372 KB |
Output is correct |
28 |
Correct |
7 ms |
11372 KB |
Output is correct |
29 |
Correct |
8 ms |
11372 KB |
Output is correct |
30 |
Correct |
7 ms |
11244 KB |
Output is correct |
31 |
Correct |
9 ms |
11372 KB |
Output is correct |
32 |
Correct |
8 ms |
11244 KB |
Output is correct |
33 |
Correct |
8 ms |
11500 KB |
Output is correct |
34 |
Correct |
11 ms |
11372 KB |
Output is correct |
35 |
Correct |
108 ms |
15212 KB |
Output is correct |
36 |
Correct |
151 ms |
20588 KB |
Output is correct |
37 |
Correct |
151 ms |
21100 KB |
Output is correct |
38 |
Correct |
144 ms |
20608 KB |
Output is correct |
39 |
Correct |
11 ms |
11500 KB |
Output is correct |
40 |
Correct |
12 ms |
11628 KB |
Output is correct |
41 |
Correct |
12 ms |
11628 KB |
Output is correct |
42 |
Correct |
10 ms |
11500 KB |
Output is correct |
43 |
Correct |
11 ms |
11628 KB |
Output is correct |
44 |
Correct |
11 ms |
11628 KB |
Output is correct |
45 |
Correct |
10 ms |
11500 KB |
Output is correct |
46 |
Correct |
10 ms |
11500 KB |
Output is correct |
47 |
Correct |
12 ms |
11648 KB |
Output is correct |
48 |
Correct |
12 ms |
11628 KB |
Output is correct |
49 |
Correct |
24 ms |
12652 KB |
Output is correct |
50 |
Correct |
155 ms |
21100 KB |
Output is correct |
51 |
Correct |
17 ms |
12012 KB |
Output is correct |
52 |
Correct |
121 ms |
18924 KB |
Output is correct |
53 |
Correct |
24 ms |
12524 KB |
Output is correct |
54 |
Correct |
134 ms |
19948 KB |
Output is correct |
55 |
Correct |
16 ms |
12140 KB |
Output is correct |
56 |
Correct |
16 ms |
12140 KB |
Output is correct |
57 |
Correct |
16 ms |
12140 KB |
Output is correct |
58 |
Correct |
15 ms |
12140 KB |
Output is correct |
59 |
Correct |
10 ms |
11500 KB |
Output is correct |
60 |
Correct |
100 ms |
16492 KB |
Output is correct |
61 |
Correct |
14 ms |
11756 KB |
Output is correct |
62 |
Correct |
145 ms |
20332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11244 KB |
Output is correct |
2 |
Correct |
7 ms |
11244 KB |
Output is correct |
3 |
Correct |
7 ms |
11244 KB |
Output is correct |
4 |
Correct |
7 ms |
11244 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11392 KB |
Output is correct |
7 |
Correct |
8 ms |
11372 KB |
Output is correct |
8 |
Correct |
8 ms |
11372 KB |
Output is correct |
9 |
Correct |
8 ms |
11372 KB |
Output is correct |
10 |
Correct |
7 ms |
11244 KB |
Output is correct |
11 |
Correct |
7 ms |
11244 KB |
Output is correct |
12 |
Correct |
7 ms |
11244 KB |
Output is correct |
13 |
Correct |
7 ms |
11244 KB |
Output is correct |
14 |
Correct |
7 ms |
11244 KB |
Output is correct |
15 |
Correct |
8 ms |
11244 KB |
Output is correct |
16 |
Correct |
7 ms |
11244 KB |
Output is correct |
17 |
Correct |
7 ms |
11244 KB |
Output is correct |
18 |
Correct |
8 ms |
11244 KB |
Output is correct |
19 |
Correct |
7 ms |
11244 KB |
Output is correct |
20 |
Correct |
8 ms |
11392 KB |
Output is correct |
21 |
Correct |
9 ms |
11516 KB |
Output is correct |
22 |
Correct |
7 ms |
11372 KB |
Output is correct |
23 |
Correct |
8 ms |
11500 KB |
Output is correct |
24 |
Correct |
9 ms |
11372 KB |
Output is correct |
25 |
Correct |
8 ms |
11372 KB |
Output is correct |
26 |
Correct |
8 ms |
11372 KB |
Output is correct |
27 |
Correct |
8 ms |
11372 KB |
Output is correct |
28 |
Correct |
7 ms |
11372 KB |
Output is correct |
29 |
Correct |
8 ms |
11372 KB |
Output is correct |
30 |
Correct |
7 ms |
11244 KB |
Output is correct |
31 |
Correct |
9 ms |
11372 KB |
Output is correct |
32 |
Correct |
8 ms |
11244 KB |
Output is correct |
33 |
Correct |
8 ms |
11500 KB |
Output is correct |
34 |
Correct |
11 ms |
11372 KB |
Output is correct |
35 |
Correct |
108 ms |
15212 KB |
Output is correct |
36 |
Correct |
151 ms |
20588 KB |
Output is correct |
37 |
Correct |
151 ms |
21100 KB |
Output is correct |
38 |
Correct |
144 ms |
20608 KB |
Output is correct |
39 |
Correct |
11 ms |
11500 KB |
Output is correct |
40 |
Correct |
12 ms |
11628 KB |
Output is correct |
41 |
Correct |
12 ms |
11628 KB |
Output is correct |
42 |
Correct |
10 ms |
11500 KB |
Output is correct |
43 |
Correct |
11 ms |
11628 KB |
Output is correct |
44 |
Correct |
11 ms |
11628 KB |
Output is correct |
45 |
Correct |
10 ms |
11500 KB |
Output is correct |
46 |
Correct |
10 ms |
11500 KB |
Output is correct |
47 |
Correct |
12 ms |
11648 KB |
Output is correct |
48 |
Correct |
12 ms |
11628 KB |
Output is correct |
49 |
Correct |
24 ms |
12652 KB |
Output is correct |
50 |
Correct |
155 ms |
21100 KB |
Output is correct |
51 |
Correct |
17 ms |
12012 KB |
Output is correct |
52 |
Correct |
121 ms |
18924 KB |
Output is correct |
53 |
Correct |
24 ms |
12524 KB |
Output is correct |
54 |
Correct |
134 ms |
19948 KB |
Output is correct |
55 |
Correct |
16 ms |
12140 KB |
Output is correct |
56 |
Correct |
16 ms |
12140 KB |
Output is correct |
57 |
Correct |
16 ms |
12140 KB |
Output is correct |
58 |
Correct |
15 ms |
12140 KB |
Output is correct |
59 |
Correct |
10 ms |
11500 KB |
Output is correct |
60 |
Correct |
100 ms |
16492 KB |
Output is correct |
61 |
Correct |
14 ms |
11756 KB |
Output is correct |
62 |
Correct |
145 ms |
20332 KB |
Output is correct |
63 |
Correct |
1115 ms |
74092 KB |
Output is correct |
64 |
Correct |
1122 ms |
74092 KB |
Output is correct |
65 |
Correct |
1115 ms |
74092 KB |
Output is correct |
66 |
Correct |
212 ms |
21228 KB |
Output is correct |
67 |
Correct |
606 ms |
27116 KB |
Output is correct |
68 |
Correct |
205 ms |
21228 KB |
Output is correct |
69 |
Correct |
508 ms |
27244 KB |
Output is correct |
70 |
Correct |
218 ms |
21228 KB |
Output is correct |
71 |
Correct |
215 ms |
21228 KB |
Output is correct |
72 |
Correct |
659 ms |
27500 KB |
Output is correct |
73 |
Correct |
642 ms |
27500 KB |
Output is correct |
74 |
Correct |
2000 ms |
52828 KB |
Output is correct |
75 |
Correct |
916 ms |
35436 KB |
Output is correct |
76 |
Correct |
1467 ms |
44792 KB |
Output is correct |
77 |
Correct |
1441 ms |
44652 KB |
Output is correct |
78 |
Correct |
342 ms |
28496 KB |
Output is correct |
79 |
Correct |
664 ms |
32572 KB |
Output is correct |
80 |
Correct |
282 ms |
28268 KB |
Output is correct |
81 |
Correct |
463 ms |
31548 KB |
Output is correct |
82 |
Correct |
1321 ms |
53512 KB |
Output is correct |
83 |
Correct |
1330 ms |
53580 KB |
Output is correct |
84 |
Correct |
1303 ms |
53228 KB |
Output is correct |
85 |
Correct |
1264 ms |
53356 KB |
Output is correct |
86 |
Correct |
250 ms |
20556 KB |
Output is correct |
87 |
Correct |
271 ms |
22764 KB |
Output is correct |
88 |
Correct |
672 ms |
27596 KB |
Output is correct |
89 |
Correct |
1383 ms |
43116 KB |
Output is correct |