#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e5+5;
const int maxm = 2e5+5;
int n, m;
vector< ii > adj[maxn];
int U[maxm], V[maxm];
int num[maxn], low[maxn];
int now = 1;
bool instack[maxm];
stack<int> s;
int compno;
int col[maxm];
vector<int> tree[2*(maxn+maxm)];
int cnt[2*(maxn+maxm)];
void tarjan(int u, int p)
{
num[u] = low[u] = now;
now++;
for(auto earn : adj[u])
{
int v = earn.X, id = earn.Y;
if(!num[v])
{
if(!instack[id])
{
s.push(id);
instack[id] = true;
}
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v]>= num[u])
{
// u is an articulation point
compno++;
while(!s.empty())
{
int x = s.top(); s.pop();
col[x] = compno;
if(x == id) break;
}
}
}
else if(v != p)
{
if(!instack[id])
{
s.push(id);
instack[id] = true;
}
low[u] = min(low[u], num[v]);
}
}
}
ll res;
void dfs(int u, int p)
{
cnt[u] = u>compno;
for(auto v : tree[u])
{
if(v == p) continue;
dfs(v, u);
cnt[u] += cnt[v];
}
}
void solve(int u, int p, int par_cnt)
{
//printf("%d (%d)\n", u, par_cnt);
if(u> compno)
{
ll tot = 0;
ll sq = 0;
for(auto v : tree[u])
{
if(v == p) continue;
tot += cnt[v];
sq += cnt[v]*cnt[v];
}
res += tot*tot-sq;
}
else
{
ll tot = 0;
ll sq = 0;
vector<int> ne;
for(auto v : tree[u])
{
if(v == p) continue;
if(!cnt[v]) continue;
ne.pb(cnt[v]);
tot += cnt[v];
sq += cnt[v]*cnt[v];
}
//printf("tot is %lld sq is %lld\n", tot, sq);
if(par_cnt)
{
tot += par_cnt; sq += 1LL*par_cnt*par_cnt;
}
for(auto x : ne)
{
res += (tot-1)*(tot-1) - (sq - (2LL*x - 1) );
}
}
//printf("res is now %lld\n", res);
int tot = u> compno;
for(auto v : tree[u])
{
if(v == p) continue;
tot += cnt[v];
}
for(auto v : tree[u])
{
if(v == p) continue;
solve(v, u, par_cnt+tot-cnt[v]);
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 1; i<= m; i++)
{
int u, v; scanf("%d %d", &u, &v);
U[i] = u; V[i] = v;
adj[u].pb(ii(v, i));
adj[v].pb(ii(u, i));
}
for(int i = 1; i<= n; i++)
{
if(num[i]) continue;
tarjan(i, 0);
if(!s.empty())
{
compno++;
while(!s.empty())
{
int x = s.top(); s.pop();
col[x] = compno;
}
}
}
for(int i = 1; i<= m; i++)
{
int x = col[i];
int u = U[i], v = V[i];
tree[compno+u].pb(x);
tree[x].pb(compno+u);
tree[compno+v].pb(x);
tree[x].pb(compno+v);
}
for(int i = 1; i<= compno+n; i++)
{
sort(tree[i].begin(), tree[i].end());
tree[i].resize(unique(tree[i].begin(), tree[i].end())-tree[i].begin());
}
for(int i = 1; i<= compno+n; i++)
{
if(cnt[i]) continue;
dfs(i, 0);
solve(i, 0, 0);
}
printf("%lld\n", res);
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:133:7: 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:136:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v; scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16764 KB |
Output is correct |
2 |
Correct |
17 ms |
16868 KB |
Output is correct |
3 |
Correct |
16 ms |
17072 KB |
Output is correct |
4 |
Correct |
15 ms |
17136 KB |
Output is correct |
5 |
Correct |
15 ms |
17136 KB |
Output is correct |
6 |
Correct |
15 ms |
17136 KB |
Output is correct |
7 |
Incorrect |
16 ms |
17136 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16764 KB |
Output is correct |
2 |
Correct |
17 ms |
16868 KB |
Output is correct |
3 |
Correct |
16 ms |
17072 KB |
Output is correct |
4 |
Correct |
15 ms |
17136 KB |
Output is correct |
5 |
Correct |
15 ms |
17136 KB |
Output is correct |
6 |
Correct |
15 ms |
17136 KB |
Output is correct |
7 |
Incorrect |
16 ms |
17136 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
36872 KB |
Output is correct |
2 |
Correct |
173 ms |
36872 KB |
Output is correct |
3 |
Incorrect |
296 ms |
40596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
40596 KB |
Output is correct |
2 |
Correct |
17 ms |
40596 KB |
Output is correct |
3 |
Correct |
20 ms |
40596 KB |
Output is correct |
4 |
Correct |
16 ms |
40596 KB |
Output is correct |
5 |
Correct |
20 ms |
40596 KB |
Output is correct |
6 |
Correct |
18 ms |
40596 KB |
Output is correct |
7 |
Correct |
24 ms |
40596 KB |
Output is correct |
8 |
Correct |
25 ms |
40596 KB |
Output is correct |
9 |
Correct |
17 ms |
40596 KB |
Output is correct |
10 |
Correct |
19 ms |
40596 KB |
Output is correct |
11 |
Correct |
18 ms |
40596 KB |
Output is correct |
12 |
Correct |
18 ms |
40596 KB |
Output is correct |
13 |
Correct |
20 ms |
40596 KB |
Output is correct |
14 |
Correct |
19 ms |
40596 KB |
Output is correct |
15 |
Correct |
18 ms |
40596 KB |
Output is correct |
16 |
Correct |
18 ms |
40596 KB |
Output is correct |
17 |
Correct |
18 ms |
40596 KB |
Output is correct |
18 |
Correct |
19 ms |
40596 KB |
Output is correct |
19 |
Correct |
19 ms |
40596 KB |
Output is correct |
20 |
Correct |
22 ms |
40596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
209 ms |
40596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
40596 KB |
Output is correct |
2 |
Correct |
21 ms |
40596 KB |
Output is correct |
3 |
Incorrect |
18 ms |
40596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
336 ms |
40596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16764 KB |
Output is correct |
2 |
Correct |
17 ms |
16868 KB |
Output is correct |
3 |
Correct |
16 ms |
17072 KB |
Output is correct |
4 |
Correct |
15 ms |
17136 KB |
Output is correct |
5 |
Correct |
15 ms |
17136 KB |
Output is correct |
6 |
Correct |
15 ms |
17136 KB |
Output is correct |
7 |
Incorrect |
16 ms |
17136 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
16764 KB |
Output is correct |
2 |
Correct |
17 ms |
16868 KB |
Output is correct |
3 |
Correct |
16 ms |
17072 KB |
Output is correct |
4 |
Correct |
15 ms |
17136 KB |
Output is correct |
5 |
Correct |
15 ms |
17136 KB |
Output is correct |
6 |
Correct |
15 ms |
17136 KB |
Output is correct |
7 |
Incorrect |
16 ms |
17136 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |