#include <bits/stdc++.h>
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
#define pii pair<int, int>
#define fr first
#define sc second
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define mk make_pair
#define int long long
using namespace std;
const int MAXN = (int)1e5 + 5;
vector <int> g[MAXN];
int n, m;
int S, C, F;
bool used[MAXN];
bool dfs(int v) {
used[v] = 1;
if (v == F) {
used[v] = 0;
if (!used[C]) {
return false;
} else {
return true;
}
}
bool found = false;
for (int to : g[v]) {
if (!used[to]) {
found |= dfs(to);
if (found) {
used[v] = 0;
return true;
}
}
}
used[v] = 0;
return false;
}
void subtask1() {
int ans = 0;
for (int s = 1; s <= n; s++) {
for (int c = 1; c <= n; c++) {
for (int f = 1; f <= n; f++) {
if (s != c && s != f && c != f) {
S = s;
C = c;
F = f;
if (dfs(s)) {
ans++;
}
}
}
}
}
cout << ans << endl;
exit(0);
}
bool us[MAXN];
void get_sz(int v, int &sz) {
us[v] = 1;
sz++;
for (int to : g[v]) {
if (!us[to]) {
get_sz(to, sz);
}
}
}
int color[MAXN];
bool cycle(int v) {
color[v] = 1;
bool f = false;
for (int to : g[v]) {
if (color[to] == 0) {
f |= cycle(to);
}
if (color[to] == 1) {
f = 1;
}
}
color[v] = 2;
return f;
}
void subtask3() {
for (int i = 1; i <= n; i++) {
if (szof(g[i]) > 2) {
return;
}
}
vector <pii> vec;
for (int i = 1; i <= n; i++) {
if (color[i] == 0) {
int sz = 0;
get_sz(i, sz);
vec.pb({cycle(i), sz});
}
}
auto get_ans = [&](bool type, int sz) {
if (sz < 3) {
return 0ll;
}
if (type == 0) {
int sum = 0;
for (int i = 1; i <= sz; i++) {
sum += (i - 1) * (sz - i);
}
return sum;
} else {
return sz * (sz - 1) * (sz - 2);
}
};
int ans = 0;
for (auto el : vec) {
ans += get_ans(el.fr, el.sc);
}
exit(0);
}
bool was[MAXN];
int cnt = 0;
void check(int v) {
was[v] = 1;
cnt++;
for (int to : g[v]) {
if (!used[to]) {
check(to);
}
}
}
signed main() {
fastInp;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
check(1);
assert(cnt == n);
// могут быть несколько компонент
if (n <= 10 && m <= 100) {
subtask1();
}
subtask3();
}
/*
4 3
1 2
2 3
3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
768 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
768 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
648 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
593 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
627 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
608 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
664 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
768 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
768 ms |
1048580 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |