Konrad and Company Evaluation

F. Konrad and Company Evaluation

Reference: [codeforces 1230F] Konrad and Company Evaluation- violence

Ideas: the meaning of problems analysis, see reference blog. Because demand is the number of triplets, so when saving time is saved as a directed graph, so that fewer wage employees point to pay more employees, then seek triple the time, we only need to triplets the middle of the staff can be solved as a reference point.

ll cal(int i)
{
    return (cnt[i]-g[i].size())*g[i].size();
}

Then when operating, because every time will be added nso that it would surely be connected between two points is larger, then for that point of the point to point, the triple consisting of necessary re-calculated , so we first had calculated before subtracting the part, and then add a new section.

Code:

// Created by CAD on 2019/10/1.
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=1e6+5;
int cnt[maxn];
vector<int> g[maxn];
ll cal(int i)
{
    return (cnt[i]-g[i].size())*g[i].size();
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;    cin>>n>>m;
    for(int i=1,u,v;i<=m;++i)
    {
        cin>>u>>v;
        if(u>v) swap(u,v);
        g[u].push_back(v);
        cnt[u]++,cnt[v]++;
    }
    ll ans=0;
    for(int i=1;i<=n;++i)   ans+=cal(i);
    int q;  cin>>q;
    cout<<ans<<endl;
    while(q--)
    {
        int x;  cin>>x;
        ans-=cal(x);
        for(auto i:g[x])
        {
            ans-=cal(i);
            g[i].push_back(x);
            ans+=cal(i);
            g[x].clear();
        }
        cout<<ans<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/CADCADCAD/p/11615492.html