Submission #986024

#TimeUsernameProblemLanguageResultExecution timeMemory
986024khanhtbDuathlon (APIO18_duathlon)C++14
100 / 100
72 ms31824 KiB
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 1e18;
const ll base = 31;
const ll blocksz = 320;
const ll N = 2e5+8;
const ll buff = 1e6+8;
vi adj[N],g[N];
ll low[N],num[N],tdfs,cntcc,sz[N],n,m,siz;
stack<ll> stk;
void tarjan(ll u, ll p){
    siz++;
    stk.push(u);
    low[u] = num[u] = ++tdfs;
    for(ll v:adj[u]){
        if(v == p) continue;
        if(!num[v]){
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] >= num[u]){
                cntcc++;
                g[u].pb(cntcc);
                while(g[cntcc].empty() || g[cntcc].back() != v) g[cntcc].pb(stk.top()),stk.pop();
            }
        }
        else low[u] = min(low[u],num[v]);
    }
}
ll ans;
void dfs(ll u){
    sz[u] = (u <= n);
    for(ll v:g[u]){
        dfs(v);
        sz[u] += sz[v];
        if(v <= n) ans -= (ll)g[u].size()*sz[v]*(sz[v]-1);
    }
    if(u > n) ans -= (ll)g[u].size()*(siz-sz[u])*(siz-sz[u]-1);
}
void solve(){
    cin >> n >> m;
    cntcc = n;
    for(ll i = 1; i <= m; i++){
        ll u,v;cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(ll i = 1; i <= n; i++){
        if(!num[i]){
            siz = 0;
            tarjan(i,i);
            ans += siz*(siz-1)*(siz-2);
            dfs(i);
        }
    }
    cout << ans;
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen("test.inp","r")){
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    ll T = 1;
    // cin >> T;
    while(T--){
        solve();
        cout << '\n';
    }
}

Compilation message (stderr)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("test.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen("test.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...