제출 #565854

#제출 시각아이디문제언어결과실행 시간메모리
565854Jarif_Rahman철인 이종 경기 (APIO18_duathlon)C++17
0 / 100
1082 ms24664 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, m;
vector<vector<int>> v, tree, backedge;
vector<bool> bl;
vector<bool> marked;
vector<int> sz;

ll ans = 0;

void dfs_tree(int nd){
    bl[nd] = 1;
    for(int x: v[nd]){
        if(!bl[x]){
            bl[x] = 1;
            tree[nd].pb(x);
            dfs_tree(x);
        }
        else backedge[nd].pb(x);
    }
}

void mark(int nd){
    marked[nd] = !marked[nd];
    for(int x: tree[nd]) mark(x);
}

void dfs_sz(int nd){
    sz[nd] = 1;
    for(int x: tree[nd]) dfs_sz(x);
    for(int x: tree[nd]) sz[nd]+=sz[x];
}

int SIZE = 0;

int dfs(int nd){
    int s = 0;
    for(int x: tree[nd]) s+=dfs(x);
    ans+=s;
    bool bkg = 0;
    for(int x: backedge[nd]) if(!marked[x]) bkg = 1;
    if(bkg) ans+=SIZE-sz[nd], s++;
    return s;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    v.assign(n, {});

    for(int i = 0; i < n; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);
    }

    marked.assign(n, 0);
    sz.resize(n);

    for(int i = 0; i < n; i++){
        tree.assign(n, {});
        backedge.assign(n, {});
        bl.assign(n, 0);
        dfs_tree(i);
        dfs_sz(i);

        for(int x: tree[i]){
            ans+=ll(sz[i]-1-sz[x])*ll(sz[x]);
            SIZE=sz[x];
            mark(x);
            dfs(x);
            mark(x);
        }
    }

    cout << ans << "\n";
}
#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...