forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.cpp
More file actions
91 lines (83 loc) · 2.71 KB
/
I.cpp
File metadata and controls
91 lines (83 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
int V, res[20111], sz[20111];
vector<int> G[20111];
vector<int> child[20111];
struct UndirectedDfs {
vector<int> low, num, parent;
vector<bool> articulation;
int counter, root, children;
vector< pair<int,int> > bridges;
vector<int> cuts;
UndirectedDfs() : low(V, 0), num(V, -1), parent(V, 0), articulation(V, false),
counter(0), children(0) {
for(int i = 0; i < V; ++i) if (num[i] == -1) {
root = i; children = 0;
parent[i] = -1;
dfs(i);
articulation[root] = (children > 1);
}
for(int i = 0; i < V; ++i)
if (articulation[i]) cuts.push_back(i);
}
private:
void dfs(int u) {
low[u] = num[u] = counter++;
sz[u] = 1;
for(int j = 0; j < G[u].size(); ++j) {
int v = G[u][j];
if (num[v] == -1) {
parent[v] = u;
if (u == root) children++;
dfs(v);
sz[u] += sz[v];
if (low[v] >= num[u]) {
articulation[u] = true;
child[u].push_back(sz[v]);
}
if (low[v] > num[u]) bridges.push_back(make_pair(u, v));
low[u] = min(low[u], low[v]);
} else if (v != parent[u])
low[u] = min(low[u], num[v]);
}
}
};
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("travel.in", "r", stdin);
freopen("travel.out", "w", stdout);
int E;
while (scanf("%d%d", &V, &E) == 2) {
REP(i,V) {
G[i].clear();
child[i].clear();
}
REP(i,V) res[i] = 0;
REP(i,E) {
int u, v; scanf("%d%d", &u, &v);
--u; --v;
G[u].push_back(v);
G[v].push_back(u);
}
UndirectedDfs tree;
for(auto u : tree.cuts) {
int sum = 0;
for(int x : child[u]) {
res[u] += x * sum;
sum += x;
}
res[u] += (V - 1 - sum) * sum;
}
REP(i,V) res[i] += V - 1;
REP(i,V) printf("%d\n", res[i]);
}
}