#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <cassert>
using namespace std;
const int maxN = 100'000;
vector<int> label(1+maxN);
set<int> out_edge[1+maxN]; //label to label
set<int> in_edge[1+maxN];
vector<int> label_list[1+maxN];
set<int> in_count[1+maxN];
// vector<int> in_count(1+maxN, 0);
int sz(int u)
{
return int(label_list[u].size());
}
long long answer = 0;
long long comp(int u)
{
u = label[u];
return (long long)(in_count[u].size()) * (long long)(label_list[u].size());
}
void insert_in(int B, int A)
{
answer -= comp(label[B]);
in_count[label[B]].insert(A);
answer += comp(label[B]);
}
queue< pair<int, int> > join_queue;
void join(int u, int v)
{
u = label[u];
v = label[v];
if(u == v) return;
assert(u != v);
answer -= comp(u);
answer -= comp(v);
out_edge[u].erase(v);
out_edge[v].erase(u);
in_edge[u].erase(v);
in_edge[v].erase(u);
if(sz(u) < sz(v)) swap(u, v);
for(int l: label_list[v])
{
label_list[u].push_back(l);
label[l] = u;
}
for(int ic: in_count[v]) in_count[u].insert(ic);
for(int o: out_edge[v])
{
out_edge[u].insert(o);
in_edge[o].erase(v);
in_edge[o].insert(u);
// assert(label[o] == o);
if(out_edge[o].find(u) != out_edge[o].end())
join_queue.push(make_pair(o, u));
}
for(int i: in_edge[v])
{
in_edge[u].insert(i);
out_edge[i].erase(v);
out_edge[i].insert(u);
// assert(label[i] == i);
if(in_edge[i].find(u) != in_edge[i].end())
join_queue.push(make_pair(i, u));
}
answer += comp(u);
in_count[v].clear();
label_list[v].clear();
out_edge[v].clear();
in_edge[v].clear();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
for(int i = 1; i <= N; i++)
{
label[i] = i;
label_list[i].push_back(i);
in_count[i].insert(i);
}
for(int j = 1; j <= M; j++)
{
int A, B;
cin >> A >> B;
insert_in(B, A);
A = label[A];
B = label[B];
cerr << "label = " << A << ' ' << B << '\n';
if(A != B)
{
out_edge[A].insert(B);
in_edge[B].insert(A);
if(out_edge[B].find(A) != out_edge[B].end())
{
join_queue.push(make_pair(A, B));
}
while(!join_queue.empty())
{
auto g = join_queue.front();
join_queue.pop();
join(g.first, g.second);
}
}
cout << answer << '\n';
cerr << "\n operation done\n";
cerr << "labels: ";
for(int i = 1; i <= N; i++) cerr << label[i] << ' ';
cerr << '\n';
for(int i = 1; i <= N; i++)
{
cerr << "out edge " << i << ": ";
for(int o: out_edge[i]) cerr << o << ' ';
cerr << '\n';
}
cerr << "\n\n";
}
for(int i = 1; i <= N; i++) cerr << label[i] << ' ';
cerr << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17100 KB |
Output is correct |
2 |
Correct |
9 ms |
17100 KB |
Output is correct |
3 |
Correct |
8 ms |
17124 KB |
Output is correct |
4 |
Correct |
8 ms |
17100 KB |
Output is correct |
5 |
Correct |
16 ms |
17100 KB |
Output is correct |
6 |
Correct |
15 ms |
17164 KB |
Output is correct |
7 |
Correct |
878 ms |
19600 KB |
Output is correct |
8 |
Correct |
863 ms |
19312 KB |
Output is correct |
9 |
Correct |
877 ms |
19296 KB |
Output is correct |
10 |
Correct |
42 ms |
17228 KB |
Output is correct |
11 |
Correct |
48 ms |
17228 KB |
Output is correct |
12 |
Correct |
46 ms |
17196 KB |
Output is correct |
13 |
Correct |
41 ms |
17184 KB |
Output is correct |
14 |
Correct |
45 ms |
17208 KB |
Output is correct |
15 |
Correct |
45 ms |
17240 KB |
Output is correct |
16 |
Correct |
42 ms |
17192 KB |
Output is correct |
17 |
Correct |
41 ms |
17188 KB |
Output is correct |
18 |
Correct |
46 ms |
17340 KB |
Output is correct |
19 |
Correct |
44 ms |
17152 KB |
Output is correct |
20 |
Correct |
141 ms |
17444 KB |
Output is correct |
21 |
Correct |
883 ms |
19308 KB |
Output is correct |
22 |
Correct |
119 ms |
17332 KB |
Output is correct |
23 |
Correct |
403 ms |
18244 KB |
Output is correct |
24 |
Correct |
209 ms |
17580 KB |
Output is correct |
25 |
Correct |
713 ms |
18964 KB |
Output is correct |
26 |
Correct |
51 ms |
17228 KB |
Output is correct |
27 |
Correct |
53 ms |
17224 KB |
Output is correct |
28 |
Correct |
51 ms |
17196 KB |
Output is correct |
29 |
Correct |
52 ms |
17160 KB |
Output is correct |
30 |
Correct |
46 ms |
17228 KB |
Output is correct |
31 |
Correct |
694 ms |
18928 KB |
Output is correct |
32 |
Correct |
53 ms |
17200 KB |
Output is correct |
33 |
Correct |
717 ms |
18792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17100 KB |
Output is correct |
2 |
Correct |
9 ms |
17100 KB |
Output is correct |
3 |
Correct |
8 ms |
17124 KB |
Output is correct |
4 |
Correct |
8 ms |
17100 KB |
Output is correct |
5 |
Correct |
16 ms |
17100 KB |
Output is correct |
6 |
Correct |
15 ms |
17164 KB |
Output is correct |
7 |
Correct |
878 ms |
19600 KB |
Output is correct |
8 |
Correct |
863 ms |
19312 KB |
Output is correct |
9 |
Correct |
877 ms |
19296 KB |
Output is correct |
10 |
Correct |
42 ms |
17228 KB |
Output is correct |
11 |
Correct |
48 ms |
17228 KB |
Output is correct |
12 |
Correct |
46 ms |
17196 KB |
Output is correct |
13 |
Correct |
41 ms |
17184 KB |
Output is correct |
14 |
Correct |
45 ms |
17208 KB |
Output is correct |
15 |
Correct |
45 ms |
17240 KB |
Output is correct |
16 |
Correct |
42 ms |
17192 KB |
Output is correct |
17 |
Correct |
41 ms |
17188 KB |
Output is correct |
18 |
Correct |
46 ms |
17340 KB |
Output is correct |
19 |
Correct |
44 ms |
17152 KB |
Output is correct |
20 |
Correct |
141 ms |
17444 KB |
Output is correct |
21 |
Correct |
883 ms |
19308 KB |
Output is correct |
22 |
Correct |
119 ms |
17332 KB |
Output is correct |
23 |
Correct |
403 ms |
18244 KB |
Output is correct |
24 |
Correct |
209 ms |
17580 KB |
Output is correct |
25 |
Correct |
713 ms |
18964 KB |
Output is correct |
26 |
Correct |
51 ms |
17228 KB |
Output is correct |
27 |
Correct |
53 ms |
17224 KB |
Output is correct |
28 |
Correct |
51 ms |
17196 KB |
Output is correct |
29 |
Correct |
52 ms |
17160 KB |
Output is correct |
30 |
Correct |
46 ms |
17228 KB |
Output is correct |
31 |
Correct |
694 ms |
18928 KB |
Output is correct |
32 |
Correct |
53 ms |
17200 KB |
Output is correct |
33 |
Correct |
717 ms |
18792 KB |
Output is correct |
34 |
Execution timed out |
5084 ms |
30236 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17100 KB |
Output is correct |
2 |
Correct |
9 ms |
17100 KB |
Output is correct |
3 |
Correct |
8 ms |
17124 KB |
Output is correct |
4 |
Correct |
8 ms |
17100 KB |
Output is correct |
5 |
Correct |
16 ms |
17100 KB |
Output is correct |
6 |
Correct |
15 ms |
17164 KB |
Output is correct |
7 |
Correct |
878 ms |
19600 KB |
Output is correct |
8 |
Correct |
863 ms |
19312 KB |
Output is correct |
9 |
Correct |
877 ms |
19296 KB |
Output is correct |
10 |
Correct |
42 ms |
17228 KB |
Output is correct |
11 |
Correct |
48 ms |
17228 KB |
Output is correct |
12 |
Correct |
46 ms |
17196 KB |
Output is correct |
13 |
Correct |
41 ms |
17184 KB |
Output is correct |
14 |
Correct |
45 ms |
17208 KB |
Output is correct |
15 |
Correct |
45 ms |
17240 KB |
Output is correct |
16 |
Correct |
42 ms |
17192 KB |
Output is correct |
17 |
Correct |
41 ms |
17188 KB |
Output is correct |
18 |
Correct |
46 ms |
17340 KB |
Output is correct |
19 |
Correct |
44 ms |
17152 KB |
Output is correct |
20 |
Correct |
141 ms |
17444 KB |
Output is correct |
21 |
Correct |
883 ms |
19308 KB |
Output is correct |
22 |
Correct |
119 ms |
17332 KB |
Output is correct |
23 |
Correct |
403 ms |
18244 KB |
Output is correct |
24 |
Correct |
209 ms |
17580 KB |
Output is correct |
25 |
Correct |
713 ms |
18964 KB |
Output is correct |
26 |
Correct |
51 ms |
17228 KB |
Output is correct |
27 |
Correct |
53 ms |
17224 KB |
Output is correct |
28 |
Correct |
51 ms |
17196 KB |
Output is correct |
29 |
Correct |
52 ms |
17160 KB |
Output is correct |
30 |
Correct |
46 ms |
17228 KB |
Output is correct |
31 |
Correct |
694 ms |
18928 KB |
Output is correct |
32 |
Correct |
53 ms |
17200 KB |
Output is correct |
33 |
Correct |
717 ms |
18792 KB |
Output is correct |
34 |
Execution timed out |
5084 ms |
30236 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |