Submission #108840

# Submission time Handle Problem Language Result Execution time Memory
108840 2019-05-02T09:39:12 Z oolimry Duathlon (APIO18_duathlon) C++14
66 / 100
470 ms 57016 KB
#include <bits/stdc++.h>

using namespace std;
int n, m;
typedef vector<int> vi;
typedef pair<int,int> ii;
struct node{
    int depth = -1;
    int low;
    int parent;
    bool vis = false;
    bool isArti = false;
    vi adj;
};

vector<unordered_set<int> > biconnected;
stack<int> s; ///this is only needed for biconnected components
///note: this will ignore single nodes with no edges
static node g[200005];
int rootChildren = 0;
void dfs(int u){
    if(g[u].depth == -1) g[u].depth = g[g[u].parent].depth + 1;
    g[u].low = g[u].depth;
    g[u].vis = true;
    for(int i = 0;i < g[u].adj.size();i++){
        int v = g[u].adj[i];
        if(!g[v].vis){
            g[v].parent = u;
            if(g[u].depth == 0) rootChildren++;
            s.push(u); ///s traces the path of tree traversal
            dfs(v);
            g[u].low = min(g[u].low,g[v].low);

            if(g[v].low >= g[u].depth){
                g[u].isArti = true;
                g[u].low = min(g[u].low,g[v].low);
                unordered_set<int> newSet;
                int nn;
                biconnected.push_back(newSet); ///create new biconnected component
                do{
                    assert(!s.empty());
                    nn = s.top();
                    s.pop();
                    biconnected.back().insert(nn);
                }while(nn != u);
            }
            ///if(g[v].low > g[u].depth{
                  ///u,v is a bridge
            ///}
        }
        else if(v != g[u].parent){
            g[u].low = min(g[u].low,g[v].low);
        }
        s.push(u);
    }
}
struct node2{
    long long sz;
    long long dp = 0;
    bool isArti = false;
    bool vis = false;
    int r;
    int p = -1;
    vi adj;
};

vector<node2> bct; ///block-cut tree
long long root = -1;
void dp(int u){
    if(bct[u].vis) return;
    bct[u].vis = true;
    bct[u].dp = bct[u].sz;
    bct[u].r = root;
    for(int i = 0;i < bct[u].adj.size();i++){
        int v = bct[u].adj[i];
        if(bct[v].vis) continue;
        bct[v].p = u;
        dp(v);
        bct[u].dp += bct[v].dp;
    }
}
int main()
{
    //freopen("i.txt","r",stdin);
    cin >> n >> m;
    ii edges[m];
    for(int i = 0;i < m;i++){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].adj.push_back(b);
        g[b].adj.push_back(a);
        edges[i] = ii(a,b);
    }

    for(int i = 0;i < n;i++){
        if(!g[i].vis){
            rootChildren = 0;
            g[i].depth = 0;
            dfs(i);
            if(rootChildren > 1) g[i].isArti = true;
            else g[i].isArti = false; ///this line is needed
        }
    }
    unordered_map<int,int> artis;
    for(int i = 0;i < n;i++){
        if(g[i].isArti){
            artis[i] = bct.size();
            node2 nn;
            nn.sz = 1;
            nn.isArti = true;
            bct.push_back(nn);
        }
    }


    for(int i = 0;i < biconnected.size();i++){
        node2 nn;
        nn.sz = biconnected[i].size();
        nn.isArti = false;
        for(unordered_set<int>::iterator it = biconnected[i].begin();it != biconnected[i].end();it++){
            //cout << *it << " ";
            if(artis.find(*it) != artis.end()){
                nn.adj.push_back(artis[*it]);
                bct[artis[*it]].adj.push_back(bct.size());
                nn.sz--;
            }
        }
        bct.push_back(nn);
        //cout << "\n";
    }


    for(int i = 0;i < bct.size();i++){
        root = i;
        dp(i);
    }

    long long ans = 0ll;
    for(int i = 0;i < bct.size();i++){
        long long total = bct[bct[i].r].dp - bct[i].sz;
        long long sz = bct[i].sz;
        if(!bct[i].isArti){
            ans += sz * (sz - 1) * (sz + bct[i].adj.size() - 2);
            ans += 2ll * sz * (sz + bct[i].adj.size() - 2) * total;
        }
        long long others = bct[bct[i].r].dp - bct[i].dp;
        for(int j = 0;j < bct[i].adj.size();j++){
            int v = bct[i].adj[j];
            if(v != bct[i].p){
                long long extra = 0ll;
                if(!bct[i].isArti) extra = bct[i].adj.size()-2;
                ans += 2ll * (bct[i].sz + extra) * bct[v].dp * others;
                others += bct[v].dp;
            }
        }


        //if(!bct[i].isArti) ans += (bct[i].sz) * (bct[i].sz - 1) * (bct[i].adj.size());
        //cout << i << " " << ans << "\n";
    }
    cout << ans;
}

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

Compilation message

count_triplets.cpp: In function 'void dfs(int)':
count_triplets.cpp:25:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < g[u].adj.size();i++){
                   ~~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dp(int)':
count_triplets.cpp:74:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < bct[u].adj.size();i++){
                   ~~^~~~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:118:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < biconnected.size();i++){
                   ~~^~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:135:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < bct.size();i++){
                   ~~^~~~~~~~~~~~
count_triplets.cpp:141:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < bct.size();i++){
                   ~~^~~~~~~~~~~~
count_triplets.cpp:149:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0;j < bct[i].adj.size();j++){
                       ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 8 ms 8192 KB Output is correct
4 Correct 8 ms 8192 KB Output is correct
5 Correct 8 ms 8192 KB Output is correct
6 Correct 12 ms 8192 KB Output is correct
7 Correct 8 ms 8192 KB Output is correct
8 Correct 11 ms 8104 KB Output is correct
9 Correct 12 ms 8192 KB Output is correct
10 Correct 8 ms 8192 KB Output is correct
11 Correct 11 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 9 ms 8192 KB Output is correct
14 Correct 9 ms 8212 KB Output is correct
15 Correct 10 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 8 ms 8192 KB Output is correct
18 Correct 10 ms 8192 KB Output is correct
19 Correct 8 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 11 ms 8192 KB Output is correct
22 Incorrect 10 ms 8148 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 8 ms 8192 KB Output is correct
4 Correct 8 ms 8192 KB Output is correct
5 Correct 8 ms 8192 KB Output is correct
6 Correct 12 ms 8192 KB Output is correct
7 Correct 8 ms 8192 KB Output is correct
8 Correct 11 ms 8104 KB Output is correct
9 Correct 12 ms 8192 KB Output is correct
10 Correct 8 ms 8192 KB Output is correct
11 Correct 11 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 9 ms 8192 KB Output is correct
14 Correct 9 ms 8212 KB Output is correct
15 Correct 10 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 8 ms 8192 KB Output is correct
18 Correct 10 ms 8192 KB Output is correct
19 Correct 8 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 11 ms 8192 KB Output is correct
22 Incorrect 10 ms 8148 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 244 ms 32324 KB Output is correct
2 Correct 223 ms 33448 KB Output is correct
3 Correct 401 ms 41400 KB Output is correct
4 Correct 297 ms 37580 KB Output is correct
5 Correct 336 ms 37208 KB Output is correct
6 Correct 356 ms 40916 KB Output is correct
7 Correct 347 ms 39996 KB Output is correct
8 Correct 312 ms 41464 KB Output is correct
9 Correct 395 ms 38588 KB Output is correct
10 Correct 362 ms 36928 KB Output is correct
11 Correct 239 ms 32488 KB Output is correct
12 Correct 243 ms 32064 KB Output is correct
13 Correct 207 ms 31556 KB Output is correct
14 Correct 225 ms 31368 KB Output is correct
15 Correct 148 ms 25020 KB Output is correct
16 Correct 208 ms 24636 KB Output is correct
17 Correct 12 ms 8192 KB Output is correct
18 Correct 12 ms 8192 KB Output is correct
19 Correct 11 ms 8192 KB Output is correct
20 Correct 12 ms 8192 KB Output is correct
21 Correct 12 ms 8192 KB Output is correct
22 Correct 12 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8576 KB Output is correct
2 Correct 12 ms 8548 KB Output is correct
3 Correct 12 ms 8576 KB Output is correct
4 Correct 12 ms 8720 KB Output is correct
5 Correct 13 ms 8744 KB Output is correct
6 Correct 8 ms 8576 KB Output is correct
7 Correct 10 ms 8576 KB Output is correct
8 Correct 11 ms 8576 KB Output is correct
9 Correct 11 ms 8576 KB Output is correct
10 Correct 9 ms 8576 KB Output is correct
11 Correct 10 ms 8576 KB Output is correct
12 Correct 10 ms 8572 KB Output is correct
13 Correct 11 ms 8572 KB Output is correct
14 Correct 9 ms 8448 KB Output is correct
15 Correct 14 ms 8448 KB Output is correct
16 Correct 10 ms 8320 KB Output is correct
17 Correct 15 ms 8448 KB Output is correct
18 Correct 12 ms 8448 KB Output is correct
19 Correct 11 ms 8448 KB Output is correct
20 Correct 10 ms 8420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 367 ms 48624 KB Output is correct
2 Correct 427 ms 48640 KB Output is correct
3 Correct 337 ms 48636 KB Output is correct
4 Correct 470 ms 48696 KB Output is correct
5 Correct 310 ms 48696 KB Output is correct
6 Correct 370 ms 57016 KB Output is correct
7 Correct 424 ms 54272 KB Output is correct
8 Correct 411 ms 52772 KB Output is correct
9 Correct 406 ms 51396 KB Output is correct
10 Correct 374 ms 48568 KB Output is correct
11 Correct 384 ms 48636 KB Output is correct
12 Correct 355 ms 48564 KB Output is correct
13 Correct 361 ms 48680 KB Output is correct
14 Correct 324 ms 47032 KB Output is correct
15 Correct 283 ms 37784 KB Output is correct
16 Correct 166 ms 29240 KB Output is correct
17 Correct 187 ms 40408 KB Output is correct
18 Correct 187 ms 40244 KB Output is correct
19 Correct 186 ms 40244 KB Output is correct
20 Correct 228 ms 40124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8576 KB Output is correct
2 Correct 12 ms 8576 KB Output is correct
3 Correct 12 ms 8448 KB Output is correct
4 Correct 11 ms 8448 KB Output is correct
5 Correct 14 ms 8448 KB Output is correct
6 Correct 11 ms 8320 KB Output is correct
7 Correct 10 ms 8320 KB Output is correct
8 Correct 9 ms 8320 KB Output is correct
9 Correct 9 ms 8320 KB Output is correct
10 Correct 11 ms 8320 KB Output is correct
11 Correct 11 ms 8320 KB Output is correct
12 Correct 11 ms 8576 KB Output is correct
13 Correct 10 ms 8576 KB Output is correct
14 Correct 11 ms 8576 KB Output is correct
15 Correct 11 ms 8576 KB Output is correct
16 Correct 10 ms 8448 KB Output is correct
17 Correct 10 ms 8448 KB Output is correct
18 Correct 11 ms 8448 KB Output is correct
19 Correct 12 ms 8448 KB Output is correct
20 Correct 11 ms 8320 KB Output is correct
21 Correct 11 ms 8448 KB Output is correct
22 Correct 11 ms 8576 KB Output is correct
23 Correct 13 ms 8448 KB Output is correct
24 Correct 11 ms 8448 KB Output is correct
25 Correct 12 ms 8192 KB Output is correct
26 Correct 10 ms 8192 KB Output is correct
27 Correct 10 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 337 ms 48680 KB Output is correct
2 Correct 313 ms 48576 KB Output is correct
3 Correct 401 ms 47312 KB Output is correct
4 Correct 373 ms 33996 KB Output is correct
5 Correct 255 ms 27516 KB Output is correct
6 Correct 265 ms 24536 KB Output is correct
7 Correct 260 ms 24008 KB Output is correct
8 Correct 205 ms 20560 KB Output is correct
9 Correct 205 ms 20300 KB Output is correct
10 Correct 227 ms 19152 KB Output is correct
11 Correct 240 ms 17876 KB Output is correct
12 Correct 183 ms 17144 KB Output is correct
13 Correct 191 ms 17276 KB Output is correct
14 Correct 225 ms 20780 KB Output is correct
15 Correct 419 ms 53700 KB Output is correct
16 Correct 446 ms 51624 KB Output is correct
17 Correct 416 ms 44524 KB Output is correct
18 Correct 341 ms 42372 KB Output is correct
19 Correct 282 ms 34140 KB Output is correct
20 Correct 293 ms 33832 KB Output is correct
21 Correct 342 ms 34120 KB Output is correct
22 Correct 374 ms 32320 KB Output is correct
23 Correct 232 ms 26948 KB Output is correct
24 Correct 296 ms 39612 KB Output is correct
25 Correct 374 ms 39604 KB Output is correct
26 Correct 299 ms 37300 KB Output is correct
27 Correct 326 ms 37228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 8 ms 8192 KB Output is correct
4 Correct 8 ms 8192 KB Output is correct
5 Correct 8 ms 8192 KB Output is correct
6 Correct 12 ms 8192 KB Output is correct
7 Correct 8 ms 8192 KB Output is correct
8 Correct 11 ms 8104 KB Output is correct
9 Correct 12 ms 8192 KB Output is correct
10 Correct 8 ms 8192 KB Output is correct
11 Correct 11 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 9 ms 8192 KB Output is correct
14 Correct 9 ms 8212 KB Output is correct
15 Correct 10 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 8 ms 8192 KB Output is correct
18 Correct 10 ms 8192 KB Output is correct
19 Correct 8 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 11 ms 8192 KB Output is correct
22 Incorrect 10 ms 8148 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 8 ms 8192 KB Output is correct
4 Correct 8 ms 8192 KB Output is correct
5 Correct 8 ms 8192 KB Output is correct
6 Correct 12 ms 8192 KB Output is correct
7 Correct 8 ms 8192 KB Output is correct
8 Correct 11 ms 8104 KB Output is correct
9 Correct 12 ms 8192 KB Output is correct
10 Correct 8 ms 8192 KB Output is correct
11 Correct 11 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 9 ms 8192 KB Output is correct
14 Correct 9 ms 8212 KB Output is correct
15 Correct 10 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 8 ms 8192 KB Output is correct
18 Correct 10 ms 8192 KB Output is correct
19 Correct 8 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 11 ms 8192 KB Output is correct
22 Incorrect 10 ms 8148 KB Output isn't correct
23 Halted 0 ms 0 KB -