Submission #701819

#TimeUsernameProblemLanguageResultExecution timeMemory
701819fatemetmhrDuathlon (APIO18_duathlon)C++17
100 / 100
165 ms83528 KiB
// ~ Be Name Khoda ~

#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  5e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;

int n, h[maxn5], mn[maxn5], newid, num = 0;
ll ans = 0, have[maxn5], allgood[maxn5], sz[maxn5];
bool mark[maxn5];
vector <int> adj[maxn5], chil[maxn5], bladj[maxn5];
vector <ll> good[maxn5]; 

void dfs1(int v, int par = -1){
    mark[v] = true;
    num++;
    mn[v] = h[v];
    for(auto u : adj[v]){
        if(!mark[u]){
            h[u] = h[v] + 1;
            dfs1(u, v);
            mn[v] = min(mn[v], mn[u]);
            chil[v].pb(u);
        }
        else if(u != par)
            mn[v] = min(mn[v], h[u]);
    }
}

void dfs2(int v, int cmpup = -1){
    allgood[v] = num;
    if(cmpup != -1){
        bladj[v].pb(cmpup);
        bladj[cmpup].pb(v);
        allgood[cmpup] = allgood[v];
    }
    for(auto u : chil[v]){
        if(mn[u] >= h[v]){
            bladj[v].pb(newid);
            bladj[newid].pb(v);
            dfs2(u, newid++);
        }
        else
            dfs2(u, cmpup);
    }
}

void dfs3(int v){
    mark[v] = true;
    sz[v] = 0;
    int idpar = -1;
    good[v].resize(bladj[v].size());
    for(int i = 0; i < bladj[v].size(); i++){
        int u = bladj[v][i];
        if(!mark[u]){
            dfs3(u);
            have[v] += sz[v] * sz[u];
            sz[v] += sz[u];
            good[v][i] = sz[u];
        }
        else
            idpar = i;
    }
    have[v] += sz[v] * (allgood[v] - sz[v] - (v < n));
    sz[v] += (v < n);
    if(idpar != -1)
        good[v][idpar] = allgood[v] - sz[v];
    //cout << "oh oh " << v << ' ' << allgood[v] << ' ' << sz[v] << endl;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);

    int m; cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    newid = n;

    for(int i = 0; i < n; i++) if(!mark[i]){
        num = 0;
        dfs1(i);
        dfs2(i);
    }
    memset(mark, false, sizeof mark);
    for(int i = 0; i < newid; i++) if(!mark[i])
        dfs3(i);
    for(int i = 0; i < n; i++){
        ans += have[i];
        //cout << "aha " << i << ' ' << have[i] << endl;
        for(int j = 0; j < bladj[i].size(); j++){
            int u = bladj[i][j];
            //cout << "ok " << j << ' ' << u << ' ' << good[i][j] << ' ' << have[u] << endl;
            ans += have[u] - good[i][j] * (allgood[i] - good[i][j]);
            //cout << "hey! " << ans << endl;
        }
    }
    cout << ans * 2 << endl;

}

Compilation message (stderr)

count_triplets.cpp: In function 'void dfs3(int)':
count_triplets.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < bladj[v].size(); i++){
      |                    ~~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:113:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         for(int j = 0; j < bladj[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~~~
#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...