#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5+5;
vector< vector<int> > adj(MAXN);
bool visited[MAXN];
long long sz[MAXN];
long long bgsz[MAXN];
int par[MAXN];
long long add[MAXN];
int n, m;
int find(int v)
{
if(par[v] == v)
return v;
return par[v] = find(par[v]);
}
void merge(int a, int b)
{
a = find(a), b = find(b);
if(a == b)
return;
if(bgsz[a] < bgsz[b])
swap(a, b);
par[b] = a;
bgsz[a] += bgsz[b];
}
pair<long long, bool> subtask3(int v, int p)
{
pair<long long, bool> ans = {1, false};
visited[v] = true;
for(int e : adj[v])
{
if(e == p)
continue;
if(visited[e]) {
ans.second = true;
continue;
}
pair<long long, bool> temp = subtask3(e, v);
ans.first += temp.first;
ans.second |= temp.second;
}
return ans;
}
long long subtask5(int v, int p)
{
long long ans = 0;
visited[v] = true;
sz[v] = 1;
long long chi_size = 0;
for(int e : adj[v])
{
if(e == p)
continue;
ans += subtask5(e, v);
sz[v] += sz[e];
chi_size += sz[e];
}
int bigpar = find(v);
long long par_size = bgsz[bigpar] - chi_size - 1;
ans += par_size * chi_size;
//cout << bgsz[bigpar] << "\n";
for(int e : adj[v])
{
if(e == p)
continue;
ans += sz[e] * (bgsz[bigpar] - sz[e] - 1LL);
}
return ans;
}
int main()
{
long long temp = 0;
add[0] = 0;
for(long long i = 1; i < MAXN; i++)
{
temp += i;
add[i] = add[i - 1] + temp;
par[i] = i;
bgsz[i] = 1;
}
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int f, s;
cin >> f >> s;
adj[f].push_back(s);
adj[s].push_back(f);
merge(f, s);
}
int max_size = 0;
for(int i = 1; i <= n; i++)
{
max_size = max(max_size, (int)adj[i].size());
}
long long ans = 0;
if(max_size <= 2) // Subtask 3
{
for(int i = 1; i <= n; i++)
{
if(visited[i])
continue;
pair<long long, bool> curr = subtask3(i, -1);
if(curr.second)
{
ans += curr.first * (curr.first - 1) * (curr.first - 2);
} else if(curr.first >= 3)
{
ans += add[curr.first - 2] * 2LL;
}
}
} else //Subtask 4, 5
{
for(int i = 1; i <= n; i++)
{
if(visited[i])
continue;
ans += subtask5(i, -1);
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
452 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
452 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
15544 KB |
Output is correct |
2 |
Correct |
97 ms |
15632 KB |
Output is correct |
3 |
Correct |
90 ms |
11644 KB |
Output is correct |
4 |
Correct |
99 ms |
13748 KB |
Output is correct |
5 |
Correct |
88 ms |
10348 KB |
Output is correct |
6 |
Correct |
91 ms |
10488 KB |
Output is correct |
7 |
Correct |
97 ms |
9368 KB |
Output is correct |
8 |
Correct |
107 ms |
9904 KB |
Output is correct |
9 |
Correct |
96 ms |
8572 KB |
Output is correct |
10 |
Correct |
91 ms |
9288 KB |
Output is correct |
11 |
Correct |
73 ms |
7728 KB |
Output is correct |
12 |
Correct |
71 ms |
7620 KB |
Output is correct |
13 |
Correct |
66 ms |
7648 KB |
Output is correct |
14 |
Correct |
64 ms |
7568 KB |
Output is correct |
15 |
Correct |
48 ms |
7168 KB |
Output is correct |
16 |
Correct |
50 ms |
7148 KB |
Output is correct |
17 |
Correct |
3 ms |
4692 KB |
Output is correct |
18 |
Correct |
4 ms |
4692 KB |
Output is correct |
19 |
Correct |
4 ms |
4692 KB |
Output is correct |
20 |
Correct |
4 ms |
4692 KB |
Output is correct |
21 |
Correct |
4 ms |
4692 KB |
Output is correct |
22 |
Correct |
3 ms |
4724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
5 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4692 KB |
Output is correct |
5 |
Correct |
3 ms |
4692 KB |
Output is correct |
6 |
Correct |
3 ms |
4692 KB |
Output is correct |
7 |
Correct |
3 ms |
4632 KB |
Output is correct |
8 |
Correct |
3 ms |
4692 KB |
Output is correct |
9 |
Correct |
3 ms |
4632 KB |
Output is correct |
10 |
Correct |
3 ms |
4628 KB |
Output is correct |
11 |
Correct |
4 ms |
4636 KB |
Output is correct |
12 |
Correct |
3 ms |
4564 KB |
Output is correct |
13 |
Correct |
3 ms |
4564 KB |
Output is correct |
14 |
Correct |
3 ms |
4564 KB |
Output is correct |
15 |
Correct |
3 ms |
4628 KB |
Output is correct |
16 |
Correct |
3 ms |
4628 KB |
Output is correct |
17 |
Correct |
3 ms |
4564 KB |
Output is correct |
18 |
Correct |
3 ms |
4628 KB |
Output is correct |
19 |
Correct |
4 ms |
4624 KB |
Output is correct |
20 |
Correct |
3 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
8616 KB |
Output is correct |
2 |
Correct |
93 ms |
8968 KB |
Output is correct |
3 |
Correct |
115 ms |
8944 KB |
Output is correct |
4 |
Correct |
94 ms |
8956 KB |
Output is correct |
5 |
Correct |
92 ms |
8860 KB |
Output is correct |
6 |
Correct |
91 ms |
12308 KB |
Output is correct |
7 |
Correct |
105 ms |
12208 KB |
Output is correct |
8 |
Correct |
121 ms |
11340 KB |
Output is correct |
9 |
Correct |
114 ms |
10464 KB |
Output is correct |
10 |
Correct |
122 ms |
8872 KB |
Output is correct |
11 |
Correct |
125 ms |
9976 KB |
Output is correct |
12 |
Correct |
106 ms |
9932 KB |
Output is correct |
13 |
Correct |
117 ms |
9908 KB |
Output is correct |
14 |
Correct |
89 ms |
9676 KB |
Output is correct |
15 |
Correct |
71 ms |
9440 KB |
Output is correct |
16 |
Correct |
55 ms |
8368 KB |
Output is correct |
17 |
Correct |
73 ms |
10196 KB |
Output is correct |
18 |
Correct |
86 ms |
10168 KB |
Output is correct |
19 |
Correct |
84 ms |
10124 KB |
Output is correct |
20 |
Correct |
87 ms |
10252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4692 KB |
Output is correct |
3 |
Runtime error |
596 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
8612 KB |
Output is correct |
2 |
Correct |
97 ms |
8664 KB |
Output is correct |
3 |
Runtime error |
702 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
452 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
452 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |