Submission #49840

# Submission time Handle Problem Language Result Execution time Memory
49840 2018-06-03T17:51:20 Z zemen Duathlon (APIO18_duathlon) C++17
49 / 100
302 ms 31908 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

const int maxn = 200100;
vector<int> g[maxn];
bool used[maxn];
int up[maxn];
int in[maxn];
int timer = 0;

bool cp[maxn];
int vid[maxn];
vector<vector<pair<int, int>>> comp;

int w[maxn];
vector<int> t[maxn];

vector<pair<int, int>> st;
void dfs(int u, int prev = -1) {
  in[u] = up[u] = timer++;
  used[u] = true;
  int nbad = prev != -1;
  for (int v : g[u]) {
    if (v == prev) {
      continue;
    }
    if (used[v] && in[v] >= in[u]) {
      continue;
    }
    auto ce = pair<int, int>(u, v);
    st.push_back(ce);
    if (!used[v]) {
      dfs(v, u);
      up[u] = min(up[u], up[v]);
      if (up[v] >= in[u]) {
        comp.emplace_back();
        ++nbad;
        while (true) {
          auto e = st.back();
          st.pop_back();
          comp.back().push_back(e);
          if (e == ce) {
            break;
          }
        }
      }
    } else {
      up[u] = min(up[u], in[v]);
    }
  }
  cp[u] = nbad >= 2;
}

ll ss[maxn];
ll cnt[maxn];
ll ts[maxn];
void pre(int u, int s, int prev = -1) {
  ts[u] = s;
  in[u] = timer++;
  cnt[u] = w[u];
  ss[u] = ll(s - 1) * (s - 2);
  for (int v : t[u]) {
    if (v == prev) {
      continue;
    }
    pre(v, s, u);
    cnt[u] += cnt[v];
    ss[u] -= cnt[v] * (cnt[v] - 1);
  }
  int cntup = s - cnt[u];
  ss[u] -= cntup * (cntup - 1);
}

int tsize(int u, int prev = -1) {
  used[u] = true;
  int res = w[u];
  for (int v : t[u]) {
    if (v == prev) {
      continue;
    }
    res += tsize(v, u);
  }
  return res;
}

signed main() {
#ifdef LOCAL
  assert(freopen("c.in", "r", stdin));
#endif
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      dfs(i);
      assert(st.empty());
    }
  }

  //for (int i = 0; i < n; ++i) {
    //cerr << cp[i] << ' ';
  //}
  //cerr << '\n';
  //for (auto v : comp) {
    //for (auto e : v) {
      //cerr << "(" << e.first << " " << e.second << ") ";
    //}
    //cerr << '\n';
  //}

  for (int i = 0; i < (int) comp.size(); ++i) {
    set<int> vs;
    for (auto e : comp[i]) {
      for (int u : {e.first, e.second}) {
        if (!cp[u]) {
          vs.insert(u);
        }
      }
    }
    w[i] = (int) vs.size();
    for (int u : vs) {
      vid[u] = i;
    }
  }
  int N = (int) comp.size();
  for (int i = 0; i < n; ++i) {
    if (cp[i]) {
      w[N] = 1;
      vid[i] = N++;
    }
  }
  for (int i = 0; i < (int) comp.size(); ++i) {
    for (auto e : comp[i]) {
      int u, v;
      tie(u, v) = e;
      if (vid[u] == vid[v]) {
        continue;
      }
      if (!cp[u]) {
        swap(u, v);
      }
      assert(cp[u]);
      if (cp[v]) {
        u = vid[u], v = vid[v];
        t[u].push_back(i);
        t[v].push_back(i);
        t[i].push_back(u);
        t[i].push_back(v);
        continue;
      }
      u = vid[u], v = vid[v];
      t[u].push_back(i);
      t[i].push_back(u);
    }
  }
  for (int i = 0; i < N; ++i) {
    sort(t[i].begin(), t[i].end());
    t[i].erase(unique(t[i].begin(), t[i].end()), t[i].end());
    //cerr << w[i] << ": ";
    //for (int v : t[i]) {
      //cerr << v+1 << ' ';
    //}
    //cerr << '\n';
  }
  
  memset(used, 0, sizeof(used));
  timer = 0;
  for (int i = 0; i < N; ++i) {
    if (used[i]) {
      continue;
    }
    int s = tsize(i);
    pre(i, s);
  }
  ll ans = 0;
  for (int i = 0; i < N; ++i) {
    ll cur = ss[i] * w[i];
    ans += cur;
    //cerr << "cur: " << i+1 << ' ' << cur << '\n';
    //cerr << "ts " << ss[i] << '\n';
    if (i < (int) comp.size()) {
      continue;
    }
    for (auto v : t[i]) {
      ll cntc;
      if (in[v] < in[i]) {
        cntc = cnt[i];
      } else {
        cntc = ts[i] - cnt[v];
      }
      ll fix = ss[v];
      fix += cntc * (cntc - 1);
      fix -= (ts[i] - 1) * (ts[i] - 2);
      ll ns = ts[i] - cntc;
      fix += ns * (ns - 1);
      //cerr << "fix: " << i+1 << ' ' << v+1 << ' ' << cntc << ' ' << ns << ' ' << fix << '\n';
      ans += fix;
    }
  }
  cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9976 KB Output is correct
2 Correct 9 ms 9976 KB Output is correct
3 Correct 11 ms 10164 KB Output is correct
4 Correct 10 ms 10164 KB Output is correct
5 Correct 9 ms 10164 KB Output is correct
6 Correct 10 ms 10164 KB Output is correct
7 Correct 10 ms 10164 KB Output is correct
8 Correct 10 ms 10164 KB Output is correct
9 Correct 9 ms 10164 KB Output is correct
10 Correct 12 ms 10196 KB Output is correct
11 Correct 9 ms 10196 KB Output is correct
12 Correct 9 ms 10196 KB Output is correct
13 Correct 9 ms 10196 KB Output is correct
14 Correct 10 ms 10196 KB Output is correct
15 Correct 10 ms 10236 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 11 ms 10240 KB Output is correct
18 Correct 10 ms 10240 KB Output is correct
19 Correct 9 ms 10240 KB Output is correct
20 Correct 13 ms 10340 KB Output is correct
21 Correct 9 ms 10340 KB Output is correct
22 Correct 10 ms 10340 KB Output is correct
23 Correct 10 ms 10340 KB Output is correct
24 Correct 11 ms 10340 KB Output is correct
25 Correct 11 ms 10340 KB Output is correct
26 Correct 10 ms 10340 KB Output is correct
27 Correct 10 ms 10340 KB Output is correct
28 Correct 10 ms 10340 KB Output is correct
29 Correct 9 ms 10340 KB Output is correct
30 Correct 12 ms 10340 KB Output is correct
31 Correct 11 ms 10340 KB Output is correct
32 Correct 9 ms 10340 KB Output is correct
33 Correct 9 ms 10340 KB Output is correct
34 Correct 9 ms 10340 KB Output is correct
35 Correct 9 ms 10340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9976 KB Output is correct
2 Correct 9 ms 9976 KB Output is correct
3 Correct 11 ms 10164 KB Output is correct
4 Correct 10 ms 10164 KB Output is correct
5 Correct 9 ms 10164 KB Output is correct
6 Correct 10 ms 10164 KB Output is correct
7 Correct 10 ms 10164 KB Output is correct
8 Correct 10 ms 10164 KB Output is correct
9 Correct 9 ms 10164 KB Output is correct
10 Correct 12 ms 10196 KB Output is correct
11 Correct 9 ms 10196 KB Output is correct
12 Correct 9 ms 10196 KB Output is correct
13 Correct 9 ms 10196 KB Output is correct
14 Correct 10 ms 10196 KB Output is correct
15 Correct 10 ms 10236 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 11 ms 10240 KB Output is correct
18 Correct 10 ms 10240 KB Output is correct
19 Correct 9 ms 10240 KB Output is correct
20 Correct 13 ms 10340 KB Output is correct
21 Correct 9 ms 10340 KB Output is correct
22 Correct 10 ms 10340 KB Output is correct
23 Correct 10 ms 10340 KB Output is correct
24 Correct 11 ms 10340 KB Output is correct
25 Correct 11 ms 10340 KB Output is correct
26 Correct 10 ms 10340 KB Output is correct
27 Correct 10 ms 10340 KB Output is correct
28 Correct 10 ms 10340 KB Output is correct
29 Correct 9 ms 10340 KB Output is correct
30 Correct 12 ms 10340 KB Output is correct
31 Correct 11 ms 10340 KB Output is correct
32 Correct 9 ms 10340 KB Output is correct
33 Correct 9 ms 10340 KB Output is correct
34 Correct 9 ms 10340 KB Output is correct
35 Correct 9 ms 10340 KB Output is correct
36 Correct 10 ms 10340 KB Output is correct
37 Correct 10 ms 10340 KB Output is correct
38 Correct 9 ms 10340 KB Output is correct
39 Correct 10 ms 10340 KB Output is correct
40 Correct 14 ms 10340 KB Output is correct
41 Correct 10 ms 10340 KB Output is correct
42 Correct 10 ms 10340 KB Output is correct
43 Correct 11 ms 10340 KB Output is correct
44 Correct 11 ms 10340 KB Output is correct
45 Correct 13 ms 10340 KB Output is correct
46 Correct 11 ms 10340 KB Output is correct
47 Correct 10 ms 10340 KB Output is correct
48 Correct 11 ms 10340 KB Output is correct
49 Correct 10 ms 10340 KB Output is correct
50 Correct 9 ms 10340 KB Output is correct
51 Correct 9 ms 10340 KB Output is correct
52 Correct 10 ms 10340 KB Output is correct
53 Correct 15 ms 10340 KB Output is correct
54 Correct 13 ms 10340 KB Output is correct
55 Correct 10 ms 10340 KB Output is correct
56 Correct 11 ms 10340 KB Output is correct
57 Correct 17 ms 10340 KB Output is correct
58 Correct 10 ms 10340 KB Output is correct
59 Correct 11 ms 10340 KB Output is correct
60 Correct 10 ms 10340 KB Output is correct
61 Correct 11 ms 10340 KB Output is correct
62 Correct 10 ms 10340 KB Output is correct
63 Correct 11 ms 10340 KB Output is correct
64 Correct 10 ms 10340 KB Output is correct
65 Correct 10 ms 10340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 31860 KB Output is correct
2 Correct 275 ms 31860 KB Output is correct
3 Incorrect 287 ms 31908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 31908 KB Output is correct
2 Correct 11 ms 31908 KB Output is correct
3 Correct 12 ms 31908 KB Output is correct
4 Correct 11 ms 31908 KB Output is correct
5 Correct 12 ms 31908 KB Output is correct
6 Correct 12 ms 31908 KB Output is correct
7 Correct 12 ms 31908 KB Output is correct
8 Correct 11 ms 31908 KB Output is correct
9 Correct 13 ms 31908 KB Output is correct
10 Correct 11 ms 31908 KB Output is correct
11 Correct 16 ms 31908 KB Output is correct
12 Correct 11 ms 31908 KB Output is correct
13 Correct 11 ms 31908 KB Output is correct
14 Correct 12 ms 31908 KB Output is correct
15 Correct 12 ms 31908 KB Output is correct
16 Correct 13 ms 31908 KB Output is correct
17 Correct 15 ms 31908 KB Output is correct
18 Correct 11 ms 31908 KB Output is correct
19 Correct 11 ms 31908 KB Output is correct
20 Correct 12 ms 31908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 281 ms 31908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 31908 KB Output is correct
2 Correct 13 ms 31908 KB Output is correct
3 Correct 15 ms 31908 KB Output is correct
4 Correct 12 ms 31908 KB Output is correct
5 Correct 13 ms 31908 KB Output is correct
6 Correct 12 ms 31908 KB Output is correct
7 Correct 11 ms 31908 KB Output is correct
8 Correct 11 ms 31908 KB Output is correct
9 Correct 12 ms 31908 KB Output is correct
10 Correct 15 ms 31908 KB Output is correct
11 Correct 12 ms 31908 KB Output is correct
12 Correct 14 ms 31908 KB Output is correct
13 Correct 14 ms 31908 KB Output is correct
14 Correct 12 ms 31908 KB Output is correct
15 Correct 16 ms 31908 KB Output is correct
16 Correct 13 ms 31908 KB Output is correct
17 Correct 13 ms 31908 KB Output is correct
18 Correct 14 ms 31908 KB Output is correct
19 Correct 12 ms 31908 KB Output is correct
20 Correct 12 ms 31908 KB Output is correct
21 Correct 12 ms 31908 KB Output is correct
22 Correct 12 ms 31908 KB Output is correct
23 Correct 12 ms 31908 KB Output is correct
24 Correct 13 ms 31908 KB Output is correct
25 Correct 13 ms 31908 KB Output is correct
26 Correct 12 ms 31908 KB Output is correct
27 Correct 9 ms 31908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 269 ms 31908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9976 KB Output is correct
2 Correct 9 ms 9976 KB Output is correct
3 Correct 11 ms 10164 KB Output is correct
4 Correct 10 ms 10164 KB Output is correct
5 Correct 9 ms 10164 KB Output is correct
6 Correct 10 ms 10164 KB Output is correct
7 Correct 10 ms 10164 KB Output is correct
8 Correct 10 ms 10164 KB Output is correct
9 Correct 9 ms 10164 KB Output is correct
10 Correct 12 ms 10196 KB Output is correct
11 Correct 9 ms 10196 KB Output is correct
12 Correct 9 ms 10196 KB Output is correct
13 Correct 9 ms 10196 KB Output is correct
14 Correct 10 ms 10196 KB Output is correct
15 Correct 10 ms 10236 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 11 ms 10240 KB Output is correct
18 Correct 10 ms 10240 KB Output is correct
19 Correct 9 ms 10240 KB Output is correct
20 Correct 13 ms 10340 KB Output is correct
21 Correct 9 ms 10340 KB Output is correct
22 Correct 10 ms 10340 KB Output is correct
23 Correct 10 ms 10340 KB Output is correct
24 Correct 11 ms 10340 KB Output is correct
25 Correct 11 ms 10340 KB Output is correct
26 Correct 10 ms 10340 KB Output is correct
27 Correct 10 ms 10340 KB Output is correct
28 Correct 10 ms 10340 KB Output is correct
29 Correct 9 ms 10340 KB Output is correct
30 Correct 12 ms 10340 KB Output is correct
31 Correct 11 ms 10340 KB Output is correct
32 Correct 9 ms 10340 KB Output is correct
33 Correct 9 ms 10340 KB Output is correct
34 Correct 9 ms 10340 KB Output is correct
35 Correct 9 ms 10340 KB Output is correct
36 Correct 10 ms 10340 KB Output is correct
37 Correct 10 ms 10340 KB Output is correct
38 Correct 9 ms 10340 KB Output is correct
39 Correct 10 ms 10340 KB Output is correct
40 Correct 14 ms 10340 KB Output is correct
41 Correct 10 ms 10340 KB Output is correct
42 Correct 10 ms 10340 KB Output is correct
43 Correct 11 ms 10340 KB Output is correct
44 Correct 11 ms 10340 KB Output is correct
45 Correct 13 ms 10340 KB Output is correct
46 Correct 11 ms 10340 KB Output is correct
47 Correct 10 ms 10340 KB Output is correct
48 Correct 11 ms 10340 KB Output is correct
49 Correct 10 ms 10340 KB Output is correct
50 Correct 9 ms 10340 KB Output is correct
51 Correct 9 ms 10340 KB Output is correct
52 Correct 10 ms 10340 KB Output is correct
53 Correct 15 ms 10340 KB Output is correct
54 Correct 13 ms 10340 KB Output is correct
55 Correct 10 ms 10340 KB Output is correct
56 Correct 11 ms 10340 KB Output is correct
57 Correct 17 ms 10340 KB Output is correct
58 Correct 10 ms 10340 KB Output is correct
59 Correct 11 ms 10340 KB Output is correct
60 Correct 10 ms 10340 KB Output is correct
61 Correct 11 ms 10340 KB Output is correct
62 Correct 10 ms 10340 KB Output is correct
63 Correct 11 ms 10340 KB Output is correct
64 Correct 10 ms 10340 KB Output is correct
65 Correct 10 ms 10340 KB Output is correct
66 Correct 10 ms 31908 KB Output is correct
67 Correct 10 ms 31908 KB Output is correct
68 Correct 14 ms 31908 KB Output is correct
69 Correct 11 ms 31908 KB Output is correct
70 Correct 13 ms 31908 KB Output is correct
71 Correct 11 ms 31908 KB Output is correct
72 Correct 12 ms 31908 KB Output is correct
73 Correct 11 ms 31908 KB Output is correct
74 Correct 13 ms 31908 KB Output is correct
75 Correct 14 ms 31908 KB Output is correct
76 Correct 12 ms 31908 KB Output is correct
77 Correct 13 ms 31908 KB Output is correct
78 Correct 11 ms 31908 KB Output is correct
79 Correct 11 ms 31908 KB Output is correct
80 Correct 12 ms 31908 KB Output is correct
81 Correct 11 ms 31908 KB Output is correct
82 Correct 11 ms 31908 KB Output is correct
83 Correct 12 ms 31908 KB Output is correct
84 Correct 12 ms 31908 KB Output is correct
85 Correct 12 ms 31908 KB Output is correct
86 Correct 12 ms 31908 KB Output is correct
87 Correct 13 ms 31908 KB Output is correct
88 Correct 12 ms 31908 KB Output is correct
89 Correct 13 ms 31908 KB Output is correct
90 Correct 11 ms 31908 KB Output is correct
91 Correct 12 ms 31908 KB Output is correct
92 Correct 11 ms 31908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9976 KB Output is correct
2 Correct 9 ms 9976 KB Output is correct
3 Correct 11 ms 10164 KB Output is correct
4 Correct 10 ms 10164 KB Output is correct
5 Correct 9 ms 10164 KB Output is correct
6 Correct 10 ms 10164 KB Output is correct
7 Correct 10 ms 10164 KB Output is correct
8 Correct 10 ms 10164 KB Output is correct
9 Correct 9 ms 10164 KB Output is correct
10 Correct 12 ms 10196 KB Output is correct
11 Correct 9 ms 10196 KB Output is correct
12 Correct 9 ms 10196 KB Output is correct
13 Correct 9 ms 10196 KB Output is correct
14 Correct 10 ms 10196 KB Output is correct
15 Correct 10 ms 10236 KB Output is correct
16 Correct 11 ms 10240 KB Output is correct
17 Correct 11 ms 10240 KB Output is correct
18 Correct 10 ms 10240 KB Output is correct
19 Correct 9 ms 10240 KB Output is correct
20 Correct 13 ms 10340 KB Output is correct
21 Correct 9 ms 10340 KB Output is correct
22 Correct 10 ms 10340 KB Output is correct
23 Correct 10 ms 10340 KB Output is correct
24 Correct 11 ms 10340 KB Output is correct
25 Correct 11 ms 10340 KB Output is correct
26 Correct 10 ms 10340 KB Output is correct
27 Correct 10 ms 10340 KB Output is correct
28 Correct 10 ms 10340 KB Output is correct
29 Correct 9 ms 10340 KB Output is correct
30 Correct 12 ms 10340 KB Output is correct
31 Correct 11 ms 10340 KB Output is correct
32 Correct 9 ms 10340 KB Output is correct
33 Correct 9 ms 10340 KB Output is correct
34 Correct 9 ms 10340 KB Output is correct
35 Correct 9 ms 10340 KB Output is correct
36 Correct 10 ms 10340 KB Output is correct
37 Correct 10 ms 10340 KB Output is correct
38 Correct 9 ms 10340 KB Output is correct
39 Correct 10 ms 10340 KB Output is correct
40 Correct 14 ms 10340 KB Output is correct
41 Correct 10 ms 10340 KB Output is correct
42 Correct 10 ms 10340 KB Output is correct
43 Correct 11 ms 10340 KB Output is correct
44 Correct 11 ms 10340 KB Output is correct
45 Correct 13 ms 10340 KB Output is correct
46 Correct 11 ms 10340 KB Output is correct
47 Correct 10 ms 10340 KB Output is correct
48 Correct 11 ms 10340 KB Output is correct
49 Correct 10 ms 10340 KB Output is correct
50 Correct 9 ms 10340 KB Output is correct
51 Correct 9 ms 10340 KB Output is correct
52 Correct 10 ms 10340 KB Output is correct
53 Correct 15 ms 10340 KB Output is correct
54 Correct 13 ms 10340 KB Output is correct
55 Correct 10 ms 10340 KB Output is correct
56 Correct 11 ms 10340 KB Output is correct
57 Correct 17 ms 10340 KB Output is correct
58 Correct 10 ms 10340 KB Output is correct
59 Correct 11 ms 10340 KB Output is correct
60 Correct 10 ms 10340 KB Output is correct
61 Correct 11 ms 10340 KB Output is correct
62 Correct 10 ms 10340 KB Output is correct
63 Correct 11 ms 10340 KB Output is correct
64 Correct 10 ms 10340 KB Output is correct
65 Correct 10 ms 10340 KB Output is correct
66 Correct 302 ms 31860 KB Output is correct
67 Correct 275 ms 31860 KB Output is correct
68 Incorrect 287 ms 31908 KB Output isn't correct
69 Halted 0 ms 0 KB -