Submission #59901

# Submission time Handle Problem Language Result Execution time Memory
59901 2018-07-23T09:32:56 Z istlemin Duathlon (APIO18_duathlon) C++14
0 / 100
768 ms 46796 KB
#include<bits/stdc++.h>

using namespace std;

#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;

vi dfsNum;
vi dfsLow;
ll currDfsNum = 0;
vector<vi> e;

set<pii> bridges;


void dfs(ll v, ll l){
	if(dfsNum[v]!=-1) return;
    dfsNum[v] = dfsLow[v] = currDfsNum++;
    rep(i,0,e[v].size()){
		if(e[v][i]==l) continue;
        dfs(e[v][i],v);
        dfsLow[v] = min(dfsLow[v],dfsLow[e[v][i]]);
    }
}

vector<vi> cycles;
vi cycleIndices;
vector<bool> seen;

void getCycle(ll v){
	if(seen[v]) return;
	seen[v] = true;
	cycles[cycles.size()-1].push_back(v);
	cycleIndices[v] = cycles.size()-1;
    rep(i,0,e[v].size()){
        if(bridges.find({v,e[v][i]})==bridges.end())
			getCycle(e[v][i]);
    }
}

vector<vi> treeE;

vector<vi> dp;

ll getNum(ll v,ll l, ll toPlace){
	//cout<<v<<" "<<toPlace-1<<endl;
	if(dp[v][toPlace-1]!=-1) return dp[v][toPlace-1];
    ll ans = 0;
	if(toPlace==1)
		ans += cycles[v].size();
    ll childSum = 0;
    rep(i,0,treeE[v].size())
        if(treeE[v][i] != l)
			childSum += getNum(treeE[v][i],v,1);

    rep(i,0,treeE[v].size()){
        if(treeE[v][i] == l) continue;
        if(toPlace==1)
			ans += getNum(treeE[v][i],v,1);
        if(toPlace==2){
			ans += getNum(treeE[v][i],v,1)*cycles[v].size();
			ans += getNum(treeE[v][i],v,2);
		}
		if(toPlace==3){
			ll childNum = getNum(treeE[v][i],v,1);
			ans += childNum*(childSum - childNum)*cycles[v].size();
			ans += getNum(treeE[v][i],v,2)*cycles[v].size()*2;
			ans += getNum(treeE[v][i],v,3);
		}
    }
    return dp[v][toPlace-1] = ans;
}

int main(){
	cin.sync_with_stdio(false);
    ll n,m;
    cin>>n>>m;
    e.resize(n);
    dfsNum.resize(n,-1);
    dfsLow.resize(n,-1);
    vector<pii> edges(m);
    rep(i,0,m){
        ll a,b;
        cin>>a>>b;
        --a; --b;
        edges[i] = {a,b};
        e[a].push_back(b);
        e[b].push_back(a);
    }

    rep(i,0,n)
		dfs(i,-1);

	//cout<<endl<<endl;

	rep(i,0,n){
		//cout<<i+1<<": "<<dfsNum[i]<<" "<<dfsLow[i]<<endl;
	}

    rep(i,0,m){
        ll a,b;
        tie(a,b) = edges[i];
        if(dfsNum[a]<dfsNum[b]&&dfsLow[b]==dfsNum[b]){
            bridges.insert({a,b});
            bridges.insert({b,a});
        }
        if(dfsNum[b]<dfsNum[a]&&dfsLow[a]==dfsNum[a]){
            bridges.insert({a,b});
            bridges.insert({b,a});
        }
    }

    cycleIndices.resize(n);
    seen.resize(n);
    rep(i,0,n){
		if(seen[i]) continue;
		cycles.push_back(vi(0));
		getCycle(i);
	}

	treeE.resize(cycles.size());

    trav(bridge,bridges){
        //cout<<bridge.first+1<<" "<<bridge.second+1<<endl;
        if(bridge.first<bridge.second){
			ll a = cycleIndices[bridge.first];
			ll b = cycleIndices[bridge.second];

            treeE[a].push_back(b);
            treeE[b].push_back(a);
        }
    }

    ll ans = 0;

    rep(i,0,cycles.size()){
		ll sz = cycles[i].size();
        ans += (sz)*(sz-1)*(sz-2);
        ans += (sz-1)*(sz-1)*(n-sz)*2;
    }

    dp.resize(cycles.size(),vi(3,-1));

    ans += getNum(0,-1,3);

    cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 205 ms 17000 KB Output is correct
2 Correct 165 ms 18240 KB Output is correct
3 Incorrect 420 ms 31112 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 31112 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 719 ms 45380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 45380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 768 ms 46796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -