Submission #49839

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

const int maxn = 100100;
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 13 ms 5112 KB Output is correct
2 Correct 7 ms 5352 KB Output is correct
3 Correct 6 ms 5352 KB Output is correct
4 Correct 7 ms 5352 KB Output is correct
5 Correct 12 ms 5352 KB Output is correct
6 Correct 6 ms 5384 KB Output is correct
7 Correct 9 ms 5384 KB Output is correct
8 Correct 6 ms 5408 KB Output is correct
9 Correct 6 ms 5408 KB Output is correct
10 Correct 6 ms 5408 KB Output is correct
11 Correct 7 ms 5416 KB Output is correct
12 Correct 6 ms 5416 KB Output is correct
13 Correct 6 ms 5416 KB Output is correct
14 Correct 6 ms 5416 KB Output is correct
15 Correct 6 ms 5416 KB Output is correct
16 Correct 5 ms 5416 KB Output is correct
17 Correct 5 ms 5416 KB Output is correct
18 Correct 18 ms 5416 KB Output is correct
19 Correct 6 ms 5416 KB Output is correct
20 Correct 6 ms 5460 KB Output is correct
21 Correct 6 ms 5460 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 11 ms 5460 KB Output is correct
24 Correct 6 ms 5460 KB Output is correct
25 Correct 5 ms 5460 KB Output is correct
26 Correct 7 ms 5460 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 5 ms 5460 KB Output is correct
29 Correct 6 ms 5460 KB Output is correct
30 Correct 6 ms 5460 KB Output is correct
31 Correct 6 ms 5460 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 10 ms 5460 KB Output is correct
34 Correct 7 ms 5460 KB Output is correct
35 Correct 5 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5112 KB Output is correct
2 Correct 7 ms 5352 KB Output is correct
3 Correct 6 ms 5352 KB Output is correct
4 Correct 7 ms 5352 KB Output is correct
5 Correct 12 ms 5352 KB Output is correct
6 Correct 6 ms 5384 KB Output is correct
7 Correct 9 ms 5384 KB Output is correct
8 Correct 6 ms 5408 KB Output is correct
9 Correct 6 ms 5408 KB Output is correct
10 Correct 6 ms 5408 KB Output is correct
11 Correct 7 ms 5416 KB Output is correct
12 Correct 6 ms 5416 KB Output is correct
13 Correct 6 ms 5416 KB Output is correct
14 Correct 6 ms 5416 KB Output is correct
15 Correct 6 ms 5416 KB Output is correct
16 Correct 5 ms 5416 KB Output is correct
17 Correct 5 ms 5416 KB Output is correct
18 Correct 18 ms 5416 KB Output is correct
19 Correct 6 ms 5416 KB Output is correct
20 Correct 6 ms 5460 KB Output is correct
21 Correct 6 ms 5460 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 11 ms 5460 KB Output is correct
24 Correct 6 ms 5460 KB Output is correct
25 Correct 5 ms 5460 KB Output is correct
26 Correct 7 ms 5460 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 5 ms 5460 KB Output is correct
29 Correct 6 ms 5460 KB Output is correct
30 Correct 6 ms 5460 KB Output is correct
31 Correct 6 ms 5460 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 10 ms 5460 KB Output is correct
34 Correct 7 ms 5460 KB Output is correct
35 Correct 5 ms 5460 KB Output is correct
36 Correct 6 ms 5460 KB Output is correct
37 Correct 7 ms 5460 KB Output is correct
38 Correct 7 ms 5500 KB Output is correct
39 Correct 7 ms 5500 KB Output is correct
40 Correct 7 ms 5500 KB Output is correct
41 Correct 6 ms 5500 KB Output is correct
42 Correct 6 ms 5500 KB Output is correct
43 Correct 6 ms 5500 KB Output is correct
44 Correct 7 ms 5500 KB Output is correct
45 Correct 7 ms 5500 KB Output is correct
46 Correct 7 ms 5500 KB Output is correct
47 Correct 6 ms 5500 KB Output is correct
48 Correct 7 ms 5500 KB Output is correct
49 Correct 7 ms 5500 KB Output is correct
50 Correct 7 ms 5500 KB Output is correct
51 Correct 7 ms 5500 KB Output is correct
52 Correct 6 ms 5500 KB Output is correct
53 Correct 6 ms 5500 KB Output is correct
54 Correct 5 ms 5500 KB Output is correct
55 Correct 7 ms 5500 KB Output is correct
56 Correct 6 ms 5500 KB Output is correct
57 Correct 6 ms 5500 KB Output is correct
58 Correct 6 ms 5500 KB Output is correct
59 Correct 6 ms 5500 KB Output is correct
60 Correct 16 ms 5500 KB Output is correct
61 Correct 6 ms 5500 KB Output is correct
62 Correct 7 ms 5500 KB Output is correct
63 Correct 7 ms 5500 KB Output is correct
64 Correct 6 ms 5500 KB Output is correct
65 Correct 7 ms 5500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 26996 KB Output is correct
2 Correct 253 ms 27156 KB Output is correct
3 Incorrect 264 ms 27156 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27156 KB Output is correct
2 Correct 8 ms 27156 KB Output is correct
3 Correct 7 ms 27156 KB Output is correct
4 Correct 7 ms 27156 KB Output is correct
5 Correct 7 ms 27156 KB Output is correct
6 Correct 8 ms 27156 KB Output is correct
7 Correct 8 ms 27156 KB Output is correct
8 Correct 8 ms 27156 KB Output is correct
9 Correct 8 ms 27156 KB Output is correct
10 Correct 7 ms 27156 KB Output is correct
11 Correct 9 ms 27156 KB Output is correct
12 Correct 8 ms 27156 KB Output is correct
13 Correct 19 ms 27156 KB Output is correct
14 Correct 8 ms 27156 KB Output is correct
15 Correct 9 ms 27156 KB Output is correct
16 Correct 7 ms 27156 KB Output is correct
17 Correct 8 ms 27156 KB Output is correct
18 Correct 7 ms 27156 KB Output is correct
19 Correct 8 ms 27156 KB Output is correct
20 Correct 7 ms 27156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 179 ms 31152 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31152 KB Output is correct
2 Correct 7 ms 31152 KB Output is correct
3 Correct 7 ms 31152 KB Output is correct
4 Correct 7 ms 31152 KB Output is correct
5 Correct 8 ms 31152 KB Output is correct
6 Correct 9 ms 31152 KB Output is correct
7 Correct 7 ms 31152 KB Output is correct
8 Correct 8 ms 31152 KB Output is correct
9 Correct 7 ms 31152 KB Output is correct
10 Correct 7 ms 31152 KB Output is correct
11 Correct 7 ms 31152 KB Output is correct
12 Correct 10 ms 31152 KB Output is correct
13 Correct 10 ms 31152 KB Output is correct
14 Correct 9 ms 31152 KB Output is correct
15 Correct 8 ms 31152 KB Output is correct
16 Correct 10 ms 31152 KB Output is correct
17 Correct 8 ms 31152 KB Output is correct
18 Correct 8 ms 31152 KB Output is correct
19 Correct 7 ms 31152 KB Output is correct
20 Correct 7 ms 31152 KB Output is correct
21 Correct 7 ms 31152 KB Output is correct
22 Correct 7 ms 31152 KB Output is correct
23 Correct 10 ms 31152 KB Output is correct
24 Correct 7 ms 31152 KB Output is correct
25 Correct 6 ms 31152 KB Output is correct
26 Correct 8 ms 31152 KB Output is correct
27 Correct 7 ms 31152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 231 ms 31188 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5112 KB Output is correct
2 Correct 7 ms 5352 KB Output is correct
3 Correct 6 ms 5352 KB Output is correct
4 Correct 7 ms 5352 KB Output is correct
5 Correct 12 ms 5352 KB Output is correct
6 Correct 6 ms 5384 KB Output is correct
7 Correct 9 ms 5384 KB Output is correct
8 Correct 6 ms 5408 KB Output is correct
9 Correct 6 ms 5408 KB Output is correct
10 Correct 6 ms 5408 KB Output is correct
11 Correct 7 ms 5416 KB Output is correct
12 Correct 6 ms 5416 KB Output is correct
13 Correct 6 ms 5416 KB Output is correct
14 Correct 6 ms 5416 KB Output is correct
15 Correct 6 ms 5416 KB Output is correct
16 Correct 5 ms 5416 KB Output is correct
17 Correct 5 ms 5416 KB Output is correct
18 Correct 18 ms 5416 KB Output is correct
19 Correct 6 ms 5416 KB Output is correct
20 Correct 6 ms 5460 KB Output is correct
21 Correct 6 ms 5460 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 11 ms 5460 KB Output is correct
24 Correct 6 ms 5460 KB Output is correct
25 Correct 5 ms 5460 KB Output is correct
26 Correct 7 ms 5460 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 5 ms 5460 KB Output is correct
29 Correct 6 ms 5460 KB Output is correct
30 Correct 6 ms 5460 KB Output is correct
31 Correct 6 ms 5460 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 10 ms 5460 KB Output is correct
34 Correct 7 ms 5460 KB Output is correct
35 Correct 5 ms 5460 KB Output is correct
36 Correct 6 ms 5460 KB Output is correct
37 Correct 7 ms 5460 KB Output is correct
38 Correct 7 ms 5500 KB Output is correct
39 Correct 7 ms 5500 KB Output is correct
40 Correct 7 ms 5500 KB Output is correct
41 Correct 6 ms 5500 KB Output is correct
42 Correct 6 ms 5500 KB Output is correct
43 Correct 6 ms 5500 KB Output is correct
44 Correct 7 ms 5500 KB Output is correct
45 Correct 7 ms 5500 KB Output is correct
46 Correct 7 ms 5500 KB Output is correct
47 Correct 6 ms 5500 KB Output is correct
48 Correct 7 ms 5500 KB Output is correct
49 Correct 7 ms 5500 KB Output is correct
50 Correct 7 ms 5500 KB Output is correct
51 Correct 7 ms 5500 KB Output is correct
52 Correct 6 ms 5500 KB Output is correct
53 Correct 6 ms 5500 KB Output is correct
54 Correct 5 ms 5500 KB Output is correct
55 Correct 7 ms 5500 KB Output is correct
56 Correct 6 ms 5500 KB Output is correct
57 Correct 6 ms 5500 KB Output is correct
58 Correct 6 ms 5500 KB Output is correct
59 Correct 6 ms 5500 KB Output is correct
60 Correct 16 ms 5500 KB Output is correct
61 Correct 6 ms 5500 KB Output is correct
62 Correct 7 ms 5500 KB Output is correct
63 Correct 7 ms 5500 KB Output is correct
64 Correct 6 ms 5500 KB Output is correct
65 Correct 7 ms 5500 KB Output is correct
66 Correct 6 ms 31188 KB Output is correct
67 Correct 8 ms 31188 KB Output is correct
68 Correct 8 ms 31188 KB Output is correct
69 Correct 8 ms 31188 KB Output is correct
70 Correct 9 ms 31188 KB Output is correct
71 Correct 8 ms 31188 KB Output is correct
72 Correct 7 ms 31188 KB Output is correct
73 Correct 8 ms 31188 KB Output is correct
74 Correct 7 ms 31188 KB Output is correct
75 Correct 8 ms 31188 KB Output is correct
76 Correct 7 ms 31188 KB Output is correct
77 Correct 7 ms 31188 KB Output is correct
78 Correct 6 ms 31188 KB Output is correct
79 Correct 6 ms 31188 KB Output is correct
80 Correct 6 ms 31188 KB Output is correct
81 Correct 6 ms 31188 KB Output is correct
82 Correct 6 ms 31188 KB Output is correct
83 Correct 7 ms 31188 KB Output is correct
84 Correct 7 ms 31188 KB Output is correct
85 Correct 7 ms 31188 KB Output is correct
86 Correct 6 ms 31188 KB Output is correct
87 Correct 7 ms 31188 KB Output is correct
88 Correct 8 ms 31188 KB Output is correct
89 Correct 7 ms 31188 KB Output is correct
90 Correct 6 ms 31188 KB Output is correct
91 Correct 6 ms 31188 KB Output is correct
92 Correct 11 ms 31188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5112 KB Output is correct
2 Correct 7 ms 5352 KB Output is correct
3 Correct 6 ms 5352 KB Output is correct
4 Correct 7 ms 5352 KB Output is correct
5 Correct 12 ms 5352 KB Output is correct
6 Correct 6 ms 5384 KB Output is correct
7 Correct 9 ms 5384 KB Output is correct
8 Correct 6 ms 5408 KB Output is correct
9 Correct 6 ms 5408 KB Output is correct
10 Correct 6 ms 5408 KB Output is correct
11 Correct 7 ms 5416 KB Output is correct
12 Correct 6 ms 5416 KB Output is correct
13 Correct 6 ms 5416 KB Output is correct
14 Correct 6 ms 5416 KB Output is correct
15 Correct 6 ms 5416 KB Output is correct
16 Correct 5 ms 5416 KB Output is correct
17 Correct 5 ms 5416 KB Output is correct
18 Correct 18 ms 5416 KB Output is correct
19 Correct 6 ms 5416 KB Output is correct
20 Correct 6 ms 5460 KB Output is correct
21 Correct 6 ms 5460 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 11 ms 5460 KB Output is correct
24 Correct 6 ms 5460 KB Output is correct
25 Correct 5 ms 5460 KB Output is correct
26 Correct 7 ms 5460 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 5 ms 5460 KB Output is correct
29 Correct 6 ms 5460 KB Output is correct
30 Correct 6 ms 5460 KB Output is correct
31 Correct 6 ms 5460 KB Output is correct
32 Correct 7 ms 5460 KB Output is correct
33 Correct 10 ms 5460 KB Output is correct
34 Correct 7 ms 5460 KB Output is correct
35 Correct 5 ms 5460 KB Output is correct
36 Correct 6 ms 5460 KB Output is correct
37 Correct 7 ms 5460 KB Output is correct
38 Correct 7 ms 5500 KB Output is correct
39 Correct 7 ms 5500 KB Output is correct
40 Correct 7 ms 5500 KB Output is correct
41 Correct 6 ms 5500 KB Output is correct
42 Correct 6 ms 5500 KB Output is correct
43 Correct 6 ms 5500 KB Output is correct
44 Correct 7 ms 5500 KB Output is correct
45 Correct 7 ms 5500 KB Output is correct
46 Correct 7 ms 5500 KB Output is correct
47 Correct 6 ms 5500 KB Output is correct
48 Correct 7 ms 5500 KB Output is correct
49 Correct 7 ms 5500 KB Output is correct
50 Correct 7 ms 5500 KB Output is correct
51 Correct 7 ms 5500 KB Output is correct
52 Correct 6 ms 5500 KB Output is correct
53 Correct 6 ms 5500 KB Output is correct
54 Correct 5 ms 5500 KB Output is correct
55 Correct 7 ms 5500 KB Output is correct
56 Correct 6 ms 5500 KB Output is correct
57 Correct 6 ms 5500 KB Output is correct
58 Correct 6 ms 5500 KB Output is correct
59 Correct 6 ms 5500 KB Output is correct
60 Correct 16 ms 5500 KB Output is correct
61 Correct 6 ms 5500 KB Output is correct
62 Correct 7 ms 5500 KB Output is correct
63 Correct 7 ms 5500 KB Output is correct
64 Correct 6 ms 5500 KB Output is correct
65 Correct 7 ms 5500 KB Output is correct
66 Correct 247 ms 26996 KB Output is correct
67 Correct 253 ms 27156 KB Output is correct
68 Incorrect 264 ms 27156 KB Output isn't correct
69 Halted 0 ms 0 KB -