QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469769#2214. Link Cut DigraphBalintRWA 145ms39116kbC++203.2kb2024-07-10 00:33:292024-07-10 00:33:29

Judging History

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

  • [2024-07-10 00:33:29]
  • 评测
  • 测评结果:WA
  • 用时:145ms
  • 内存:39116kb
  • [2024-07-10 00:33:29]
  • 提交

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 << ")";}

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]){
        newSzs.pb(0);
        while(true){
            int n2 = stk.back();
            stk.pop_back();
            lo[n2] = ~gi;
            newSzs[gi] += oldSzs[n2];
            if(n2 == n1) break;
        }
        ans += (ll) newSzs[gi]*(newSzs[gi]-1)/2;
        gi++;
    }
}

void solve(int l, int r, const vector<pair<int, pii>> &edges){
    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);

    vi lSzs, lCompr(SZ(oldSzs));
    FR(i, SZ(oldSzs)){
        if(oldSzs[i] != newSzs[~lo[i]]) lCompr[i] = SZ(lSzs), lSzs.pb(oldSzs[i]);
        else lCompr[i] = -1;
    }

    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]}});
    }

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

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

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

Details

Test #1:

score: 0
Wrong Answer
time: 145ms
memory: 39116kb