#define DBG 1
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (DBG) cerr << #x << " = " << x << endl;
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)1e5 + 5;
set<int> in[MAXN], out[MAXN], inv[MAXN];
vector<int> ver[MAXN];
int par[MAXN];
pii Q[MAXN];
int n, m, t;
ll ans;
int getPar(int x) {
if (x == par[x]) {
return x;
}
return par[x] = getPar(par[x]);
}
ll f(int x) {
return ver[x].size() * (ver[x].size() - 1ll + inv[x].size());
}
void uni(int a, int b) {
a = getPar(a);
b = getPar(b);
if (a == b) {
return;
}
ans -= f(b);
ans -= f(a);
if (inv[a].size() + ver[a].size() + in[a].size() + out[a].size() < inv[b].size() + ver[b].size() + in[b].size() + out[b].size()) {
swap(a, b);
}
for (int x : inv[b]) {
if (getPar(x) != a) {
inv[a].insert(x);
}
}
inv[b].clear();
for (int x : ver[b]) {
inv[a].erase(x);
ver[a].pb(x);
}
in[a].erase(b);
out[a].erase(b);
in[b].erase(a);
out[b].erase(a);
for (int x : out[b]) {
in[x].erase(b);
in[x].insert(a);
out[a].insert(x);
if (in[a].find(x) != in[a].end()) {
Q[t++] = mp(a, x);
}
}
out[b].clear();
for (int x : in[b]) {
out[x].erase(b);
out[x].insert(a);
in[a].insert(x);
if (out[a].find(x) != out[a].end()) {
Q[t++] = mp(a, x);
}
}
in[b].clear();
par[b] = a;
ans += f(a);
}
void push() {
for (int i = 0; i < t; ++i) {
uni(Q[i].fi, Q[i].se);
}
}
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) {
par[i] = i;
ver[i].pb(i);
}
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
v = getPar(v);
if (getPar(u) != v) {
ans -= f(v);
inv[v].insert(u);
u = getPar(u);
out[u].insert(v);
in[v].insert(u);
ans += f(v);
if (in[u].find(v) != in[u].end()) {
t = 0;
Q[t++] = mp(u, v);
push();
}
}
printf("%lld\n", ans);
}
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
joitter2.cpp: In function 'void solve()':
joitter2.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
20 |
Correct |
17 ms |
16896 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
15 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
17 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
20 |
Correct |
17 ms |
16896 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
15 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
17 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16768 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
140 ms |
20192 KB |
Output is correct |
36 |
Correct |
171 ms |
22784 KB |
Output is correct |
37 |
Correct |
161 ms |
22776 KB |
Output is correct |
38 |
Correct |
166 ms |
22468 KB |
Output is correct |
39 |
Correct |
17 ms |
16896 KB |
Output is correct |
40 |
Correct |
18 ms |
17024 KB |
Output is correct |
41 |
Correct |
18 ms |
17024 KB |
Output is correct |
42 |
Correct |
17 ms |
16896 KB |
Output is correct |
43 |
Correct |
18 ms |
17024 KB |
Output is correct |
44 |
Correct |
18 ms |
17024 KB |
Output is correct |
45 |
Correct |
17 ms |
16896 KB |
Output is correct |
46 |
Correct |
17 ms |
16896 KB |
Output is correct |
47 |
Correct |
19 ms |
17024 KB |
Output is correct |
48 |
Correct |
18 ms |
17024 KB |
Output is correct |
49 |
Correct |
27 ms |
17656 KB |
Output is correct |
50 |
Correct |
169 ms |
22776 KB |
Output is correct |
51 |
Correct |
23 ms |
17280 KB |
Output is correct |
52 |
Correct |
148 ms |
21112 KB |
Output is correct |
53 |
Correct |
26 ms |
17660 KB |
Output is correct |
54 |
Correct |
157 ms |
22008 KB |
Output is correct |
55 |
Correct |
21 ms |
17408 KB |
Output is correct |
56 |
Correct |
21 ms |
17408 KB |
Output is correct |
57 |
Correct |
22 ms |
17408 KB |
Output is correct |
58 |
Correct |
20 ms |
17408 KB |
Output is correct |
59 |
Correct |
17 ms |
16896 KB |
Output is correct |
60 |
Correct |
136 ms |
19320 KB |
Output is correct |
61 |
Correct |
19 ms |
17024 KB |
Output is correct |
62 |
Correct |
159 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
16 ms |
16768 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
15 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
20 |
Correct |
17 ms |
16896 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
15 ms |
16768 KB |
Output is correct |
25 |
Correct |
15 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
17 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16768 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
140 ms |
20192 KB |
Output is correct |
36 |
Correct |
171 ms |
22784 KB |
Output is correct |
37 |
Correct |
161 ms |
22776 KB |
Output is correct |
38 |
Correct |
166 ms |
22468 KB |
Output is correct |
39 |
Correct |
17 ms |
16896 KB |
Output is correct |
40 |
Correct |
18 ms |
17024 KB |
Output is correct |
41 |
Correct |
18 ms |
17024 KB |
Output is correct |
42 |
Correct |
17 ms |
16896 KB |
Output is correct |
43 |
Correct |
18 ms |
17024 KB |
Output is correct |
44 |
Correct |
18 ms |
17024 KB |
Output is correct |
45 |
Correct |
17 ms |
16896 KB |
Output is correct |
46 |
Correct |
17 ms |
16896 KB |
Output is correct |
47 |
Correct |
19 ms |
17024 KB |
Output is correct |
48 |
Correct |
18 ms |
17024 KB |
Output is correct |
49 |
Correct |
27 ms |
17656 KB |
Output is correct |
50 |
Correct |
169 ms |
22776 KB |
Output is correct |
51 |
Correct |
23 ms |
17280 KB |
Output is correct |
52 |
Correct |
148 ms |
21112 KB |
Output is correct |
53 |
Correct |
26 ms |
17660 KB |
Output is correct |
54 |
Correct |
157 ms |
22008 KB |
Output is correct |
55 |
Correct |
21 ms |
17408 KB |
Output is correct |
56 |
Correct |
21 ms |
17408 KB |
Output is correct |
57 |
Correct |
22 ms |
17408 KB |
Output is correct |
58 |
Correct |
20 ms |
17408 KB |
Output is correct |
59 |
Correct |
17 ms |
16896 KB |
Output is correct |
60 |
Correct |
136 ms |
19320 KB |
Output is correct |
61 |
Correct |
19 ms |
17024 KB |
Output is correct |
62 |
Correct |
159 ms |
22264 KB |
Output is correct |
63 |
Correct |
626 ms |
64504 KB |
Output is correct |
64 |
Correct |
627 ms |
64552 KB |
Output is correct |
65 |
Correct |
634 ms |
64632 KB |
Output is correct |
66 |
Correct |
190 ms |
22900 KB |
Output is correct |
67 |
Correct |
355 ms |
30704 KB |
Output is correct |
68 |
Correct |
187 ms |
22900 KB |
Output is correct |
69 |
Correct |
451 ms |
29828 KB |
Output is correct |
70 |
Correct |
190 ms |
22896 KB |
Output is correct |
71 |
Correct |
196 ms |
23100 KB |
Output is correct |
72 |
Correct |
425 ms |
29684 KB |
Output is correct |
73 |
Correct |
427 ms |
30212 KB |
Output is correct |
74 |
Correct |
1067 ms |
40356 KB |
Output is correct |
75 |
Correct |
675 ms |
34540 KB |
Output is correct |
76 |
Correct |
850 ms |
39392 KB |
Output is correct |
77 |
Correct |
867 ms |
39548 KB |
Output is correct |
78 |
Correct |
235 ms |
29172 KB |
Output is correct |
79 |
Correct |
401 ms |
30752 KB |
Output is correct |
80 |
Correct |
257 ms |
29072 KB |
Output is correct |
81 |
Correct |
419 ms |
30832 KB |
Output is correct |
82 |
Correct |
960 ms |
51104 KB |
Output is correct |
83 |
Correct |
951 ms |
51188 KB |
Output is correct |
84 |
Correct |
726 ms |
51092 KB |
Output is correct |
85 |
Correct |
691 ms |
51060 KB |
Output is correct |
86 |
Correct |
237 ms |
27540 KB |
Output is correct |
87 |
Correct |
276 ms |
28396 KB |
Output is correct |
88 |
Correct |
457 ms |
29944 KB |
Output is correct |
89 |
Correct |
827 ms |
38832 KB |
Output is correct |