#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define test(x) cout << "Line(" << __LINE__ << ") " #x << ' ' << x << endl
#define printv(x) {\
for (auto i : x) cout << i << ' ';\
cout << endl;\
}
#define pii pair <int, int>
#define pll pair <lli, lli>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<typename A, typename B>
ostream& operator << (ostream& o, pair<A, B> a){
return o << a.X << ' ' << a.Y;
}
template<typename A, typename B>
istream& operator >> (istream& o, pair<A, B> &a){
return o >> a.X >> a.Y;
}
const int mod = 1e9 + 7, abc = 864197532, N = 20, logN = 17, K = 333;
vector <int> adj[N], bcc[N], cnt[N];
int low[N], dep[N], nbcc = 0;
bool vis[N], cut[N];
stack <int> stk;
void dfs(int v, int pa) {
vis[v] = true;
low[v] = dep[v] = ~pa ? dep[pa] + 1 : 0;
stk.push(v);
int ch = 0;
for (int u : adj[v]) if (u != pa) {
if (vis[u]) low[v] = min(low[v], dep[u]);
else {
dfs(u, v);
low[v] = min(low[v], low[u]);
ch++;
if (low[u] >= dep[v]) {
cut[v] = true;
int x;
do {
x = stk.top(); stk.pop();
bcc[x].pb(nbcc);
cnt[nbcc].pb(x);
} while (x != u);
cnt[nbcc].pb(v);
bcc[v].pb(nbcc++);
}
}
}
if (pa == -1 && ch < 2) cut[v] = false;
}
vector <int> newadj[N * 2];
bool vis2[N * 2];
int sz[N * 2], p[N * 2], val[N * 2];
vector <int> cc;
lli C2(int n) {
return 1ll * n * (n - 1);
}
void dfs2(int v, int pa) {
sz[v] = val[v];
vis2[v] = true;
p[v] = pa;
cc.pb(v);
for (int u : newadj[v]) if (u != pa) {
dfs2(u, v);
sz[v] += sz[u];
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
if (n <= 2) {
cout << 0 << endl;
return 0;
}
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v, --u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i < n; ++i) if (!vis[i]) dfs(i, -1);
for (int i = 0; i < n; ++i) if (cut[i]) {
for (int j : bcc[i]) {
newadj[i].pb(j + N);
newadj[j + N].pb(i);
}
}
for (int i = 0; i < nbcc; ++i) {
for (int j : cnt[i]) {
if (!cut[j]) val[i + N]++;
}
}
for (int i = 0; i < n; ++i) if (cut[i]) val[i]++;
lli ans = 0;
for (int i = N; i < nbcc + N; ++i) if (!vis2[i]) {
dfs2(i, -1);
int m = sz[i];
ans += C2(m - 1) * m;
for (int j : cc) {
if (j < n) {
for (int k : newadj[j]) {
if (k == p[j]) ans -= C2(sz[i] - sz[j] - val[k]);
else ans -= C2(sz[k] - val[k]);
}
} else {
for (int k : newadj[j]) {
if (k == p[j]) ans -= C2(sz[i] - sz[j]) * val[j];
else ans -= C2(sz[k]) * val[j];
}
}
}
cc.clear();
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
620 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
620 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |