QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#272317#2214. Link Cut DigraphZeardoeAC ✓668ms87760kbC++203.8kb2023-12-02 16:52:532023-12-02 16:52:54

Judging History

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

  • [2023-12-02 16:52:54]
  • 评测
  • 测评结果:AC
  • 用时:668ms
  • 内存:87760kb
  • [2023-12-02 16:52:53]
  • 提交

answer

/*
[templates]: 
duipai
compre
addhis
floor_sum
treedfs
matrix
network_flow
polynomial
lca
bitset
valuesgt
fenwick
erbitree
segment
mcmf
primal_dual
*/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
#define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e18;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count()); 
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr  << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
    string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; } 
    reverse(s.begin(), s.end()); cerr << s << endl; 
    return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int N = 250000; 
int u[N+10],v[N+10],fa[N+10],sz[N+10],ans[N+10]; 
int vis[N+10],dfn[N+10],seq[N+10],col[N+10],dcnt,ccnt; 
vector<int> quan,e[N+10],f[N+10]; 
int getfa(int x){
    if(fa[x]==x)return x;
    return fa[x]=getfa(fa[x]);
}
void merge(int x,int y) {
    x=getfa(x),y=getfa(y);
    if(x==y)return;
    fa[x]=y;sz[y]+=sz[x];
} 
void dfs1(int x){
    vis[x]=1;for(int i:e[x])if(!vis[i])dfs1(i);
    dfn[x]=++dcnt;seq[dcnt]=x;
}
void dfs2(int x){
    col[x]=ccnt;
    for(int i:f[x])if(!col[i])dfs2(i);
}
void kosaraju() {
    dcnt=ccnt=0;for(int i:quan)vis[i]=dfn[i]=seq[i]=col[i]=0;
    for(int i:quan)if(!vis[i])dfs1(i);
    for(int i=dcnt;i>=1;i--)if(!col[seq[i]]){
        ++ccnt;dfs2(seq[i]);
    }
}
void solve(int l, int r, vector<int> s) {
    // cerr<<l<<" "<<r<<endl;
    // cerr<<"l="<<l<<",r="<<r<<",s=";for(int i:s)cerr<<i<<" ";
    // cerr<<endl;
    int mid = (l + r) >> 1; quan.clear();
    for(int i:s){
        // cerr<<"i="<<i<<endl;
        if(i>mid)break;
        quan.push_back(getfa(u[i]));quan.push_back(getfa(v[i]));
    }
    sort(quan.begin(),quan.end());
    quan.erase(unique(quan.begin(),quan.end()),quan.end());
    for(int i:quan){e[i].clear();f[i].clear();}
    for(int i:s)if(i<=mid)e[getfa(u[i])].push_back(getfa(v[i])),
        f[getfa(v[i])].push_back(getfa(u[i]));
    // cerr<<l<<" "<<r<<" "<<quan.size()<<endl;
    kosaraju(); 
    if(l==r){
        int delta=0;
        for(int i:s){
            int uu=getfa(u[i]),vv=getfa(v[i]);
            if(uu==vv||col[uu]!=col[vv])continue;
            delta+=sz[uu]*sz[vv];
            merge(uu,vv);
        }
        ans[l]=ans[l-1]+delta;
    }
    else {
        vector<int>sl,sr;
        for(int i:s) {
            if(i>mid)sr.push_back(i);
            else {
                if(col[getfa(u[i])]==col[getfa(v[i])])sl.push_back(i);
                else sr.push_back(i);
            }
        }
        solve(l,mid,sl);solve(mid+1,r,sr);
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen();
    //freopen();
    //time_t start = clock();
    //think twice,code once.
    //think once,debug forever.
    int n, m; cin >> n >> m; 
    // cerr<<"n="<<n<<",m="<<m<<endl;
    f(i, 1, n) fa[i]=i,sz[i]=1;
    f(i, 1, m) {
        cin >> u[i] >> v[i]; 
    }
    vector<int> s; f(i, 1, m) s.push_back(i); 
    solve(1, m, s);
    f(i, 1, m) cout << ans[i] << endl; 
    //time_t finish = clock();
    //cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 668ms
memory: 81752kb

Test #2:

score: 0
Accepted
time: 653ms
memory: 82812kb

Test #3:

score: 0
Accepted
time: 651ms
memory: 85380kb

Test #4:

score: 0
Accepted
time: 619ms
memory: 86028kb

Test #5:

score: 0
Accepted
time: 657ms
memory: 84308kb

Test #6:

score: 0
Accepted
time: 650ms
memory: 85296kb

Test #7:

score: 0
Accepted
time: 656ms
memory: 83012kb

Test #8:

score: 0
Accepted
time: 638ms
memory: 84380kb

Test #9:

score: 0
Accepted
time: 572ms
memory: 87256kb

Test #10:

score: 0
Accepted
time: 645ms
memory: 84384kb

Test #11:

score: 0
Accepted
time: 627ms
memory: 85344kb

Test #12:

score: 0
Accepted
time: 642ms
memory: 82728kb

Test #13:

score: 0
Accepted
time: 566ms
memory: 86784kb

Test #14:

score: 0
Accepted
time: 535ms
memory: 87760kb

Test #15:

score: 0
Accepted
time: 247ms
memory: 57676kb

Test #16:

score: 0
Accepted
time: 549ms
memory: 75612kb

Test #17:

score: 0
Accepted
time: 555ms
memory: 76372kb

Test #18:

score: 0
Accepted
time: 580ms
memory: 77016kb

Test #19:

score: 0
Accepted
time: 640ms
memory: 85616kb

Test #20:

score: 0
Accepted
time: 598ms
memory: 83920kb

Test #21:

score: 0
Accepted
time: 624ms
memory: 83880kb

Test #22:

score: 0
Accepted
time: 634ms
memory: 84124kb

Test #23:

score: 0
Accepted
time: 611ms
memory: 84744kb

Test #24:

score: 0
Accepted
time: 634ms
memory: 83104kb

Test #25:

score: 0
Accepted
time: 601ms
memory: 84164kb

Test #26:

score: 0
Accepted
time: 590ms
memory: 83116kb

Test #27:

score: 0
Accepted
time: 611ms
memory: 81844kb