답안 #866169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
866169 2023-10-25T14:09:27 Z alexander707070 철인 이종 경기 (APIO18_duathlon) C++14
10 / 100
1000 ms 30536 KB
#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;

long long n,m,a[MAXN],b[MAXN];
long long sz[MAXN],szcomp[MAXN],total[MAXN],rem[MAXN];
int comp[MAXN],k,root[MAXN];
vector< pair<int,int> > v[MAXN];
vector<int> tree[MAXN];

long long ans;

int dep[MAXN],low[MAXN];
bool li[MAXN],bridge[MAXN];

void bcc(int x,int p,int d,int e){

    li[x]=true; dep[x]=d; low[x]=dep[x];

    for(int i=0;i<v[x].size();i++){
        if(!li[v[x][i].first]){
            bcc(v[x][i].first,x,d+1,v[x][i].second);
            low[x]=min(low[x],low[v[x][i].first]);
        }else if(v[x][i].first!=p){
            low[x]=min(low[x],dep[v[x][i].first]);
        }
    }

    if(p!=0 and low[x]==dep[x]){
        bridge[e]=true;
    }
}

void dfs(int x,int c){
    li[x]=true; comp[x]=c;
    szcomp[c]++;

    for(int i=0;i<v[x].size();i++){
        if(li[v[x][i].first] or bridge[v[x][i].second])continue;
        dfs(v[x][i].first,c);
    }
}

void dfs2(int x,int p){
    sz[x]=szcomp[x];

    for(int i=0;i<tree[x].size();i++){
        if(tree[x][i]==p)continue;

        dfs2(tree[x][i],x);
        sz[x]+=sz[tree[x][i]];
    }
}

void dfs3(int x,int p,int t,int r){
    total[x]=t; root[x]=r;

    for(int i=0;i<tree[x].size();i++){
        if(tree[x][i]==p)continue;
        dfs3(tree[x][i],x,t,r);
    }
}

void calc(int x,int y){
    ans+=(total[comp[x]]-szcomp[comp[x]])*2;

    for(int f=0;f<v[x].size();f++){
        if(comp[v[x][f].first]==comp[x])continue;

        if(sz[comp[v[x][f].first]]<sz[comp[x]]){
            ans-=sz[comp[v[x][f].first]]*2;
        }else{
            ans-=(total[comp[x]]-sz[comp[x]])*2;
        }
    }
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>a[i]>>b[i];
        v[a[i]].push_back({b[i],i});
        v[b[i]].push_back({a[i],i});
    }

    for(int i=1;i<=n;i++){
        if(!li[i])bcc(i,0,0,0);
    }

    for(int i=1;i<=n;i++)li[i]=false;

    for(int i=1;i<=n;i++){
        if(!li[i]){
            k++; dfs(i,k);
        }
    }

    for(int i=1;i<=m;i++){
        if(comp[a[i]]==comp[b[i]])continue;

        tree[comp[a[i]]].push_back(comp[b[i]]);
        tree[comp[b[i]]].push_back(comp[a[i]]);
    }

    for(int i=1;i<=k;i++){
        if(sz[i]==0){
            dfs2(i,0);
            dfs3(i,0,sz[i],i);
        }
    }

    for(int i=1;i<=m;i++){
        if(comp[a[i]]==comp[b[i]])continue;
        
        if(sz[comp[a[i]]]<sz[comp[b[i]]])swap(a[i],b[i]);

        rem[a[i]]+=sz[comp[b[i]]];
        rem[b[i]]+=total[comp[b[i]]]-sz[comp[b[i]]];
    }

    for(int i=1;i<=k;i++){
        ans+=(total[i]-szcomp[i])*(total[i]-szcomp[i]-1)*szcomp[i];

        for(int f=0;f<tree[i].size();f++){
            if(sz[tree[i][f]]<sz[i]){
                ans-=sz[tree[i][f]]*(sz[tree[i][f]]-1)*szcomp[i];
            }else{
                ans-=(total[i]-sz[i])*(total[i]-sz[i]-1)*szcomp[i];
            }
        }
    }

    for(int i=1;i<=n;i++){
        for(int f=1;f<=n;f++){
            if(comp[i]==comp[f] and i!=f){
                calc(i,f);
            }
        }
    }
        
    for(int i=1;i<=k;i++){
        ans+=szcomp[i]*(szcomp[i]-1)*(szcomp[i]-2);
    }

    cout<<ans<<"\n";

    return 0;
}

Compilation message

count_triplets.cpp: In function 'void bcc(int, int, int, int)':
count_triplets.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs(int, int)':
count_triplets.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<tree[x].size();i++){
      |                 ~^~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void dfs3(int, int, int, int)':
count_triplets.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(int i=0;i<tree[x].size();i++){
      |                 ~^~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void calc(int, int)':
count_triplets.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int f=0;f<v[x].size();f++){
      |                 ~^~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:129:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for(int f=0;f<tree[i].size();f++){
      |                     ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Incorrect 3 ms 18780 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Incorrect 3 ms 18780 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1027 ms 30536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 4 ms 18776 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 4 ms 19076 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 4 ms 18968 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 5 ms 18780 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 5 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 4 ms 18776 KB Output is correct
16 Correct 4 ms 18776 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 4 ms 19036 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1040 ms 28636 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19032 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Incorrect 4 ms 18780 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1010 ms 29008 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Incorrect 3 ms 18780 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18780 KB Output is correct
2 Correct 3 ms 18776 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Incorrect 3 ms 18780 KB Output isn't correct
8 Halted 0 ms 0 KB -