QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#457203 | #2214. Link Cut Digraph | hos_lyric | WA | 269ms | 52924kb | C++14 | 4.3kb | 2024-06-29 09:03:41 | 2024-06-29 09:03:42 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
vector<int> uf;
int root(int u) {
return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (uf[u] > uf[v]) swap(u, v);
uf[u] += uf[v];
uf[v] = u;
return true;
}
namespace scc {
int n, l;
vector<vector<int>> graph, hparg;
vector<int> ids;
vector<int> stack;
void init(int n_) {
n = n_;
graph.assign(n, {});
hparg.assign(n, {});
ids.assign(n, -2);
stack.clear();
}
void clear(int u) {
graph[u].clear();
hparg[u].clear();
ids[u] = -2;
}
void ae(int u, int v) {
graph[u].push_back(v);
hparg[v].push_back(u);
}
void dfs0(int u) {
if (ids[u] == -2) {
ids[u] = -1;
for (const int v : graph[u]) dfs0(v);
stack.push_back(u);
}
}
void dfs1(int u) {
if (ids[u] == -1) {
ids[u] = l;
for (const int v : hparg[u]) dfs1(v);
}
}
void run1() {
l = 0;
for (; stack.size(); ) {
const int u = stack.back();
stack.pop_back();
if (ids[u] == -1) {
dfs1(u);
++l;
}
}
}
} // scc
int N, M;
vector<int> A, B;
// connect iss[i][*] when adding edge i
vector<vector<int>> iss;
using Edge = pair<pair<int, int>, int>;
void rec(int l, int r, const vector<Edge> &es) {
// cerr<<"[rec] "<<l<<" "<<r<<" "<<es<<endl;
if (l + 1 == r) {
for (const Edge &e : es) {
iss[l].push_back(e.second);
}
} else {
const int mid = (l + r) / 2;
for (const Edge &e : es) {
const int u = e.first.first, v = e.first.second;
scc::clear(u);
scc::clear(v);
}
for (const Edge &e : es) if (e.second < mid) {
const int u = e.first.first, v = e.first.second;
scc::ae(u, v);
}
for (const Edge &e : es) {
const int u = e.first.first, v = e.first.second;
scc::dfs0(u);
scc::dfs0(v);
}
scc::run1();
vector<Edge> esL, esR;
for (const Edge &e : es) {
const int u = e.first.first, v = e.first.second;
if (scc::ids[u] == scc::ids[v]) {
esL.emplace_back(make_pair(u, v), e.second);
} else {
esR.emplace_back(make_pair(scc::ids[u], scc::ids[v]), e.second);
}
}
rec(l, mid, esL);
rec(mid, r, esR);
}
}
Int c2(Int n) {
return n * (n - 1) / 2;
}
int main() {
for (; ~scanf("%d%d", &N, &M); ) {
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
scc::init(N);
vector<Edge> es(M);
for (int i = 0; i < M; ++i) {
es[i] = Edge(make_pair(A[i], B[i]), i);
}
iss.assign(M, {});
rec(0, M, es);
// cerr<<"iss = "<<iss<<endl;
uf.assign(N, -1);
Int now = 0;
for (int i = 0; i < M; ++i) {
for (const int j : iss[i]) {
const int u = root(A[j]);
const int v = root(B[j]);
if (u != v) {
now -= c2(-uf[u]);
now -= c2(-uf[v]);
connect(u, v);
now += c2(-uf[root(u)]);
}
}
printf("%lld\n", now);
}
}
return 0;
}
Details
Test #1:
score: 0
Wrong Answer
time: 269ms
memory: 52924kb