Submission #372729

# Submission time Handle Problem Language Result Execution time Memory
372729 2021-03-01T11:36:10 Z BartolM Duathlon (APIO18_duathlon) C++17
31 / 100
133 ms 20452 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=1e5+5;

int n, m, dtime=1;
ll sol=0;
int cnt[N], siz[N], P[N], sweep[N];
int dt[N], lw[N];
vector <int> ls[N], ls2[N];
vector <pii> ed;
#define DEBUG 0

int name(int x) {
    if (x==P[x]) return x;
    P[x]=name(P[x]);
    return P[x];
}

void mrg(int a, int b) {
    a=name(a); b=name(b);
    if (a==b) return;
    P[a]=b;
    siz[b]+=siz[a];
}

void rek(int node, int par) {
    dt[node]=dtime++;
    lw[node]=dt[node];
    for (int sus:ls[node]) {
        if (sus==par) continue;
        if (dt[sus]==0) {
            rek(sus, node);
            lw[node]=min(lw[node], lw[sus]);
        }
        else { //backedge
            lw[node]=min(lw[node], dt[sus]);
            sweep[node]++; sweep[sus]--;
        }
    }
    if (par && lw[node]==dt[node]) { //bridge
        ed.pb(mp(node, par));
        #if DEBUG
            printf("bridge %d -> %d\n", node, par);
        #endif
    }
    else if (par) {
        mrg(node, par);
        #if DEBUG
            printf("mrgam %d i %d\n", node, par);
        #endif
    }
//    sweep[par]+=sweep[node];
//    if (sweep[node]) mrg(node, par);
}

void dfs(int node, int par) {
    cnt[node]=siz[node];
    for (int sus:ls2[node]) {
        if (sus==par) continue;
        dfs(sus, node);
        cnt[node]+=cnt[sus];
    }
}

void calc(int node, int par, int uk) {
    sol+=(ll)siz[node]*(siz[node]-1)*(siz[node]-2);
    int iz=uk-siz[node];
    for (int sus:ls2[node]) {
        if (sus==par) continue;
        calc(sus, node, uk);
        iz-=cnt[sus];
        sol+=(ll)cnt[sus]*(uk-cnt[sus]-siz[node]);


        if (siz[node]>2) sol+=(ll)cnt[sus]*(siz[node]-1)*(siz[node]-1)*2;
    }
    sol+=(ll)iz*(uk-iz-siz[node]);
//    printf("node: %d, iz: %d\n", node, iz);
    if (siz[node]>2) sol+=(ll)iz*(siz[node]-1)*(siz[node]-1)*2;
}

void solve() {
    for (int i=1; i<=n; ++i) P[i]=i, siz[i]=1;
    for (int i=1; i<=n; ++i) {
        if (dt[i]) continue;
        dtime=1;
        ed.clear();
        rek(i, 0);
        for (pii pp:ed) {
            ls2[name(pp.X)].pb(name(pp.Y));
            ls2[name(pp.Y)].pb(name(pp.X));
        }
    }

    for (int i=1; i<=n; ++i) {
        sort(ls2[i].begin(), ls2[i].end());
        ls2[i].erase(unique(ls2[i].begin(), ls2[i].end()), ls2[i].end());

        #if DEBUG
            printf("node: %d, siz: %d, sus: ", i, siz[i]);
            for (int sus:ls2[i]) printf("%d ", sus);
            printf("\n");
        #endif // DEBUG
    }

    for (int i=1; i<=n; ++i) {
        if (cnt[i] || P[i]!=i) continue;
        dfs(i, 0);
        calc(i, 0, cnt[i]);
    }
    printf("%lld\n", sol);
}

void load() {
    scanf("%d %d", &n, &m);
    for (int i=0; i<m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        ls[a].pb(b); ls[b].pb(a);
    }
}

int main() {
    load();
    solve();
    return 0;
}

Compilation message

count_triplets.cpp: In function 'void load()':
count_triplets.cpp:128:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  128 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  131 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Incorrect 4 ms 5100 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Incorrect 4 ms 5100 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 20332 KB Output is correct
2 Correct 86 ms 20332 KB Output is correct
3 Correct 102 ms 18156 KB Output is correct
4 Correct 96 ms 19440 KB Output is correct
5 Correct 96 ms 15852 KB Output is correct
6 Correct 117 ms 16768 KB Output is correct
7 Correct 101 ms 15340 KB Output is correct
8 Correct 111 ms 16344 KB Output is correct
9 Correct 100 ms 14444 KB Output is correct
10 Correct 99 ms 14832 KB Output is correct
11 Correct 90 ms 13164 KB Output is correct
12 Correct 87 ms 13036 KB Output is correct
13 Correct 73 ms 13036 KB Output is correct
14 Correct 75 ms 12908 KB Output is correct
15 Correct 59 ms 12524 KB Output is correct
16 Correct 58 ms 12268 KB Output is correct
17 Correct 8 ms 7020 KB Output is correct
18 Correct 7 ms 7020 KB Output is correct
19 Correct 7 ms 7020 KB Output is correct
20 Correct 7 ms 7020 KB Output is correct
21 Correct 7 ms 7020 KB Output is correct
22 Correct 7 ms 7020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5228 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5248 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 15556 KB Output is correct
2 Correct 103 ms 15464 KB Output is correct
3 Correct 103 ms 15460 KB Output is correct
4 Correct 115 ms 15608 KB Output is correct
5 Correct 119 ms 15472 KB Output is correct
6 Correct 116 ms 20452 KB Output is correct
7 Correct 114 ms 18640 KB Output is correct
8 Correct 114 ms 17764 KB Output is correct
9 Correct 115 ms 16992 KB Output is correct
10 Correct 124 ms 15084 KB Output is correct
11 Correct 109 ms 15332 KB Output is correct
12 Correct 133 ms 14956 KB Output is correct
13 Correct 109 ms 14956 KB Output is correct
14 Correct 105 ms 14444 KB Output is correct
15 Correct 93 ms 14060 KB Output is correct
16 Correct 60 ms 12268 KB Output is correct
17 Correct 76 ms 16092 KB Output is correct
18 Correct 76 ms 16092 KB Output is correct
19 Correct 83 ms 16216 KB Output is correct
20 Correct 95 ms 16224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Incorrect 4 ms 5100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 104 ms 15488 KB Output is correct
2 Correct 113 ms 15584 KB Output is correct
3 Incorrect 108 ms 14688 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Incorrect 4 ms 5100 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Incorrect 4 ms 5100 KB Output isn't correct
8 Halted 0 ms 0 KB -