#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define per(i,a,b) for(int i = a; i >= b; i--)
#define inf 1000000000000000000
#define sz(x) (ll)(x).size()
ll n,m,u,v,cur;
vector<vi> edges;
vi _,vis,tree,vis2,cnt;
void dfs(ll a){
tree.push_back(a);
rep(i,0,sz(edges[a])){
ll b = edges[a][i];
if (vis[b] == 0) {
vis[b] = cur;
dfs(b);
}
}
}
ll dfs2(ll a){
vis2[a] = 1;
if (cnt[a] != -1) return cnt[a];
ll ans = 1;
rep(i,0,sz(edges[a])){
ll b = edges[a][i];
if (vis2[b] == 0) {
ans += dfs2(b);
}
}
return cnt[a] = ans;
}
ll center(ll a){
ll ans = 0,big = 0;
rep(i,0,sz(edges[a])){
ll b = edges[a][i];
if (cnt[b] > cnt[a]) { continue; }
ans += (sz(tree)-1-cnt[b])*cnt[b];
big += cnt[b];
}
ans += big*(sz(tree)-1-big);
return ans;
}
int main(){
cin >> n >> m;
edges.assign(n,_);
rep(i,0,m){
cin >> u >> v; u--; v--;
edges[u].push_back(v);
edges[v].push_back(u);
}
vis.assign(n,0);
cur = 1; ll res = 0;
rep(i,0,n){
if (vis[i] != 0) continue;
tree.clear();
if (vis[i] == 0) { vis[i] = cur; dfs(i); cur++; }
vis2.assign(n,0); cnt.assign(n,-1); dfs2(tree[0]);
rep(i,0,sz(tree)){ res += center(tree[i]); cout << res << " " << i << endl; }
}
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
434 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
469 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
456 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |