#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)),v.erase(unique(all(v)),v.end())
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const int maxn = 100025;
int pa[maxn], sz[maxn];
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
set<int> in[maxn], cc[maxn];
set<int> s_in[maxn], s_out[maxn];
queue<pair<int,int>> que;
ll ans = 0;
bool join(int x, int y) {
x = anc(x);
y = anc(y);
if (x == y)
return false;
if (sz[x] > sz[y]) swap(x, y);
ans -= 1LL * sz[x] * (sz[x] - 1) + sz[x] * ll(in[x].size());
ans -= 1LL * sz[y] * (sz[y] - 1) + sz[y] * ll(in[y].size());
pa[x] = y;
sz[y] += sz[x];
for (int z: s_in[x]) {
if (s_out[y].count(z)) {
que.emplace(y, z);
}
}
for (int z: s_out[x]) {
if (s_in[y].count(z)) {
que.emplace(y, z);
}
}
for (int z: s_out[x]) {
s_in[z].erase(x);
s_in[z].insert(y);
}
for (int z: s_in[x]) {
s_out[z].erase(x);
s_out[z].insert(y);
}
s_in[y].erase(x);
s_out[y].erase(x);
for (int z: s_out[x])
if (z != y)
s_out[y].insert(z);
for (int z: s_in[x])
if (z != y)
s_in[y].insert(z);
for (int i: cc[x]) in[y].erase(i);
for (int i: cc[x]) cc[y].insert(i);
for (int i: in[x]) if (!cc[y].count(i)) in[y].insert(i);
ans += 1LL * sz[y] * (sz[y] - 1) + sz[y] * ll(in[y].size());
return true;
}
int ADJ[2025][2025];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1, cc[i].insert(i);
vector<pair<int,int>> edges;
while (m--) {
int a, b;
cin >> a >> b;
--a, --b;
if (s_out[anc(b)].count(anc(a))) {
que.emplace(a, b);
while (!que.empty()) {
auto [s, t] = que.front(); que.pop();
join(s, t);
}
}
if (anc(a) != anc(b)) {
s_out[anc(a)].insert(anc(b));
s_in[anc(b)].insert(anc(a));
if (!in[anc(b)].count(a))
ans += sz[anc(b)];
in[anc(b)].insert(a);
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19012 KB |
Output is correct |
3 |
Correct |
13 ms |
19128 KB |
Output is correct |
4 |
Correct |
13 ms |
19020 KB |
Output is correct |
5 |
Correct |
13 ms |
19020 KB |
Output is correct |
6 |
Correct |
13 ms |
19112 KB |
Output is correct |
7 |
Correct |
15 ms |
19164 KB |
Output is correct |
8 |
Correct |
14 ms |
19168 KB |
Output is correct |
9 |
Correct |
14 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19036 KB |
Output is correct |
11 |
Correct |
13 ms |
19052 KB |
Output is correct |
12 |
Correct |
12 ms |
19020 KB |
Output is correct |
13 |
Correct |
14 ms |
19020 KB |
Output is correct |
14 |
Correct |
13 ms |
19100 KB |
Output is correct |
15 |
Correct |
13 ms |
19136 KB |
Output is correct |
16 |
Correct |
13 ms |
19044 KB |
Output is correct |
17 |
Correct |
13 ms |
19020 KB |
Output is correct |
18 |
Correct |
13 ms |
19020 KB |
Output is correct |
19 |
Correct |
14 ms |
19020 KB |
Output is correct |
20 |
Correct |
15 ms |
19148 KB |
Output is correct |
21 |
Correct |
16 ms |
19104 KB |
Output is correct |
22 |
Correct |
13 ms |
19148 KB |
Output is correct |
23 |
Correct |
13 ms |
19080 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
15 ms |
19148 KB |
Output is correct |
26 |
Correct |
13 ms |
19092 KB |
Output is correct |
27 |
Correct |
12 ms |
19116 KB |
Output is correct |
28 |
Correct |
12 ms |
19140 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
13 ms |
19028 KB |
Output is correct |
31 |
Correct |
13 ms |
19160 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
13 ms |
19160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19012 KB |
Output is correct |
3 |
Correct |
13 ms |
19128 KB |
Output is correct |
4 |
Correct |
13 ms |
19020 KB |
Output is correct |
5 |
Correct |
13 ms |
19020 KB |
Output is correct |
6 |
Correct |
13 ms |
19112 KB |
Output is correct |
7 |
Correct |
15 ms |
19164 KB |
Output is correct |
8 |
Correct |
14 ms |
19168 KB |
Output is correct |
9 |
Correct |
14 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19036 KB |
Output is correct |
11 |
Correct |
13 ms |
19052 KB |
Output is correct |
12 |
Correct |
12 ms |
19020 KB |
Output is correct |
13 |
Correct |
14 ms |
19020 KB |
Output is correct |
14 |
Correct |
13 ms |
19100 KB |
Output is correct |
15 |
Correct |
13 ms |
19136 KB |
Output is correct |
16 |
Correct |
13 ms |
19044 KB |
Output is correct |
17 |
Correct |
13 ms |
19020 KB |
Output is correct |
18 |
Correct |
13 ms |
19020 KB |
Output is correct |
19 |
Correct |
14 ms |
19020 KB |
Output is correct |
20 |
Correct |
15 ms |
19148 KB |
Output is correct |
21 |
Correct |
16 ms |
19104 KB |
Output is correct |
22 |
Correct |
13 ms |
19148 KB |
Output is correct |
23 |
Correct |
13 ms |
19080 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
15 ms |
19148 KB |
Output is correct |
26 |
Correct |
13 ms |
19092 KB |
Output is correct |
27 |
Correct |
12 ms |
19116 KB |
Output is correct |
28 |
Correct |
12 ms |
19140 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
13 ms |
19028 KB |
Output is correct |
31 |
Correct |
13 ms |
19160 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
13 ms |
19160 KB |
Output is correct |
34 |
Correct |
16 ms |
19276 KB |
Output is correct |
35 |
Correct |
120 ms |
22684 KB |
Output is correct |
36 |
Correct |
163 ms |
25236 KB |
Output is correct |
37 |
Correct |
164 ms |
25292 KB |
Output is correct |
38 |
Correct |
159 ms |
24992 KB |
Output is correct |
39 |
Correct |
19 ms |
19528 KB |
Output is correct |
40 |
Correct |
20 ms |
20044 KB |
Output is correct |
41 |
Correct |
24 ms |
19940 KB |
Output is correct |
42 |
Correct |
18 ms |
19532 KB |
Output is correct |
43 |
Correct |
18 ms |
19404 KB |
Output is correct |
44 |
Correct |
17 ms |
19404 KB |
Output is correct |
45 |
Correct |
17 ms |
19532 KB |
Output is correct |
46 |
Correct |
16 ms |
19532 KB |
Output is correct |
47 |
Correct |
18 ms |
19788 KB |
Output is correct |
48 |
Correct |
18 ms |
19788 KB |
Output is correct |
49 |
Correct |
27 ms |
20460 KB |
Output is correct |
50 |
Correct |
169 ms |
25352 KB |
Output is correct |
51 |
Correct |
22 ms |
19788 KB |
Output is correct |
52 |
Correct |
159 ms |
23560 KB |
Output is correct |
53 |
Correct |
27 ms |
20292 KB |
Output is correct |
54 |
Correct |
146 ms |
24464 KB |
Output is correct |
55 |
Correct |
22 ms |
19944 KB |
Output is correct |
56 |
Correct |
22 ms |
19880 KB |
Output is correct |
57 |
Correct |
20 ms |
19876 KB |
Output is correct |
58 |
Correct |
22 ms |
19916 KB |
Output is correct |
59 |
Correct |
19 ms |
20240 KB |
Output is correct |
60 |
Correct |
126 ms |
22524 KB |
Output is correct |
61 |
Correct |
23 ms |
19788 KB |
Output is correct |
62 |
Correct |
164 ms |
24728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19012 KB |
Output is correct |
3 |
Correct |
13 ms |
19128 KB |
Output is correct |
4 |
Correct |
13 ms |
19020 KB |
Output is correct |
5 |
Correct |
13 ms |
19020 KB |
Output is correct |
6 |
Correct |
13 ms |
19112 KB |
Output is correct |
7 |
Correct |
15 ms |
19164 KB |
Output is correct |
8 |
Correct |
14 ms |
19168 KB |
Output is correct |
9 |
Correct |
14 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19036 KB |
Output is correct |
11 |
Correct |
13 ms |
19052 KB |
Output is correct |
12 |
Correct |
12 ms |
19020 KB |
Output is correct |
13 |
Correct |
14 ms |
19020 KB |
Output is correct |
14 |
Correct |
13 ms |
19100 KB |
Output is correct |
15 |
Correct |
13 ms |
19136 KB |
Output is correct |
16 |
Correct |
13 ms |
19044 KB |
Output is correct |
17 |
Correct |
13 ms |
19020 KB |
Output is correct |
18 |
Correct |
13 ms |
19020 KB |
Output is correct |
19 |
Correct |
14 ms |
19020 KB |
Output is correct |
20 |
Correct |
15 ms |
19148 KB |
Output is correct |
21 |
Correct |
16 ms |
19104 KB |
Output is correct |
22 |
Correct |
13 ms |
19148 KB |
Output is correct |
23 |
Correct |
13 ms |
19080 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
15 ms |
19148 KB |
Output is correct |
26 |
Correct |
13 ms |
19092 KB |
Output is correct |
27 |
Correct |
12 ms |
19116 KB |
Output is correct |
28 |
Correct |
12 ms |
19140 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
13 ms |
19028 KB |
Output is correct |
31 |
Correct |
13 ms |
19160 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
13 ms |
19160 KB |
Output is correct |
34 |
Correct |
16 ms |
19276 KB |
Output is correct |
35 |
Correct |
120 ms |
22684 KB |
Output is correct |
36 |
Correct |
163 ms |
25236 KB |
Output is correct |
37 |
Correct |
164 ms |
25292 KB |
Output is correct |
38 |
Correct |
159 ms |
24992 KB |
Output is correct |
39 |
Correct |
19 ms |
19528 KB |
Output is correct |
40 |
Correct |
20 ms |
20044 KB |
Output is correct |
41 |
Correct |
24 ms |
19940 KB |
Output is correct |
42 |
Correct |
18 ms |
19532 KB |
Output is correct |
43 |
Correct |
18 ms |
19404 KB |
Output is correct |
44 |
Correct |
17 ms |
19404 KB |
Output is correct |
45 |
Correct |
17 ms |
19532 KB |
Output is correct |
46 |
Correct |
16 ms |
19532 KB |
Output is correct |
47 |
Correct |
18 ms |
19788 KB |
Output is correct |
48 |
Correct |
18 ms |
19788 KB |
Output is correct |
49 |
Correct |
27 ms |
20460 KB |
Output is correct |
50 |
Correct |
169 ms |
25352 KB |
Output is correct |
51 |
Correct |
22 ms |
19788 KB |
Output is correct |
52 |
Correct |
159 ms |
23560 KB |
Output is correct |
53 |
Correct |
27 ms |
20292 KB |
Output is correct |
54 |
Correct |
146 ms |
24464 KB |
Output is correct |
55 |
Correct |
22 ms |
19944 KB |
Output is correct |
56 |
Correct |
22 ms |
19880 KB |
Output is correct |
57 |
Correct |
20 ms |
19876 KB |
Output is correct |
58 |
Correct |
22 ms |
19916 KB |
Output is correct |
59 |
Correct |
19 ms |
20240 KB |
Output is correct |
60 |
Correct |
126 ms |
22524 KB |
Output is correct |
61 |
Correct |
23 ms |
19788 KB |
Output is correct |
62 |
Correct |
164 ms |
24728 KB |
Output is correct |
63 |
Correct |
647 ms |
68828 KB |
Output is correct |
64 |
Correct |
647 ms |
68804 KB |
Output is correct |
65 |
Correct |
641 ms |
68792 KB |
Output is correct |
66 |
Correct |
349 ms |
40772 KB |
Output is correct |
67 |
Correct |
800 ms |
77564 KB |
Output is correct |
68 |
Correct |
384 ms |
40780 KB |
Output is correct |
69 |
Correct |
712 ms |
40516 KB |
Output is correct |
70 |
Correct |
377 ms |
43076 KB |
Output is correct |
71 |
Correct |
394 ms |
43052 KB |
Output is correct |
72 |
Correct |
800 ms |
58176 KB |
Output is correct |
73 |
Correct |
810 ms |
60248 KB |
Output is correct |
74 |
Correct |
1753 ms |
82852 KB |
Output is correct |
75 |
Correct |
1089 ms |
48476 KB |
Output is correct |
76 |
Correct |
1377 ms |
65816 KB |
Output is correct |
77 |
Correct |
1298 ms |
66100 KB |
Output is correct |
78 |
Correct |
400 ms |
46648 KB |
Output is correct |
79 |
Correct |
647 ms |
53184 KB |
Output is correct |
80 |
Correct |
488 ms |
47756 KB |
Output is correct |
81 |
Correct |
799 ms |
51780 KB |
Output is correct |
82 |
Correct |
1298 ms |
62596 KB |
Output is correct |
83 |
Correct |
1292 ms |
62420 KB |
Output is correct |
84 |
Correct |
976 ms |
61320 KB |
Output is correct |
85 |
Correct |
991 ms |
61476 KB |
Output is correct |
86 |
Correct |
671 ms |
96152 KB |
Output is correct |
87 |
Correct |
699 ms |
98416 KB |
Output is correct |
88 |
Correct |
758 ms |
59812 KB |
Output is correct |
89 |
Correct |
1203 ms |
64476 KB |
Output is correct |