#include <bits/stdc++.h>
#define sz(v) v.size()
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)3e5 + 7;
const int M = (int)15e6 + 7;
const ll MOD = (ll)1e9 + 7;
const int inf = (ll)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int n, m, s, c, f, cur, d[N];
bool used[N], is;
vector<int> g[N];
bool dfs(int v, bool was) {
used[v] = 1;
if(v == c) was = 1;
if(was && v == f) return 1;
bool ok = 0;
for(auto to : g[v]) {
if(!used[to]) {
ok |= dfs(to, was);
if(ok) break;
}
}
used[v] = 0;
return ok;
}
void DFS(int v, int pr) {
used[v] = 1;
cur++;
for(auto to : g[v]) {
if(to == pr) continue;
if(used[to]) is = 1;
else {
DFS(to, v);
}
}
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
bool ok = 1;
for(int i = 1; i <= n && ok; ++i) ok &= (sz(g[i]) <= 2);
int ans = 0;
if(n <= 10 && m <= 100) {
for(s = 1; s <= n; ++s) {
int cur = 0;
for(c = 1; c <= n; ++c) {
for(f = 1; f <= n; ++f) {
if(s != c && c != f && s != f) {
fill(used + 1, used + 1 + n, 0);
ans += dfs(s, 0);
}
}
} }
} else if(ok) {
for(int i = 1; i <= n; ++i) {
if(!used[i]) {
cur = is = 0;
DFS(i, 0);
if(is) ans += cur * (cur - 1) * (cur - 2);
else ans += cur * (cur - 1) * (cur - 2) / 3;
}
}
} else {
for(s = 1; s <= n; ++s) {
queue<int> q;
q.push(s);
fill(d + 1, d + 1 + n, inf);
d[s] = 0;
while(!q.empty()) {
int v = q.front();
q.pop();
for(auto to : g[v]) {
if(d[v] + 1 < d[to]) {
d[to] = d[v] + 1;
q.push(to);
}
}
}
for(int i = 1; i <= n; ++i) {
if(d[i] == inf || i == s) continue;
ans += (d[i] - 1);
}
}
}
cout << ans << nl;
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
/*
4 3
1 2
2 3
3 4
*/
Compilation message
count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:70:8: warning: unused variable 'cur' [-Wunused-variable]
70 | int cur = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7288 KB |
Output is correct |
3 |
Correct |
3 ms |
7244 KB |
Output is correct |
4 |
Correct |
3 ms |
7244 KB |
Output is correct |
5 |
Correct |
4 ms |
7296 KB |
Output is correct |
6 |
Correct |
4 ms |
7328 KB |
Output is correct |
7 |
Correct |
4 ms |
7292 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
21 ms |
7368 KB |
Output is correct |
10 |
Correct |
247 ms |
7344 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
3 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7244 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
4 ms |
7244 KB |
Output is correct |
19 |
Correct |
4 ms |
7244 KB |
Output is correct |
20 |
Correct |
3 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
4 ms |
7244 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7244 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
3 ms |
7244 KB |
Output is correct |
28 |
Correct |
4 ms |
7244 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
4 ms |
7320 KB |
Output is correct |
31 |
Correct |
3 ms |
7244 KB |
Output is correct |
32 |
Correct |
4 ms |
7244 KB |
Output is correct |
33 |
Correct |
4 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
3 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7288 KB |
Output is correct |
3 |
Correct |
3 ms |
7244 KB |
Output is correct |
4 |
Correct |
3 ms |
7244 KB |
Output is correct |
5 |
Correct |
4 ms |
7296 KB |
Output is correct |
6 |
Correct |
4 ms |
7328 KB |
Output is correct |
7 |
Correct |
4 ms |
7292 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
21 ms |
7368 KB |
Output is correct |
10 |
Correct |
247 ms |
7344 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
3 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7244 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
4 ms |
7244 KB |
Output is correct |
19 |
Correct |
4 ms |
7244 KB |
Output is correct |
20 |
Correct |
3 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
4 ms |
7244 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7244 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
3 ms |
7244 KB |
Output is correct |
28 |
Correct |
4 ms |
7244 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
4 ms |
7320 KB |
Output is correct |
31 |
Correct |
3 ms |
7244 KB |
Output is correct |
32 |
Correct |
4 ms |
7244 KB |
Output is correct |
33 |
Correct |
4 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
3 ms |
7244 KB |
Output is correct |
36 |
Correct |
4 ms |
7244 KB |
Output is correct |
37 |
Correct |
4 ms |
7244 KB |
Output is correct |
38 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
15232 KB |
Output is correct |
2 |
Correct |
42 ms |
15252 KB |
Output is correct |
3 |
Correct |
40 ms |
12804 KB |
Output is correct |
4 |
Correct |
46 ms |
14000 KB |
Output is correct |
5 |
Correct |
45 ms |
12136 KB |
Output is correct |
6 |
Correct |
40 ms |
12108 KB |
Output is correct |
7 |
Correct |
38 ms |
11516 KB |
Output is correct |
8 |
Correct |
39 ms |
11844 KB |
Output is correct |
9 |
Correct |
39 ms |
11044 KB |
Output is correct |
10 |
Correct |
43 ms |
11468 KB |
Output is correct |
11 |
Correct |
31 ms |
10440 KB |
Output is correct |
12 |
Correct |
32 ms |
10444 KB |
Output is correct |
13 |
Correct |
30 ms |
10436 KB |
Output is correct |
14 |
Correct |
30 ms |
10264 KB |
Output is correct |
15 |
Correct |
23 ms |
10016 KB |
Output is correct |
16 |
Correct |
23 ms |
9804 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7372 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7472 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
7372 KB |
Output is correct |
2 |
Correct |
20 ms |
7416 KB |
Output is correct |
3 |
Correct |
20 ms |
7416 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
24 ms |
7420 KB |
Output is correct |
6 |
Correct |
25 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
25 ms |
7420 KB |
Output is correct |
9 |
Correct |
25 ms |
7424 KB |
Output is correct |
10 |
Correct |
18 ms |
7424 KB |
Output is correct |
11 |
Correct |
17 ms |
7372 KB |
Output is correct |
12 |
Correct |
12 ms |
7372 KB |
Output is correct |
13 |
Correct |
8 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7404 KB |
Output is correct |
17 |
Correct |
12 ms |
7424 KB |
Output is correct |
18 |
Correct |
13 ms |
7432 KB |
Output is correct |
19 |
Correct |
12 ms |
7440 KB |
Output is correct |
20 |
Correct |
12 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1094 ms |
11992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
7428 KB |
Output is correct |
2 |
Correct |
21 ms |
7420 KB |
Output is correct |
3 |
Incorrect |
23 ms |
7488 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1095 ms |
11896 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7288 KB |
Output is correct |
3 |
Correct |
3 ms |
7244 KB |
Output is correct |
4 |
Correct |
3 ms |
7244 KB |
Output is correct |
5 |
Correct |
4 ms |
7296 KB |
Output is correct |
6 |
Correct |
4 ms |
7328 KB |
Output is correct |
7 |
Correct |
4 ms |
7292 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
21 ms |
7368 KB |
Output is correct |
10 |
Correct |
247 ms |
7344 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
3 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7244 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
4 ms |
7244 KB |
Output is correct |
19 |
Correct |
4 ms |
7244 KB |
Output is correct |
20 |
Correct |
3 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
4 ms |
7244 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7244 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
3 ms |
7244 KB |
Output is correct |
28 |
Correct |
4 ms |
7244 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
4 ms |
7320 KB |
Output is correct |
31 |
Correct |
3 ms |
7244 KB |
Output is correct |
32 |
Correct |
4 ms |
7244 KB |
Output is correct |
33 |
Correct |
4 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
3 ms |
7244 KB |
Output is correct |
36 |
Correct |
4 ms |
7244 KB |
Output is correct |
37 |
Correct |
4 ms |
7244 KB |
Output is correct |
38 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7288 KB |
Output is correct |
3 |
Correct |
3 ms |
7244 KB |
Output is correct |
4 |
Correct |
3 ms |
7244 KB |
Output is correct |
5 |
Correct |
4 ms |
7296 KB |
Output is correct |
6 |
Correct |
4 ms |
7328 KB |
Output is correct |
7 |
Correct |
4 ms |
7292 KB |
Output is correct |
8 |
Correct |
6 ms |
7360 KB |
Output is correct |
9 |
Correct |
21 ms |
7368 KB |
Output is correct |
10 |
Correct |
247 ms |
7344 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
3 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
4 ms |
7244 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
4 ms |
7244 KB |
Output is correct |
19 |
Correct |
4 ms |
7244 KB |
Output is correct |
20 |
Correct |
3 ms |
7244 KB |
Output is correct |
21 |
Correct |
4 ms |
7244 KB |
Output is correct |
22 |
Correct |
4 ms |
7244 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7244 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
3 ms |
7244 KB |
Output is correct |
28 |
Correct |
4 ms |
7244 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
4 ms |
7320 KB |
Output is correct |
31 |
Correct |
3 ms |
7244 KB |
Output is correct |
32 |
Correct |
4 ms |
7244 KB |
Output is correct |
33 |
Correct |
4 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
3 ms |
7244 KB |
Output is correct |
36 |
Correct |
4 ms |
7244 KB |
Output is correct |
37 |
Correct |
4 ms |
7244 KB |
Output is correct |
38 |
Incorrect |
4 ms |
7372 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |