#pragma optimize("Bismillahirrahmanirrahim")
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█■█
//█─█─█▄─█▄─█─█─█─█
//Allahuekber
//ahmet23 orz...
//FatihSultanMehmedHan
//YavuzSultanSelimHan
//AbdulhamidHan
//Sani buyuk Osman Pasa Plevneden cikmam diyor
#define author tolbi
#include <bits/stdc++.h>
using namespace std;
#define deci(x) int x;cin>>x;
#define decstr(x) string x;cin>>x;
#define sortarr(x) sort(x.begin(), x.end())
#define sortrarr(x) sort(x.rbegin(), x.rend())
#define rev(x) reverse(x.begin(), x.end())
#define cinarr(x) for (auto &it : x) cin>>it;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
#define tol(bi) (1LL<<((int)(bi)))
#define endl '\n'
#define int long long
//mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count());
int solve(int n, vector<int> u, vector<int> v){
int totn = n;
vector<vector<int>> arr(n);
for (int i = 0; i < u.size(); i++){
arr[u[i]].push_back(v[i]);
arr[v[i]].push_back(u[i]);
}
vector<int> dept(n,-1);
vector<int> stk;
vector<int> ele(n);
vector<int> sz;
vector<vector<int>> hehe;
function<int(int,int)> f;
vector<int> par(n);
f = [&](int node, int lnode)->int{
if (lnode!=-1) par[node]=lnode;
if (lnode!=-1) dept[node]=dept[lnode]+1;
int mide = dept[node];
for (int i = 0; i < arr[node].size(); i++){
if (arr[node][i]==lnode) continue;
if (dept[arr[node][i]]!=-1){
mide=min(mide,dept[arr[node][i]]);
continue;
}
mide=min(mide,f(arr[node][i],node));
}
stk.push_back(node);
if (mide==dept[node]){
sz.push_back(0);
while (stk.size()){
ele[stk.back()]=hehe.size();
stk.pop_back();
sz.back()++;
}
hehe.push_back(vector<int>());
}
return mide;
};
dept[0]=0;
f(0,-1);
for (int i = 1; i < n; i++){
if (ele[i]!=ele[par[i]]){
hehe[ele[i]].push_back(ele[par[i]]);
hehe[ele[par[i]]].push_back(ele[i]);
}
}
n=hehe.size();
swap(arr,hehe);
int ans = 0;
for (int i = 0; i < sz.size(); i++){
ans+=sz[i]*(sz[i]-1)*(sz[i]-2);
}//3 from cycle
vector<int> subsz(n);
//coutarr(sz);
f = [&](int node, int lnode)->int{
subsz[node]=sz[node];
for (int i = 0; i < arr[node].size(); i++){
if (arr[node][i]==lnode) continue;
f(arr[node][i],node);
subsz[node]+=subsz[arr[node][i]];
ans+=(subsz[arr[node][i]])*(sz[node])*(totn-subsz[arr[node][i]]-sz[node]);
//2 from outside, 1 from cycle
ans+=(sz[node]-1)*(sz[node]-1)*subsz[arr[node][i]]*2;
//2 from cycle, 1 from outside
}
ans+=(sz[node]-1)*(sz[node]-1)*(totn-subsz[node])*2;
ans+=(sz[node])*(totn-subsz[node])*(subsz[node]-sz[node]);
return 23;
};
f(0,-1);
return ans;
}
int32_t main(){
int T = 1;
int tno = 0;
while (T-(tno++)){
deci(n);deci(m);
vector<int> par(n);
iota(par.begin(), par.end(), 0);
function<int(int)> find;
find = [&](int node)->int{
if (par[node]==node) return node;
return par[node]=find(par[node]);
};
vector<pair<int,int>> edges(m);
for (int i = 0; i < m; i++){
cin>>edges[i].first>>edges[i].second;
edges[i].first--,edges[i].second--;
int u = edges[i].first;
int v = edges[i].second;
u=find(u);
v=find(v);
par[u]=v;
}
map<int,int> mp;
map<int,vector<int>> u;
map<int,vector<int>> v;
vector<int> indis(n);
for (int i = 0; i < n; i++){
indis[i]=mp[find(i)]++;
}
for (int i = 0; i < m; i++){
u[find(edges[i].first)].push_back(indis[edges[i].first]);
v[find(edges[i].first)].push_back(indis[edges[i].second]);
}
int ans = 0;
for (auto it : mp){
ans+=solve(it.second,u[it.first],v[it.first]);
}
cout<<ans<<endl;
}
}
Compilation message
count_triplets.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("Bismillahirrahmanirrahim")
|
count_triplets.cpp: In function 'long long int solve(long long int, std::vector<long long int>, std::vector<long long int>)':
count_triplets.cpp:28:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i = 0; i < u.size(); i++){
| ~~^~~~~~~~~~
count_triplets.cpp: In lambda function:
count_triplets.cpp:43:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'long long int solve(long long int, std::vector<long long int>, std::vector<long long int>)':
count_triplets.cpp:74:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < sz.size(); i++){
| ~~^~~~~~~~~~~
count_triplets.cpp: In lambda function:
count_triplets.cpp:81:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
483 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
483 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
25756 KB |
Output is correct |
2 |
Correct |
81 ms |
25680 KB |
Output is correct |
3 |
Correct |
131 ms |
20728 KB |
Output is correct |
4 |
Correct |
80 ms |
20920 KB |
Output is correct |
5 |
Correct |
75 ms |
15744 KB |
Output is correct |
6 |
Correct |
82 ms |
22436 KB |
Output is correct |
7 |
Correct |
80 ms |
11768 KB |
Output is correct |
8 |
Correct |
84 ms |
16472 KB |
Output is correct |
9 |
Correct |
81 ms |
9204 KB |
Output is correct |
10 |
Correct |
75 ms |
10764 KB |
Output is correct |
11 |
Correct |
114 ms |
11912 KB |
Output is correct |
12 |
Correct |
109 ms |
11740 KB |
Output is correct |
13 |
Correct |
113 ms |
13388 KB |
Output is correct |
14 |
Correct |
110 ms |
13196 KB |
Output is correct |
15 |
Correct |
112 ms |
16172 KB |
Output is correct |
16 |
Correct |
107 ms |
16080 KB |
Output is correct |
17 |
Correct |
74 ms |
23728 KB |
Output is correct |
18 |
Correct |
82 ms |
23732 KB |
Output is correct |
19 |
Correct |
76 ms |
23744 KB |
Output is correct |
20 |
Correct |
82 ms |
23644 KB |
Output is correct |
21 |
Correct |
78 ms |
23724 KB |
Output is correct |
22 |
Correct |
84 ms |
23716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
436 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
23088 KB |
Output is correct |
2 |
Correct |
108 ms |
22864 KB |
Output is correct |
3 |
Correct |
96 ms |
22928 KB |
Output is correct |
4 |
Correct |
102 ms |
22836 KB |
Output is correct |
5 |
Correct |
97 ms |
22836 KB |
Output is correct |
6 |
Correct |
104 ms |
35700 KB |
Output is correct |
7 |
Correct |
119 ms |
27716 KB |
Output is correct |
8 |
Correct |
132 ms |
26420 KB |
Output is correct |
9 |
Correct |
103 ms |
25140 KB |
Output is correct |
10 |
Correct |
97 ms |
15368 KB |
Output is correct |
11 |
Correct |
94 ms |
21052 KB |
Output is correct |
12 |
Correct |
88 ms |
11124 KB |
Output is correct |
13 |
Correct |
83 ms |
10292 KB |
Output is correct |
14 |
Correct |
107 ms |
8324 KB |
Output is correct |
15 |
Correct |
108 ms |
10264 KB |
Output is correct |
16 |
Correct |
101 ms |
15828 KB |
Output is correct |
17 |
Correct |
89 ms |
24808 KB |
Output is correct |
18 |
Correct |
78 ms |
24208 KB |
Output is correct |
19 |
Correct |
81 ms |
24952 KB |
Output is correct |
20 |
Correct |
78 ms |
23984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Runtime error |
379 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
23644 KB |
Output is correct |
2 |
Correct |
105 ms |
23004 KB |
Output is correct |
3 |
Runtime error |
515 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
483 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
483 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |