#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 100013;
int N, M;
int dsu[MAXN];
vi nodes[MAXN];
ll ans;
set<int> in[MAXN], out[MAXN], cc[MAXN];
//set of nodes going into a clique
//set of cliques going out of a node
//set of cliques going out of a clique
vpi todo;
vpi merges;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void proc()
{
while(!todo.empty())
{
int s = todo.back().fi, t = todo.back().se;
todo.pop_back();
s = get(s); t = get(t);
if (s == t) continue;
cc[s].erase(t);
cc[t].erase(s);
// cerr << "merge " << s << ' ' << t << endl;
ans -= (ll) SZ(in[s]) * SZ(nodes[s]);
ans -= (ll) SZ(in[t]) * SZ(nodes[t]);
if (SZ(nodes[s]) > SZ(nodes[t])) swap(s, t);
//so in[s] and in[t] merge.
for (int c : cc[s])
{
if (cc[c].count(t))
{
todo.PB({c, t});
}
cc[t].insert(c);
}
cc[s].clear();
for (int u : in[s])
{
if (cc[t].count(get(u)))
{
todo.PB({get(u), t});
}
cc[get(u)].erase(s);
cc[get(u)].insert(t);
out[u].erase(s);
out[u].insert(t);
in[t].insert(u);
}
in[s].clear();
nodes[t].insert(nodes[t].end(), ALL(nodes[s]));
dsu[s] = t;
ans += (ll) SZ(in[t]) * SZ(nodes[t]);
}
return;
}
void update(int u, int v)
{
v = get(v);
if (in[v].count(u)) return;
//if v -> u doesn't exist.
if (!cc[v].count(get(u)))
{
// cerr << "no " << v << ' ' << get(u) << endl;
in[v].insert(u);
// cerr << "in " << v << " insert " << u << endl;
out[u].insert(v);
ans += SZ(nodes[v]);
cc[get(u)].insert(v);
return;
}
todo.PB({get(u), v});
proc();
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
FOR(i, 0, N)
{
dsu[i] = i;
nodes[i].PB(i);
in[i].insert(i);
cc[i].insert(i);
}
//cc stores the edges in a forward direction.
FOR(i, 0, M)
{
int u, v;
cin >> u >> v; u--; v--;
update(u, v);
// ans = -N;
// FOR(i, 0, N)
// {
// ans += SZ(in[i]) * SZ(nodes[i]);
// }
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
14 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 |
16 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
13 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
13 ms |
16768 KB |
Output is correct |
20 |
Correct |
14 ms |
16768 KB |
Output is correct |
21 |
Correct |
14 ms |
16768 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
14 ms |
16896 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
13 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
13 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 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 |
14 ms |
16768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
14 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 |
16 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
13 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
13 ms |
16768 KB |
Output is correct |
20 |
Correct |
14 ms |
16768 KB |
Output is correct |
21 |
Correct |
14 ms |
16768 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
14 ms |
16896 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
13 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
13 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 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 |
14 ms |
16768 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
127 ms |
20472 KB |
Output is correct |
36 |
Correct |
164 ms |
23068 KB |
Output is correct |
37 |
Correct |
166 ms |
23156 KB |
Output is correct |
38 |
Correct |
160 ms |
22792 KB |
Output is correct |
39 |
Correct |
17 ms |
17152 KB |
Output is correct |
40 |
Correct |
20 ms |
17280 KB |
Output is correct |
41 |
Correct |
21 ms |
17280 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17280 KB |
Output is correct |
44 |
Correct |
18 ms |
17280 KB |
Output is correct |
45 |
Correct |
17 ms |
17152 KB |
Output is correct |
46 |
Correct |
18 ms |
17152 KB |
Output is correct |
47 |
Correct |
20 ms |
17280 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
29 ms |
17912 KB |
Output is correct |
50 |
Correct |
162 ms |
23152 KB |
Output is correct |
51 |
Correct |
23 ms |
17408 KB |
Output is correct |
52 |
Correct |
150 ms |
21496 KB |
Output is correct |
53 |
Correct |
28 ms |
17792 KB |
Output is correct |
54 |
Correct |
152 ms |
22264 KB |
Output is correct |
55 |
Correct |
21 ms |
17664 KB |
Output is correct |
56 |
Correct |
20 ms |
17664 KB |
Output is correct |
57 |
Correct |
21 ms |
17792 KB |
Output is correct |
58 |
Correct |
20 ms |
17664 KB |
Output is correct |
59 |
Correct |
19 ms |
17280 KB |
Output is correct |
60 |
Correct |
127 ms |
19704 KB |
Output is correct |
61 |
Correct |
21 ms |
17408 KB |
Output is correct |
62 |
Correct |
156 ms |
22536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
14 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 |
16 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
13 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
13 ms |
16768 KB |
Output is correct |
20 |
Correct |
14 ms |
16768 KB |
Output is correct |
21 |
Correct |
14 ms |
16768 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16768 KB |
Output is correct |
24 |
Correct |
14 ms |
16896 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
13 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
13 ms |
16768 KB |
Output is correct |
29 |
Correct |
13 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 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 |
14 ms |
16768 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
127 ms |
20472 KB |
Output is correct |
36 |
Correct |
164 ms |
23068 KB |
Output is correct |
37 |
Correct |
166 ms |
23156 KB |
Output is correct |
38 |
Correct |
160 ms |
22792 KB |
Output is correct |
39 |
Correct |
17 ms |
17152 KB |
Output is correct |
40 |
Correct |
20 ms |
17280 KB |
Output is correct |
41 |
Correct |
21 ms |
17280 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17280 KB |
Output is correct |
44 |
Correct |
18 ms |
17280 KB |
Output is correct |
45 |
Correct |
17 ms |
17152 KB |
Output is correct |
46 |
Correct |
18 ms |
17152 KB |
Output is correct |
47 |
Correct |
20 ms |
17280 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
29 ms |
17912 KB |
Output is correct |
50 |
Correct |
162 ms |
23152 KB |
Output is correct |
51 |
Correct |
23 ms |
17408 KB |
Output is correct |
52 |
Correct |
150 ms |
21496 KB |
Output is correct |
53 |
Correct |
28 ms |
17792 KB |
Output is correct |
54 |
Correct |
152 ms |
22264 KB |
Output is correct |
55 |
Correct |
21 ms |
17664 KB |
Output is correct |
56 |
Correct |
20 ms |
17664 KB |
Output is correct |
57 |
Correct |
21 ms |
17792 KB |
Output is correct |
58 |
Correct |
20 ms |
17664 KB |
Output is correct |
59 |
Correct |
19 ms |
17280 KB |
Output is correct |
60 |
Correct |
127 ms |
19704 KB |
Output is correct |
61 |
Correct |
21 ms |
17408 KB |
Output is correct |
62 |
Correct |
156 ms |
22536 KB |
Output is correct |
63 |
Correct |
607 ms |
74104 KB |
Output is correct |
64 |
Correct |
602 ms |
74104 KB |
Output is correct |
65 |
Correct |
601 ms |
74000 KB |
Output is correct |
66 |
Correct |
316 ms |
36848 KB |
Output is correct |
67 |
Correct |
989 ms |
44776 KB |
Output is correct |
68 |
Correct |
304 ms |
36976 KB |
Output is correct |
69 |
Correct |
589 ms |
41584 KB |
Output is correct |
70 |
Correct |
345 ms |
37104 KB |
Output is correct |
71 |
Correct |
357 ms |
36976 KB |
Output is correct |
72 |
Correct |
811 ms |
42152 KB |
Output is correct |
73 |
Correct |
834 ms |
42284 KB |
Output is correct |
74 |
Correct |
1469 ms |
55064 KB |
Output is correct |
75 |
Correct |
814 ms |
45808 KB |
Output is correct |
76 |
Correct |
1100 ms |
52220 KB |
Output is correct |
77 |
Correct |
1093 ms |
53076 KB |
Output is correct |
78 |
Correct |
390 ms |
42100 KB |
Output is correct |
79 |
Correct |
752 ms |
44468 KB |
Output is correct |
80 |
Correct |
352 ms |
42172 KB |
Output is correct |
81 |
Correct |
595 ms |
44016 KB |
Output is correct |
82 |
Correct |
901 ms |
59804 KB |
Output is correct |
83 |
Correct |
885 ms |
60016 KB |
Output is correct |
84 |
Correct |
783 ms |
60228 KB |
Output is correct |
85 |
Correct |
786 ms |
60284 KB |
Output is correct |
86 |
Correct |
727 ms |
41692 KB |
Output is correct |
87 |
Correct |
817 ms |
42520 KB |
Output is correct |
88 |
Correct |
828 ms |
42216 KB |
Output is correct |
89 |
Correct |
1071 ms |
51300 KB |
Output is correct |