Submission #197531

#TimeUsernameProblemLanguageResultExecution timeMemory
197531quocnguyen1012철인 이종 경기 (APIO18_duathlon)C++14
100 / 100
216 ms28404 KiB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int base = 131;
const int maxn = 1e5 + 5;

vector<int> adj[maxn], g[maxn * 2];
int N, M;
int nsz = 0;
int sz[2 * maxn], comp = maxn;
int num[maxn], low[maxn];
ll res = 0;
vector<int> ST;

void add(int u, int v)
{
  g[u].pb(v); g[v].pb(u);
  ///cout << u << ' ' << v << '\n';
}

void dfs(int u, int p = -1)
{
  ST.pb(u);
  static int nTime = 0;
  num[u] = low[u] = ++nTime;
  ++nsz;
  for (int v : adj[u]){
    if (v == p) continue;
    if (num[v]){
      low[u] = min(low[u], num[v]);
    }
    else{
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] >= num[u]){
        ++comp;
        add(u, comp);
        while(1){
          int now = ST.back(); ST.pop_back();
          add(now, comp);
          if (now == v) break;
        }
      }
    }
  }
}

void solve(int u, int p = -1)
{
  if (u < maxn) sz[u] = 1;
  for (int v : g[u]){
    if (v == p) continue;
    solve(v, u);
    sz[u] += sz[v];
  }
  if (u < maxn) return;
  for (int v : g[u]){
    int need;
    if (v == p) need = nsz - sz[u];
    else need = sz[v];
    res -= 1ll * need * (need - 1) * (int(g[u].size()) - 1);
  }
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> M;
  while (M--){
    int u, v; cin >> u >> v;
    adj[u].pb(v); adj[v].pb(u);
  }
  for (int i = 1; i <= N; ++i){
    if (!num[i]){
      nsz = 0;
      dfs(i);
      res += 1ll * nsz * (nsz - 1) * (nsz - 2);
      solve(i);
    }
  }
  cout << res << '\n';
}

Compilation message (stderr)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...