QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469780#2214. Link Cut DigraphBalintRAC ✓165ms45716kbC++203.6kb2024-07-10 01:08:022024-07-10 01:08:02

Judging History

你现在查看的是最新测评结果

  • [2024-07-10 01:08:02]
  • 评测
  • 测评结果:AC
  • 用时:165ms
  • 内存:45716kb
  • [2024-07-10 01:08:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cpx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}
template <typename T, typename U>
ostream& operator<<(ostream &os, pair<T, U> p){return os << "(" << p.fs << ", " << p.sn << ")";}

ll tri(int a){
    return (ll) a*(a-1)/2;
}

const int MN = 1e5 + 5;
const int ME = 2.5e5 + 5;
int n, m;
ll ansArr[ME];

vi adjList[MN];
int init[MN], lo[MN], ti, gi;
vi stk, oldSzs, newSzs;
ll ans;

void dfs(int n1){
    stk.pb(n1);
    init[n1] = lo[n1] = ++ti;
    for(int n2 : adjList[n1]){
        if(!init[n2]) dfs(n2);
        if(lo[n2] > 0) lo[n1] = min(lo[n1], lo[n2]);
    }
    if(lo[n1] == init[n1]){
        assert(gi == SZ(newSzs));
        newSzs.pb(0);
        while(true){
            int n2 = stk.back();
            stk.pop_back();
            lo[n2] = ~gi;
            newSzs[gi] += oldSzs[n2];
            if(n2 == n1) break;
        }
        ans += tri(newSzs[gi]);
        gi++;
    }
}

void solve(int l, int r, const vector<pair<int, pii>> &edges, ll offs){
    if(r-l <= 0) return;
    int mid = (l+r)/2;
    for(auto [t, p] : edges){
        if(t > mid) break;
        adjList[p.fs].pb(p.sn);
    }
    FR(src, SZ(oldSzs)) if(!init[src]) dfs(src);

    ll lOffs = 0;
    vi lSzs, lCompr(SZ(oldSzs));
    FR(n1, SZ(oldSzs)){
        if(oldSzs[n1] != newSzs[~lo[n1]]) lCompr[n1] = SZ(lSzs), lSzs.pb(oldSzs[n1]);
        else lCompr[n1] = -1, lOffs += tri(oldSzs[n1]);
    }

    vector<pair<int, pii>> lEdges, rEdges;
    for(auto [t, p] : edges){
        auto [a, b] = p;
        if(lo[a] != lo[b]) rEdges.pb({t, {~lo[a], ~lo[b]}});
        else if(t < mid){
            lEdges.pb({t, {lCompr[a], lCompr[b]}});
            assert(lCompr[a] != -1 && lCompr[b] != -1);
        }
    }

    ansArr[mid] = ans + offs;
    ans = 0;
    FR(i, SZ(oldSzs)) adjList[i].clear(), init[i] = 0;
    ti = gi = 0;
    vi rSzs = move(newSzs);
    newSzs.clear();
    oldSzs.clear();

    /*dbg(mid);
    dbgArr(lSzs, SZ(lSzs));
    dbgArr(lEdges, SZ(lEdges));
    dbgArr(rSzs, SZ(rSzs));
    dbgArr(rEdges, SZ(rEdges));*/

    oldSzs = move(lSzs);
    solve(l, mid, lEdges, offs + lOffs);
    oldSzs = move(rSzs);
    solve(mid+1, r, rEdges, offs);
}

int main(){
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    vector<pair<int, pii>> edges;
    FR(i, m){
        int a, b;
        cin >> a >> b;
        a--; b--;
        if(a != b) edges.pb({i, {a, b}});
    }
    oldSzs.assign(n, 1);
    solve(0, m, edges, 0);
    FR(i, m) cout << ansArr[i] << '\n';
}

Details

Test #1:

score: 100
Accepted
time: 153ms
memory: 38928kb

Test #2:

score: 0
Accepted
time: 151ms
memory: 39384kb

Test #3:

score: 0
Accepted
time: 154ms
memory: 39700kb

Test #4:

score: 0
Accepted
time: 154ms
memory: 43164kb

Test #5:

score: 0
Accepted
time: 149ms
memory: 39248kb

Test #6:

score: 0
Accepted
time: 154ms
memory: 39172kb

Test #7:

score: 0
Accepted
time: 151ms
memory: 38884kb

Test #8:

score: 0
Accepted
time: 162ms
memory: 39136kb

Test #9:

score: 0
Accepted
time: 126ms
memory: 45252kb

Test #10:

score: 0
Accepted
time: 160ms
memory: 39248kb

Test #11:

score: 0
Accepted
time: 148ms
memory: 38892kb

Test #12:

score: 0
Accepted
time: 150ms
memory: 38820kb

Test #13:

score: 0
Accepted
time: 130ms
memory: 45716kb

Test #14:

score: 0
Accepted
time: 121ms
memory: 45672kb

Test #15:

score: 0
Accepted
time: 86ms
memory: 25956kb

Test #16:

score: 0
Accepted
time: 143ms
memory: 31724kb

Test #17:

score: 0
Accepted
time: 138ms
memory: 31476kb

Test #18:

score: 0
Accepted
time: 145ms
memory: 31152kb

Test #19:

score: 0
Accepted
time: 165ms
memory: 38908kb

Test #20:

score: 0
Accepted
time: 165ms
memory: 41528kb

Test #21:

score: 0
Accepted
time: 151ms
memory: 41500kb

Test #22:

score: 0
Accepted
time: 154ms
memory: 41168kb

Test #23:

score: 0
Accepted
time: 155ms
memory: 41968kb

Test #24:

score: 0
Accepted
time: 160ms
memory: 41048kb

Test #25:

score: 0
Accepted
time: 157ms
memory: 40012kb

Test #26:

score: 0
Accepted
time: 156ms
memory: 39708kb

Test #27:

score: 0
Accepted
time: 149ms
memory: 39760kb