#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long int LLI;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
vi adjList[100000];
int disc[100000],low[100000],art[100000],num = 0;
vi com[100000];
vpii S;
int bcc = 0;
int doDFS(int u,int p) {
int i,c = 0;
disc[u] = low[u] = num++;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (disc[v] == -1) {
c++;
S.pb(mp(u,v));
doDFS(v,u);
low[u] = min(low[u],low[v]);
if (((p == -1) && (c > 1)) || ((p != -1) && (low[v] >= disc[u]))) {
art[u] = 1;
while (1) {
int uu = S.back().first,vv = S.back().second;
S.pop_back();
com[bcc].pb(uu),com[bcc].pb(vv);
if ((u == uu) && (v == vv)) break;
}
bcc++;
}
}
else if ((v != p) && (disc[v] < disc[u])) low[u] = min(low[u],disc[v]),S.pb(mp(u,v));
}
return 0;
}
int size[100000];
vi adjList2[200000];
LLI ans = 0;
int visited[200000],sub[200000];
int doDFS2(int u,int p) {
int i;
sub[u] = ((u < bcc) ? size[u]:0);
visited[u] = 1;
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (v != p) sub[u] += doDFS2(v,u);
}
return sub[u];
}
int doDFS3(int u,int p,int r) {
int i;
if (u < bcc) {
ans += 2LL*size[u]*sub[u]*(sub[r]-sub[u]+size[u]);
ans -= (LLI) size[u]*size[u]*size[u];
}
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (v != p) doDFS3(v,u,r);
}
return 0;
}
int doDFS4(int u,int p,int s,int sum) {
int i;
if (u < bcc) {
sum += size[u];
ans += (LLI) size[s]*size[u]*sum;
}
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (v != p) doDFS4(v,u,s,sum);
}
return 0;
}
int main() {
int i;
int n,m,u,v;
scanf("%d %d",&n,&m);
for (i = 0; i < m; i++) {
scanf("%d %d",&u,&v);
u--,v--;
adjList[u].pb(v);
adjList[v].pb(u);
}
int j;
for (i = 0; i < n; i++) disc[i] = low[i] = -1;
for (i = 0; i < n; i++) {
if (disc[i] == -1) {
doDFS(i,-1);
if (!S.empty()) {
while (!S.empty()) {
int u = S.back().first,v = S.back().second;
S.pop_back();
com[bcc].pb(u),com[bcc].pb(v);
}
bcc++;
}
}
}
for (i = 0; i < bcc; i++) {
sort(com[i].begin(),com[i].end());
com[i].resize(unique(com[i].begin(),com[i].end())-com[i].begin());
for (j = 0; j < com[i].size(); j++) {
if (art[com[i][j]]) {
adjList2[i].pb(com[i][j]+bcc);
adjList2[com[i][j]+bcc].pb(i);
}
}
size[i] = com[i].size()-1;
ans += (LLI) com[i].size()*(com[i].size()-1)*(com[i].size()-2);
ans -= (LLI) size[i]*size[i]*size[i];
ans += (LLI) size[i]*size[i];
}
for (i = 0; i < bcc; i++) {
if (!visited[i]) {
doDFS2(i,-1);
doDFS3(i,-1,i);
ans -= (LLI) sub[i]*sub[i];
}
}
//for (i = 0; i < bcc; i++) doDFS4(i,-1,i,0);
printf("%lld\n",ans);
return 0;
}
Compilation message
count_triplets.cpp: In function 'int doDFS(int, int)':
count_triplets.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int doDFS2(int, int)':
count_triplets.cpp:48:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList2[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int doDFS3(int, int, int)':
count_triplets.cpp:60:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList2[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int doDFS4(int, int, int, int)':
count_triplets.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList2[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:107:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < com[i].size(); j++) {
~~^~~~~~~~~~~~~~~
count_triplets.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
count_triplets.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&u,&v);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
13 ms |
9832 KB |
Output is correct |
3 |
Correct |
14 ms |
9888 KB |
Output is correct |
4 |
Correct |
11 ms |
9888 KB |
Output is correct |
5 |
Correct |
14 ms |
9888 KB |
Output is correct |
6 |
Correct |
13 ms |
9888 KB |
Output is correct |
7 |
Incorrect |
11 ms |
9944 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
13 ms |
9832 KB |
Output is correct |
3 |
Correct |
14 ms |
9888 KB |
Output is correct |
4 |
Correct |
11 ms |
9888 KB |
Output is correct |
5 |
Correct |
14 ms |
9888 KB |
Output is correct |
6 |
Correct |
13 ms |
9888 KB |
Output is correct |
7 |
Incorrect |
11 ms |
9944 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
27204 KB |
Output is correct |
2 |
Correct |
158 ms |
27204 KB |
Output is correct |
3 |
Correct |
222 ms |
27204 KB |
Output is correct |
4 |
Correct |
240 ms |
27204 KB |
Output is correct |
5 |
Correct |
199 ms |
27204 KB |
Output is correct |
6 |
Correct |
271 ms |
27204 KB |
Output is correct |
7 |
Correct |
274 ms |
27204 KB |
Output is correct |
8 |
Correct |
306 ms |
27204 KB |
Output is correct |
9 |
Correct |
282 ms |
27204 KB |
Output is correct |
10 |
Correct |
198 ms |
27204 KB |
Output is correct |
11 |
Correct |
165 ms |
27204 KB |
Output is correct |
12 |
Correct |
192 ms |
27204 KB |
Output is correct |
13 |
Correct |
137 ms |
27204 KB |
Output is correct |
14 |
Correct |
164 ms |
27204 KB |
Output is correct |
15 |
Correct |
97 ms |
27204 KB |
Output is correct |
16 |
Correct |
115 ms |
27204 KB |
Output is correct |
17 |
Correct |
18 ms |
27204 KB |
Output is correct |
18 |
Correct |
16 ms |
27204 KB |
Output is correct |
19 |
Correct |
16 ms |
27204 KB |
Output is correct |
20 |
Correct |
16 ms |
27204 KB |
Output is correct |
21 |
Correct |
18 ms |
27204 KB |
Output is correct |
22 |
Correct |
14 ms |
27204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
27204 KB |
Output is correct |
2 |
Correct |
15 ms |
27204 KB |
Output is correct |
3 |
Correct |
14 ms |
27204 KB |
Output is correct |
4 |
Correct |
12 ms |
27204 KB |
Output is correct |
5 |
Correct |
16 ms |
27204 KB |
Output is correct |
6 |
Correct |
15 ms |
27204 KB |
Output is correct |
7 |
Correct |
16 ms |
27204 KB |
Output is correct |
8 |
Correct |
13 ms |
27204 KB |
Output is correct |
9 |
Correct |
14 ms |
27204 KB |
Output is correct |
10 |
Correct |
16 ms |
27204 KB |
Output is correct |
11 |
Correct |
14 ms |
27204 KB |
Output is correct |
12 |
Correct |
13 ms |
27204 KB |
Output is correct |
13 |
Correct |
13 ms |
27204 KB |
Output is correct |
14 |
Correct |
13 ms |
27204 KB |
Output is correct |
15 |
Correct |
11 ms |
27204 KB |
Output is correct |
16 |
Correct |
11 ms |
27204 KB |
Output is correct |
17 |
Correct |
16 ms |
27204 KB |
Output is correct |
18 |
Correct |
15 ms |
27204 KB |
Output is correct |
19 |
Correct |
14 ms |
27204 KB |
Output is correct |
20 |
Correct |
13 ms |
27204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
27204 KB |
Output is correct |
2 |
Correct |
209 ms |
27204 KB |
Output is correct |
3 |
Correct |
215 ms |
27204 KB |
Output is correct |
4 |
Correct |
198 ms |
27204 KB |
Output is correct |
5 |
Correct |
207 ms |
27204 KB |
Output is correct |
6 |
Correct |
298 ms |
35432 KB |
Output is correct |
7 |
Correct |
358 ms |
35432 KB |
Output is correct |
8 |
Correct |
266 ms |
35432 KB |
Output is correct |
9 |
Correct |
349 ms |
35432 KB |
Output is correct |
10 |
Correct |
294 ms |
35432 KB |
Output is correct |
11 |
Correct |
234 ms |
35432 KB |
Output is correct |
12 |
Correct |
205 ms |
35432 KB |
Output is correct |
13 |
Correct |
250 ms |
35432 KB |
Output is correct |
14 |
Correct |
220 ms |
35432 KB |
Output is correct |
15 |
Correct |
199 ms |
35432 KB |
Output is correct |
16 |
Correct |
128 ms |
35432 KB |
Output is correct |
17 |
Correct |
149 ms |
35432 KB |
Output is correct |
18 |
Correct |
151 ms |
35432 KB |
Output is correct |
19 |
Correct |
185 ms |
35432 KB |
Output is correct |
20 |
Correct |
136 ms |
35432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35432 KB |
Output is correct |
2 |
Correct |
13 ms |
35432 KB |
Output is correct |
3 |
Incorrect |
15 ms |
35432 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
35432 KB |
Output is correct |
2 |
Correct |
318 ms |
35432 KB |
Output is correct |
3 |
Incorrect |
275 ms |
35432 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
13 ms |
9832 KB |
Output is correct |
3 |
Correct |
14 ms |
9888 KB |
Output is correct |
4 |
Correct |
11 ms |
9888 KB |
Output is correct |
5 |
Correct |
14 ms |
9888 KB |
Output is correct |
6 |
Correct |
13 ms |
9888 KB |
Output is correct |
7 |
Incorrect |
11 ms |
9944 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
Output is correct |
2 |
Correct |
13 ms |
9832 KB |
Output is correct |
3 |
Correct |
14 ms |
9888 KB |
Output is correct |
4 |
Correct |
11 ms |
9888 KB |
Output is correct |
5 |
Correct |
14 ms |
9888 KB |
Output is correct |
6 |
Correct |
13 ms |
9888 KB |
Output is correct |
7 |
Incorrect |
11 ms |
9944 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |