Submission #283426

#TimeUsernameProblemLanguageResultExecution timeMemory
283426evpipisDuathlon (APIO18_duathlon)C++11
100 / 100
298 ms40004 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef long long ll;

const int len = 2e5+5;

int num[len], low[len], sz[len], sub[len], vis[len], par[len];
int cnt_dfs, cnt_com, n, m;
ii edge[len];
vector<int> com[len], nex[len], stck;
vector<ii> adj[len];

// the graph is simple
void dfs(int u, int root = 0){
    num[u] = low[u] = ++cnt_dfs;

    for (auto v: adj[u]){
        if (!vis[v.se] && !num[v.fi]){
            vis[v.se] = 1, stck.pb(v.se);
            dfs(v.fi);
            low[u] = min(low[u], low[v.fi]);

            if (low[v.fi] >= num[u]){
                cnt_com++;
                //printf("component %d\n", cnt_com);
                while (true){
                    int e = stck.back();
                    stck.pop_back();

                    //printf("%d - %d\n", edge[e].fi, edge[e].se);
                    com[cnt_com].pb(e);

                    if (e == v.se)
                        break;
                }
            }
        }
        else if (!vis[v.se] && num[v.fi]){
            vis[v.se] = 1, stck.pb(v.se);
            low[u] = min(low[u], num[v.fi]);
        }
    }

    //printf("finished dfs: u = %d, art = %d, num = %d, low = %d\n", u, art[u], num[u], low[u]);
}

void compose(){
    for (int i = 1; i <= cnt_com; i++){
        set<int> mys;
        for (auto e: com[i]){
            int a = edge[e].fi, b = edge[e].se;
            mys.insert(a), mys.insert(b);
        }

        for (auto u: mys){
            sz[i+n]++;
            nex[u].pb(i+n);
            nex[i+n].pb(u);
        }
        //printf("sz = %d\n", sz[i+n]);
    }
}

void fix(int u){
    if (u <= n)
        sub[u] = 1;

    for (auto v: nex[u]){
        if (v == par[u]) continue;

        par[v] = u;
        fix(v);
        sub[u] += sub[v];
    }

    //printf("ins fix: u = %d, p = %d, sub = %d\n", u, par[u], sub[u]);
}

ll solve(int u, int all){
    //printf("u = %d, all = %d\n", u, all);
    ll ans = 0;
    if (u > n){
        for (auto v: nex[u]){
            if (v != par[u])
                ans += (sz[u]-1)*1LL*(sub[v])*1LL*(sub[v]-1);
            else
                ans += (sz[u]-1)*1LL*(all-sub[u])*1LL*(all-sub[u]-1);
        }
    }

    for (auto v: nex[u])
        if (v != par[u])
            ans += solve(v, all);

    return ans;
}

int main(){
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].pb(mp(b, i));
        adj[b].pb(mp(a, i));
        edge[i] = mp(a, b);
    }

    for (int i = 1; i <= n; i++)
        if (!num[i])
            dfs(i, 1);
    //printf("after dfs\n");

    compose();
    //printf("after decompose\n");

    //printf("cnt_com = %d\n", cnt_com);

    ll ans = 0;
    for (int i = 1; i <= n; i++)
        if (!par[i])
            fix(i), ans += (sub[i])*1LL*(sub[i]-1)*1LL*(sub[i]-2) - solve(i, sub[i]);
    //printf("after solve\n");

    printf("%lld\n", ans);
    return 0;
}
/*
4 3
1 2
2 3
3 4

4 4
1 2
2 3
3 4
4 2

13 18
1 2
1 3
2 3
3 4
3 10
4 5
4 6
4 7
5 6
7 8
7 9
7 10
8 9
10 11
10 12
10 13
11 12
12 13
*/

Compilation message (stderr)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#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...