QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#469770 | #2214. Link Cut Digraph | BalintR | WA | 162ms | 39028kb | C++20 | 3.3kb | 2024-07-10 00:44:03 | 2024-07-10 00:44:03 |
Judging History
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]){
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 += (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(n1, SZ(oldSzs)){
if(oldSzs[n1] != newSzs[~lo[n1]]) lCompr[n1] = SZ(lSzs), lSzs.pb(oldSzs[n1]);
else lCompr[n1] = -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]}});
assert(lCompr[a] != -1 && lCompr[b] != -1);
}
}
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';
}
详细
Test #1:
score: 0
Wrong Answer
time: 162ms
memory: 39028kb