답안 #260654

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
260654 2020-08-10T16:24:09 Z eohomegrownapps 철인 이종 경기 (APIO18_duathlon) C++14
8 / 100
169 ms 21672 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<vector<ll>> adjlist;
vector<vector<ll>> bridgetree;
vector<ll> comsize;
vector<ll> majorcomponents;
vector<ll> minortomajor;
vector<ll> majsize;
ll majind = 0;
ll nc;
ll n;
ll INF = 1e9;

namespace tarjan{
    vector<ll> depth;
    vector<ll> hval;
    stack<ll> nodesvisited;

    vector<ll> components;
    ll curcom = 0;

    void processComponent(ll node){
        ll cnt = 1;
        while (nodesvisited.top()!=node){
            ll t = nodesvisited.top();
            components[t]=curcom;
            minortomajor[curcom]=majind;
            nodesvisited.pop();
            cnt++;
        }
        components[node]=curcom;
        nodesvisited.pop();
        comsize.push_back(cnt);
        curcom++;
    }

    void getHighest(ll node, ll parent){
        majorcomponents[node]=majind;
        ll highest = depth[node];
        hval[node]=highest;
        nodesvisited.push(node);
        for (ll i : adjlist[node]){
            if (i==parent){continue;}
            if (depth[i]==INF){
                depth[i]=depth[node]+1;
                getHighest(i,node);
            }
            ll hcur = hval[i];
            if (hcur<=depth[node]){
                //not a bridge
                highest=min(highest,hcur);
            } else {
                //bridge
                processComponent(i);
            }
        }
        hval[node]=highest;
    }

    void runBridgeDecom(){
        depth.resize(n,INF);
        hval.resize(n,n+1);
        components.resize(n);
        majorcomponents.resize(n);
        minortomajor.resize(n);
        for (int i = 0; i<n; i++){
            if (depth[i]!=INF){continue;}
            depth[i]=0;
            getHighest(i,-1);
            processComponent(i);
            if (nodesvisited.size()>0){
                comsize.push_back(nodesvisited.size());
                while (nodesvisited.size()>0){
                    ll t = nodesvisited.top();
                    components[t]=curcom;
                    nodesvisited.pop();
                }
                curcom++;
            }
            majind++;
        }
        majsize.resize(majind);
        for (int i = 0; i<n; i++){
            majsize[majorcomponents[i]]++;
        }

        bridgetree.resize(curcom);
        nc = curcom;

        for (ll i = 0; i<n; i++){
            for (ll j : adjlist[i]){
                ll ix = components[i];
                ll jx = components[j];
                if (ix!=jx){
                    bridgetree[ix].push_back(jx);
                    bridgetree[jx].push_back(ix);
                }
            }
        }
        for (ll i = 0; i<nc; i++){
            sort(bridgetree[i].begin(),bridgetree[i].end());
            bridgetree[i].erase(unique(bridgetree[i].begin(),bridgetree[i].end()),bridgetree[i].end());
        }
    }
}

vector<ll> subtreesize;
vector<ll> tdepth;
ll dfs(ll node, ll parent){
    //cout<<node<<' '<<parent<<'\n';
    ll tot = 0;
    subtreesize[node]=comsize[node];
    ll ccnt = 0;
    for (ll i : bridgetree[node]){
        if (i==parent){continue;}
        tdepth[i]=tdepth[node]+1;
        tot+=dfs(i,node);
        subtreesize[node]+=subtreesize[i];
        ccnt++;
    }
    ll childrentot = subtreesize[node]-comsize[node];
    if (ccnt>=2){
        for (ll i : bridgetree[node]){
            if (i==parent){continue;}
            tot+=comsize[node]*subtreesize[i]*(childrentot-subtreesize[i]);
        }
    }
    //cout<<"semit "<<tot<<'\n';
    //cout<<node<<" "<<tdepth[node]<<'\n';
    
    //consider starting at node and going up
    tot+=2*tdepth[node]*childrentot;
    //cout<<node<<": total "<<tot<<'\n';
    return tot;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    ll m;
    cin>>n>>m;
    adjlist.resize(n);
    for (ll i = 0; i<m; i++){
        ll a,b;
        cin>>a>>b;
        a--;b--;
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
    }
    
    tarjan::runBridgeDecom();
    /*for (ll i = 0; i<n; i++){
        cout<<"("<<i<<": "<<tarjan::components[i]<<") ";
    }cout<<'\n';
    for (ll i = 0; i<bridgetree.size(); i++){
        cout<<i<<" ("<<comsize[i]<<"): ";
        for (ll j : bridgetree[i]){
            cout<<j<<", ";
        }cout<<'\n';
    }*/

    ll ans = 0;

    // within one component

    for (ll i = 0; i<nc; i++){
        if (comsize[i]>=3){
            ans+=comsize[i]*(comsize[i]-1)*(comsize[i]-2);
        }
    }
    /*cout<<ans<<'\n';
    for (int i : comsize){
        cout<<i<<'\n';
    }*/
    //s+m in one component, e in other (or vice versa)
    for (ll i = 0; i<nc; i++){
        if (comsize[i]>=2){
            //assert(majsize[majorcomponents[i]]-comsize[i]>0);
            ans+=2*((comsize[i]-1)*(comsize[i]-1))*(majsize[minortomajor[i]]-comsize[i]);
        }
    }
    //cout<<ans<<'\n';
    //all three in different components
    subtreesize.resize(nc);
    tdepth.resize(nc,INF);
    for (int i = 0; i<nc; i++){
        if (tdepth[i]!=INF){continue;}
        tdepth[i]=0;
        ans+=dfs(i,-1);
    }
    cout<<ans<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Incorrect 1 ms 384 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Incorrect 1 ms 384 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 19640 KB Output is correct
2 Correct 81 ms 19576 KB Output is correct
3 Correct 106 ms 20692 KB Output is correct
4 Correct 95 ms 18912 KB Output is correct
5 Correct 100 ms 16928 KB Output is correct
6 Correct 112 ms 21196 KB Output is correct
7 Correct 119 ms 18288 KB Output is correct
8 Correct 109 ms 20336 KB Output is correct
9 Correct 123 ms 17908 KB Output is correct
10 Correct 101 ms 17576 KB Output is correct
11 Correct 87 ms 16240 KB Output is correct
12 Correct 86 ms 16112 KB Output is correct
13 Correct 88 ms 16400 KB Output is correct
14 Correct 91 ms 16108 KB Output is correct
15 Correct 79 ms 16236 KB Output is correct
16 Correct 73 ms 15936 KB Output is correct
17 Correct 12 ms 12280 KB Output is correct
18 Correct 14 ms 12280 KB Output is correct
19 Correct 12 ms 12280 KB Output is correct
20 Correct 12 ms 12280 KB Output is correct
21 Correct 12 ms 12280 KB Output is correct
22 Correct 12 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 169 ms 21612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 162 ms 21672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Incorrect 1 ms 384 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Incorrect 1 ms 384 KB Output isn't correct
8 Halted 0 ms 0 KB -