#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 21, MOD = (int)1e9+7;
int n,m;
vector<vector<int>> bcc(vector<vector<int>> &g,vector<int> &sz){
vector<bool> ap(n+1,0);
vector<int> tin(n+1,0),up(n+1,0),id(n+1,0);
vector<int> stk;
vector<vector<int>> comps,G;
function <void(int,int,int&)> dfs = [&](int v,int pr,int &t){
tin[v] = up[v] = ++t;
stk.push_back(v);
int cc =0;
for(int to:g[v]){
if(to == pr)continue;
if(!tin[to]){
dfs(to,v,t);
cc++;
up[v] = min(up[v],up[to]);
if(up[to] >= tin[v]){
if(pr != -1){
ap[v] = 1;
}
comps.push_back({v});
while(comps.back().back() != to){
comps.back().push_back(stk.back());
stk.pop_back();
}
}
}else{
up[v] = min(up[v],tin[to]);
}
}
if(pr == -1){
ap[v] = (cc > 1);
}
};
int timer = 0;
for(int i = 1;i <= n;i++){
if(!tin[i]){
dfs(i,-1,timer);
}
}
function<vector<vector<int>>()> build_tree = [&]() {
vector<vector<int>> t(1);
int it = 0;
sz = vector<int> (1,0);
for(int i = 1;i <= n;i++){
if(ap[i]){
id[i] = ++it;
sz.push_back(1);
sz[id[i]] = 1;
t.push_back({});
}
}
for(auto c:comps){
it++;
sz.push_back(0);
t.push_back({});
for(auto j:c){
if(ap[j]){
t[id[j]].push_back(it);
t[it].push_back(id[j]);
}else{
id[j] = it;
sz[it]++;
}
}
}
return t;
};
return build_tree();
}
vector<vector<int>> g(N),G;
ll calc(vector<int> &S){
ll s = 0,d = 0,t = 0;
for(auto i:S){
t += i *1ll * d;
d += s * 1ll * i;
s += i;
}
return t;
}
int bf_n;
vector<int> sz,ss;
bool vis[N];
void go(int v,int pr = -1){
vis[v] = 1;
ss.push_back(sz[v]);
bf_n += sz[v];
for(int to:G[v]){
if(to == pr)continue;
go(to,v);
}
}
void test(){
cin >> n >> m;
for(int i = 1;i <= m;i++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
G = bcc(g,sz);
n = (int)G.size() - 1;
ll res = 0;
for(int i = 1;i <= n;i++){
if(!vis[i])
{
go(i);
res += calc(ss);
ss.clear();
}
}
for(int i = 1;i <= n;i++){
ll val = sz[i] * 1ll * (sz[i] - 1),k = val / 2;
bf_n = 0;
go(i);
val = val * 1ll * (bf_n - sz[i]);
res += val;
res += k * 1ll * (ll)G[i].size();
ll t = sz[i];
res += t * (t - 1) / 2 * (t - 2);
}
cout << res * 2 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
6 ms |
9820 KB |
Output is correct |
7 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
6 ms |
9820 KB |
Output is correct |
7 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
29516 KB |
Output is correct |
2 |
Correct |
41 ms |
30612 KB |
Output is correct |
3 |
Execution timed out |
1045 ms |
292404 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
26560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1073 ms |
289928 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
26552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
289820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
6 ms |
9820 KB |
Output is correct |
7 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9820 KB |
Output is correct |
6 |
Correct |
6 ms |
9820 KB |
Output is correct |
7 |
Incorrect |
3 ms |
9820 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |