// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 300005;
int n, m;
ll ans;
struct thing {
int v, u;
bool operator< (const thing &o) const {
return v < o.v;
}
};
set<thing> adj[MAXN];
set<ii> iadj[MAXN];
set<int> adjp[MAXN];
int p[MAXN], sz[MAXN];
void init() {
REP (i, 1, n + 1) {
p[i] = i;
sz[i] = 1;
}
}
int findp(int i) {
if (p[i] == i) {
return i;
}
return p[i] = findp(p[i]);
}
bool join(int a, int b) {
int pa = findp(a), pb = findp(b);
if (pa == pb) {
return 1;
}
if (sz[pa] < sz[pb]) {
swap(pa, pb);
}
vii todo;
ans -= (ll) SZ(adj[pa]) * sz[pa];
ans -= (ll) SZ(adj[pb]) * sz[pb];
adjp[pa].erase(pb);
adjp[pb].erase(pa);
for (auto [v, u] : adj[pb]) {
if (findp(v) == pa) {
iadj[pa].erase({u, v});
} else {
adj[pa].insert({v, u});
adjp[pa].insert(findp(v));
if (adjp[findp(v)].count(pa)) {
todo.pb({findp(v), pa});
}
}
}
for (auto [v, u] : iadj[pb]) {
if (findp(v) == pa) {
adj[pa].erase({u, v});
} else {
iadj[pa].insert({v, u});
adjp[findp(v)].erase(pb);
adjp[findp(v)].insert(pa);
if (adjp[pa].count(findp(v))) {
todo.pb({findp(v), pa});
}
}
}
/*
for (auto [x, y] : eg[pa][pb]) {
adj[pa].erase(y);
}
for (auto [x, y] : eg[pb][pa]) {
adj[pb].erase(y);
}
*/
ans -= (ll) sz[pa] * (sz[pa] - 1);
ans -= (ll) sz[pb] * (sz[pb] - 1);
sz[pa] += sz[pb];
ans += (ll) sz[pa] * (sz[pa] - 1);
ans += (ll) SZ(adj[pa]) * sz[pa];
p[pb] = pa;
for (auto [x, y] : todo) {
join(x, y);
}
return 1;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m;
init();
REP (i, 0, m) {
int a, b; cin >> a >> b;
if (findp(a) != findp(b)) {
bool gd = adj[findp(b)].insert({a, b}).SE;
if (gd) {
ans += sz[findp(b)];
}
iadj[findp(a)].insert({b, a});
adjp[findp(b)].insert(findp(a));
if (adjp[findp(a)].count(findp(b))) {
cerr << "JOIN " << a << ' ' << b << '\n';
join(a, b);
}
}
/*
while (1) {
bool fnd = 0;
RREP (j, i, 0) {
RREP (k, j - 1, 0) {
if (findp(a[j]) == findp(b[k]) && findp(a[k]) == findp(b[j]) && findp(a[j]) != findp(b[j])) {
fnd = 1;
join(a[j], b[j]);
}
}
}
if (!fnd) {
break;
}
}
ll ans = 0;
set<ii> st;
RREP (j, i, 0) {
if (findp(a[j]) == findp(b[j])) {
continue;
}
int pa = a[j], pb = findp(b[j]);
if (st.find({pa, pb}) != st.end()) {
continue;
}
st.insert({pa, pb});
ans += sz[pb];
}
REP (j, 1, n + 1) {
if (findp(j) != j) {
continue;
}
ans += (ll) sz[j] * (sz[j] - 1);
}
cout << ans << '\n';
*/
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
42580 KB |
Output is correct |
2 |
Correct |
20 ms |
42504 KB |
Output is correct |
3 |
Correct |
19 ms |
42584 KB |
Output is correct |
4 |
Correct |
19 ms |
42580 KB |
Output is correct |
5 |
Correct |
19 ms |
42580 KB |
Output is correct |
6 |
Correct |
20 ms |
42552 KB |
Output is correct |
7 |
Correct |
20 ms |
42608 KB |
Output is correct |
8 |
Correct |
19 ms |
42580 KB |
Output is correct |
9 |
Correct |
22 ms |
42580 KB |
Output is correct |
10 |
Correct |
19 ms |
42580 KB |
Output is correct |
11 |
Correct |
19 ms |
42580 KB |
Output is correct |
12 |
Correct |
19 ms |
42580 KB |
Output is correct |
13 |
Correct |
19 ms |
42580 KB |
Output is correct |
14 |
Correct |
20 ms |
42580 KB |
Output is correct |
15 |
Correct |
23 ms |
42580 KB |
Output is correct |
16 |
Correct |
19 ms |
42580 KB |
Output is correct |
17 |
Correct |
19 ms |
42704 KB |
Output is correct |
18 |
Correct |
20 ms |
42580 KB |
Output is correct |
19 |
Correct |
19 ms |
42580 KB |
Output is correct |
20 |
Correct |
20 ms |
42580 KB |
Output is correct |
21 |
Correct |
24 ms |
42580 KB |
Output is correct |
22 |
Correct |
20 ms |
42580 KB |
Output is correct |
23 |
Correct |
20 ms |
42580 KB |
Output is correct |
24 |
Correct |
20 ms |
42588 KB |
Output is correct |
25 |
Correct |
20 ms |
42604 KB |
Output is correct |
26 |
Correct |
18 ms |
42580 KB |
Output is correct |
27 |
Correct |
19 ms |
42580 KB |
Output is correct |
28 |
Correct |
19 ms |
42580 KB |
Output is correct |
29 |
Correct |
20 ms |
42580 KB |
Output is correct |
30 |
Correct |
20 ms |
42612 KB |
Output is correct |
31 |
Correct |
19 ms |
42580 KB |
Output is correct |
32 |
Correct |
19 ms |
42580 KB |
Output is correct |
33 |
Correct |
20 ms |
42580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
42580 KB |
Output is correct |
2 |
Correct |
20 ms |
42504 KB |
Output is correct |
3 |
Correct |
19 ms |
42584 KB |
Output is correct |
4 |
Correct |
19 ms |
42580 KB |
Output is correct |
5 |
Correct |
19 ms |
42580 KB |
Output is correct |
6 |
Correct |
20 ms |
42552 KB |
Output is correct |
7 |
Correct |
20 ms |
42608 KB |
Output is correct |
8 |
Correct |
19 ms |
42580 KB |
Output is correct |
9 |
Correct |
22 ms |
42580 KB |
Output is correct |
10 |
Correct |
19 ms |
42580 KB |
Output is correct |
11 |
Correct |
19 ms |
42580 KB |
Output is correct |
12 |
Correct |
19 ms |
42580 KB |
Output is correct |
13 |
Correct |
19 ms |
42580 KB |
Output is correct |
14 |
Correct |
20 ms |
42580 KB |
Output is correct |
15 |
Correct |
23 ms |
42580 KB |
Output is correct |
16 |
Correct |
19 ms |
42580 KB |
Output is correct |
17 |
Correct |
19 ms |
42704 KB |
Output is correct |
18 |
Correct |
20 ms |
42580 KB |
Output is correct |
19 |
Correct |
19 ms |
42580 KB |
Output is correct |
20 |
Correct |
20 ms |
42580 KB |
Output is correct |
21 |
Correct |
24 ms |
42580 KB |
Output is correct |
22 |
Correct |
20 ms |
42580 KB |
Output is correct |
23 |
Correct |
20 ms |
42580 KB |
Output is correct |
24 |
Correct |
20 ms |
42588 KB |
Output is correct |
25 |
Correct |
20 ms |
42604 KB |
Output is correct |
26 |
Correct |
18 ms |
42580 KB |
Output is correct |
27 |
Correct |
19 ms |
42580 KB |
Output is correct |
28 |
Correct |
19 ms |
42580 KB |
Output is correct |
29 |
Correct |
20 ms |
42580 KB |
Output is correct |
30 |
Correct |
20 ms |
42612 KB |
Output is correct |
31 |
Correct |
19 ms |
42580 KB |
Output is correct |
32 |
Correct |
19 ms |
42580 KB |
Output is correct |
33 |
Correct |
20 ms |
42580 KB |
Output is correct |
34 |
Correct |
21 ms |
42708 KB |
Output is correct |
35 |
Correct |
85 ms |
48496 KB |
Output is correct |
36 |
Correct |
97 ms |
51576 KB |
Output is correct |
37 |
Correct |
97 ms |
51748 KB |
Output is correct |
38 |
Correct |
95 ms |
51204 KB |
Output is correct |
39 |
Correct |
21 ms |
42828 KB |
Output is correct |
40 |
Correct |
22 ms |
42964 KB |
Output is correct |
41 |
Correct |
22 ms |
42964 KB |
Output is correct |
42 |
Correct |
27 ms |
42852 KB |
Output is correct |
43 |
Correct |
23 ms |
42836 KB |
Output is correct |
44 |
Correct |
23 ms |
42896 KB |
Output is correct |
45 |
Correct |
21 ms |
42836 KB |
Output is correct |
46 |
Correct |
21 ms |
42836 KB |
Output is correct |
47 |
Correct |
22 ms |
42964 KB |
Output is correct |
48 |
Correct |
22 ms |
42964 KB |
Output is correct |
49 |
Correct |
27 ms |
43884 KB |
Output is correct |
50 |
Correct |
100 ms |
51844 KB |
Output is correct |
51 |
Correct |
25 ms |
43220 KB |
Output is correct |
52 |
Correct |
86 ms |
49952 KB |
Output is correct |
53 |
Correct |
27 ms |
43860 KB |
Output is correct |
54 |
Correct |
96 ms |
50748 KB |
Output is correct |
55 |
Correct |
25 ms |
43220 KB |
Output is correct |
56 |
Correct |
24 ms |
43220 KB |
Output is correct |
57 |
Correct |
23 ms |
43604 KB |
Output is correct |
58 |
Correct |
23 ms |
43476 KB |
Output is correct |
59 |
Correct |
20 ms |
42872 KB |
Output is correct |
60 |
Correct |
71 ms |
47704 KB |
Output is correct |
61 |
Correct |
21 ms |
43092 KB |
Output is correct |
62 |
Correct |
93 ms |
51028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
42580 KB |
Output is correct |
2 |
Correct |
20 ms |
42504 KB |
Output is correct |
3 |
Correct |
19 ms |
42584 KB |
Output is correct |
4 |
Correct |
19 ms |
42580 KB |
Output is correct |
5 |
Correct |
19 ms |
42580 KB |
Output is correct |
6 |
Correct |
20 ms |
42552 KB |
Output is correct |
7 |
Correct |
20 ms |
42608 KB |
Output is correct |
8 |
Correct |
19 ms |
42580 KB |
Output is correct |
9 |
Correct |
22 ms |
42580 KB |
Output is correct |
10 |
Correct |
19 ms |
42580 KB |
Output is correct |
11 |
Correct |
19 ms |
42580 KB |
Output is correct |
12 |
Correct |
19 ms |
42580 KB |
Output is correct |
13 |
Correct |
19 ms |
42580 KB |
Output is correct |
14 |
Correct |
20 ms |
42580 KB |
Output is correct |
15 |
Correct |
23 ms |
42580 KB |
Output is correct |
16 |
Correct |
19 ms |
42580 KB |
Output is correct |
17 |
Correct |
19 ms |
42704 KB |
Output is correct |
18 |
Correct |
20 ms |
42580 KB |
Output is correct |
19 |
Correct |
19 ms |
42580 KB |
Output is correct |
20 |
Correct |
20 ms |
42580 KB |
Output is correct |
21 |
Correct |
24 ms |
42580 KB |
Output is correct |
22 |
Correct |
20 ms |
42580 KB |
Output is correct |
23 |
Correct |
20 ms |
42580 KB |
Output is correct |
24 |
Correct |
20 ms |
42588 KB |
Output is correct |
25 |
Correct |
20 ms |
42604 KB |
Output is correct |
26 |
Correct |
18 ms |
42580 KB |
Output is correct |
27 |
Correct |
19 ms |
42580 KB |
Output is correct |
28 |
Correct |
19 ms |
42580 KB |
Output is correct |
29 |
Correct |
20 ms |
42580 KB |
Output is correct |
30 |
Correct |
20 ms |
42612 KB |
Output is correct |
31 |
Correct |
19 ms |
42580 KB |
Output is correct |
32 |
Correct |
19 ms |
42580 KB |
Output is correct |
33 |
Correct |
20 ms |
42580 KB |
Output is correct |
34 |
Correct |
21 ms |
42708 KB |
Output is correct |
35 |
Correct |
85 ms |
48496 KB |
Output is correct |
36 |
Correct |
97 ms |
51576 KB |
Output is correct |
37 |
Correct |
97 ms |
51748 KB |
Output is correct |
38 |
Correct |
95 ms |
51204 KB |
Output is correct |
39 |
Correct |
21 ms |
42828 KB |
Output is correct |
40 |
Correct |
22 ms |
42964 KB |
Output is correct |
41 |
Correct |
22 ms |
42964 KB |
Output is correct |
42 |
Correct |
27 ms |
42852 KB |
Output is correct |
43 |
Correct |
23 ms |
42836 KB |
Output is correct |
44 |
Correct |
23 ms |
42896 KB |
Output is correct |
45 |
Correct |
21 ms |
42836 KB |
Output is correct |
46 |
Correct |
21 ms |
42836 KB |
Output is correct |
47 |
Correct |
22 ms |
42964 KB |
Output is correct |
48 |
Correct |
22 ms |
42964 KB |
Output is correct |
49 |
Correct |
27 ms |
43884 KB |
Output is correct |
50 |
Correct |
100 ms |
51844 KB |
Output is correct |
51 |
Correct |
25 ms |
43220 KB |
Output is correct |
52 |
Correct |
86 ms |
49952 KB |
Output is correct |
53 |
Correct |
27 ms |
43860 KB |
Output is correct |
54 |
Correct |
96 ms |
50748 KB |
Output is correct |
55 |
Correct |
25 ms |
43220 KB |
Output is correct |
56 |
Correct |
24 ms |
43220 KB |
Output is correct |
57 |
Correct |
23 ms |
43604 KB |
Output is correct |
58 |
Correct |
23 ms |
43476 KB |
Output is correct |
59 |
Correct |
20 ms |
42872 KB |
Output is correct |
60 |
Correct |
71 ms |
47704 KB |
Output is correct |
61 |
Correct |
21 ms |
43092 KB |
Output is correct |
62 |
Correct |
93 ms |
51028 KB |
Output is correct |
63 |
Correct |
439 ms |
91096 KB |
Output is correct |
64 |
Correct |
490 ms |
91088 KB |
Output is correct |
65 |
Correct |
469 ms |
90968 KB |
Output is correct |
66 |
Correct |
119 ms |
57176 KB |
Output is correct |
67 |
Correct |
232 ms |
61852 KB |
Output is correct |
68 |
Correct |
123 ms |
57164 KB |
Output is correct |
69 |
Correct |
271 ms |
58212 KB |
Output is correct |
70 |
Correct |
140 ms |
57164 KB |
Output is correct |
71 |
Correct |
126 ms |
57164 KB |
Output is correct |
72 |
Correct |
285 ms |
62476 KB |
Output is correct |
73 |
Correct |
257 ms |
62412 KB |
Output is correct |
74 |
Correct |
742 ms |
93376 KB |
Output is correct |
75 |
Correct |
410 ms |
65664 KB |
Output is correct |
76 |
Correct |
520 ms |
78208 KB |
Output is correct |
77 |
Correct |
512 ms |
78272 KB |
Output is correct |
78 |
Correct |
235 ms |
60760 KB |
Output is correct |
79 |
Correct |
269 ms |
65968 KB |
Output is correct |
80 |
Correct |
170 ms |
59628 KB |
Output is correct |
81 |
Correct |
262 ms |
64016 KB |
Output is correct |
82 |
Correct |
480 ms |
77004 KB |
Output is correct |
83 |
Correct |
483 ms |
76992 KB |
Output is correct |
84 |
Correct |
491 ms |
93008 KB |
Output is correct |
85 |
Correct |
523 ms |
93024 KB |
Output is correct |
86 |
Correct |
126 ms |
56360 KB |
Output is correct |
87 |
Correct |
176 ms |
58572 KB |
Output is correct |
88 |
Correct |
286 ms |
62812 KB |
Output is correct |
89 |
Correct |
507 ms |
75464 KB |
Output is correct |