#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int inN, inM;
vector<int> inGR[100007];
void initialize() {
ios_base :: sync_with_stdio(0);
cin.tie(0); cout.tie(0); cerr.tie(0);
if (fopen("FILE.INP", "r")) {
freopen("FILE.INP", "r", stdin);
freopen("FILE.OUT", "w", stdout);
}
cin >> inN >> inM;
for(int i = 1, u, v; i <= inM; ++i) {
cin >> u >> v;
inGR[u].push_back(v);
inGR[v].push_back(u);
}
}
int type_determine() {
if (inN == 10) return 1;
if (inN == 50) return 2;
}
namespace subtask1 {
int N, M;
bool havepath[17][17][17], visited[17];
vector<int> GR[100007];
ll ans = 0;
void backtrack2(ll finish, ll mid, ll start) {
if (finish != mid) {
havepath[start][mid][finish] = 1;
}
for(int nxtfinish : GR[finish]) {
if (visited[nxtfinish]) continue;
visited[nxtfinish] = 1;
backtrack2(nxtfinish, mid, start);
visited[nxtfinish] = 0;
}
}
void backtrack(ll mid, ll start) {
if (mid != start) {
backtrack2(mid, mid, start);
}
for(int nxtmid : GR[mid]) {
if (visited[nxtmid]) continue;
visited[nxtmid] = 1;
backtrack(nxtmid, start);
visited[nxtmid] = 0;
}
}
void preprocess() {
N = inN;
M = inM;
for(int i = 1; i <= N; ++i) GR[i] = inGR[i];
}
void solve() {
for(int i = 1; i <= N; ++i) {
visited[i] = 1;
backtrack(i, i);
visited[i] = 0;
}
for(int i = 1; i <= N; ++i) {
for(int j = 1; j <= N; ++j) {
for(int k = 1; k <= N; ++k) {
ans += havepath[i][j][k];
}
}
}
cout << ans << endl;
}
void process() {
preprocess();
solve();
}
}
namespace subtask3 {
int N, M, CpnCnt, cpn[100007], sz[100007], type[100007];
vector<int> GR[100007];
void preprocess() {
N = inN;
M = inM;
for(int i = 1; i <= N; ++i) GR[i] = inGR[i];
}
void Dfs(ll u, ll last) {
cpn[u] = CpnCnt;
for(int v : GR[u]) {
if (v == last) continue;
if (!cpn[v]) Dfs(v, u);
else type[CpnCnt] = 1;
}
}
void solve() {
for(int i = 1; i <= N; ++i) {
if (!cpn[i]) {
CpnCnt++;
Dfs(i, 0);
}
}
for(int i = 1; i <= N; ++i) {
sz[cpn[i]]++;
}
ll ans = 0;
for(int i = 1; i <= CpnCnt; ++i) {
ll k = sz[i];
//cerr << i << " " << k << endl;
if (!type[i]) ans += (k * (k - 1) * (k - 2)) / 6;
else ans += ((k - 1) * (k - 2)) / 2 * k;
}
cout << ans * 2;
}
void process() {
preprocess();
solve();
}
}
namespace subtask5 {
int N, M;
vector<int> GR[100007];
int visited[100007], sz[100007];
ll ans = 0;
void Dfs(int u) {
visited[u] = 1;
for(int v : GR[u]) {
if (visited[v]) continue;
Dfs(v);
sz[u] += sz[v];
}
sz[u]++;
}
void Dfs2(int u, int last, int root) {
vector<ll> vect;
vect.push_back(sz[root] - sz[u]);
for(int v : GR[u]) {
if (v == last) continue;
Dfs2(v, u, root);
vect.push_back(sz[v]);
}
// cerr << "u = " << u << endl;
// for(int c : vect) cerr << c << " ";
// cerr << endl;
ll sum = accumulate(vect.begin(), vect.end(), 0LL);
for(int c : vect) {
ans += c * (sum - c);
}
}
void preprocess() {
N = inN;
M = inM;
for(int i = 1; i <= N; ++i) GR[i] = inGR[i];
}
void solve() {
for(int i = 1; i <= N; ++i) {
if (!visited[i]) {
Dfs(i);
Dfs2(i, 0, i);
}
}
// for(int i = 1; i <= N; ++i) {
// cerr << i << " " << sz[i] << endl;
// }
cout << ans;
}
void process() {
preprocess();
solve();
}
}
int main() {
initialize();
//subtask1 :: process();
subtask5 :: process();
// cout << 10 << " " << (100 * 99) / 2 << endl;
// for(int i = 1; i <= 100; ++i) {
// for(int j = 1; j <= i - 1; ++j) {
// cout << i << " " << j << endl;
// }
// }
}
Compilation message
count_triplets.cpp: In function 'void initialize()':
count_triplets.cpp:13:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen("FILE.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen("FILE.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int type_determine()':
count_triplets.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
28 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
958 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11608 KB |
Output is correct |
2 |
Correct |
3 ms |
11620 KB |
Output is correct |
3 |
Correct |
3 ms |
11612 KB |
Output is correct |
4 |
Correct |
3 ms |
11616 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11788 KB |
Output is correct |
7 |
Correct |
3 ms |
11612 KB |
Output is correct |
8 |
Correct |
3 ms |
11612 KB |
Output is correct |
9 |
Correct |
3 ms |
11612 KB |
Output is correct |
10 |
Correct |
3 ms |
11608 KB |
Output is correct |
11 |
Correct |
3 ms |
11612 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
3 ms |
11612 KB |
Output is correct |
14 |
Correct |
3 ms |
11612 KB |
Output is correct |
15 |
Correct |
4 ms |
11612 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
3 ms |
11612 KB |
Output is correct |
18 |
Correct |
3 ms |
11612 KB |
Output is correct |
19 |
Correct |
3 ms |
11608 KB |
Output is correct |
20 |
Correct |
3 ms |
11612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
19252 KB |
Output is correct |
2 |
Correct |
41 ms |
19708 KB |
Output is correct |
3 |
Correct |
41 ms |
19292 KB |
Output is correct |
4 |
Correct |
54 ms |
19536 KB |
Output is correct |
5 |
Correct |
40 ms |
19280 KB |
Output is correct |
6 |
Correct |
49 ms |
28496 KB |
Output is correct |
7 |
Correct |
58 ms |
25332 KB |
Output is correct |
8 |
Correct |
71 ms |
24064 KB |
Output is correct |
9 |
Correct |
48 ms |
22236 KB |
Output is correct |
10 |
Correct |
39 ms |
19448 KB |
Output is correct |
11 |
Correct |
41 ms |
19276 KB |
Output is correct |
12 |
Correct |
43 ms |
19284 KB |
Output is correct |
13 |
Correct |
41 ms |
19292 KB |
Output is correct |
14 |
Correct |
36 ms |
19092 KB |
Output is correct |
15 |
Correct |
35 ms |
18780 KB |
Output is correct |
16 |
Correct |
24 ms |
16888 KB |
Output is correct |
17 |
Correct |
34 ms |
21224 KB |
Output is correct |
18 |
Correct |
33 ms |
21124 KB |
Output is correct |
19 |
Correct |
35 ms |
21072 KB |
Output is correct |
20 |
Correct |
35 ms |
20920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11768 KB |
Output is correct |
2 |
Correct |
3 ms |
11512 KB |
Output is correct |
3 |
Runtime error |
827 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
19452 KB |
Output is correct |
2 |
Correct |
43 ms |
19292 KB |
Output is correct |
3 |
Execution timed out |
1061 ms |
1048576 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |