Submission #1009733

#TimeUsernameProblemLanguageResultExecution timeMemory
1009733CookieDuathlon (APIO18_duathlon)C++14
100 / 100
61 ms46540 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
const int cox[4] = {1, 0, -1, 0};
const int coy[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 4e5 + 5, mxd = 250 * 250, sq = 500, mxv = 2e6 + 1;
const int max_iter = 8e4, global_iter = 15e5 + 5;
//const int base = (1 <<18);
const ll inf = 1e16 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n, m;
ll all = 0, ans = 0, sub[mxn + 1], siz[mxn + 1];
int low[mxn + 1], num[mxn + 1], tt = 0, idbcc = 0;
vt<int>adj[mxn + 1], g[mxn + 1];
vt<int>st;
void addedge(int u, int v){
    g[u].pb(v); g[v].pb(u);
}
void dfs(int s, int pre){
    all++;
    low[s] = num[s] = ++tt;
    st.pb(s);
    for(auto i: adj[s]){
        if(i != pre){
            if(!num[i]){
                dfs(i, s); low[s] = min(low[s], low[i]);
                if(low[i] >= num[s]){
                    idbcc++; siz[idbcc] = 1; 
                    addedge(idbcc, s);
                    int last = s;
                    while(last != i){
                        last = st.back(); st.pop_back(); siz[idbcc]++; addedge(idbcc, last);
                    }
                }
            }else{
                low[s] = min(low[s], num[i]);
            }
        }
    }
}
void dfs2(int s, int pre){
    if(s <= n)sub[s] = 1;
    for(auto i: g[s]){
        if(i != pre){
            dfs2(i, s); sub[s] += sub[i];
            if(s > n){
                // c in s 
                ans -= 1LL * (siz[s] - 1) * (sub[i]) * (sub[i] - 1);
            }
        }
    }
    if(s > n){
        ans -= 1LL * (siz[s] - 1) * (all - sub[s]) * (all - sub[s] - 1);
    }
}
void solve(){
    cin >> n >> m;
    idbcc = n;
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    for(int i = 1; i <= n; i++){
        if(!num[i]){
            all = 0;
            dfs(i, -1);
            dfs2(i, -1);
            ans += 1LL * all * (all - 1) * (all - 2);
        }
    }
    cout << ans;
}
 
 
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("KHONG.inp", "r", stdin);
    //freopen("KHONG.out", "w", stdout);
    int tt; tt = 1;
    while(tt--)solve();
    return(0);
}
#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...