이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 1e5 + 10;
vector<int> tree[N], g[N], adj[N];
int Time = 0;
int Low[N], Num[N], in[N], out[N], sz[N], up_node[N], depth[N];
vector<int> node;
void dfs(int u, int par) {
Low[u] = Num[u] = ++Time;
in[u] = Time;
sz[u] = 1;
node.pb(u);
for(auto v : g[u]) {
if(v != par) {
if(Num[v]) {
if(minimize(Low[u], Num[v])) up_node[u] = v;
if(Num[v] < Num[u]) adj[v].pb(u);
} else {
tree[u].pb(v);
tree[v].pb(u);
depth[v] = depth[u] + 1;
dfs(v, u);
sz[u] += sz[v];
if(minimize(Low[u], Low[v])) up_node[u] = up_node[v];
}
}
}
out[u] = Time;
}
template <class T> struct FenwickTree {
int n;
vector<T> bit;
FenwickTree(int _n = 0) {
n = _n;
bit.resize(n + 5);
for(int i = 1; i <= n; i++) bit[i] = 0;
}
void update(int pos, T x) {
for(int i = pos; i <= n; i += i & (-i)) bit[i] += x;
}
T get(int pos) {
T ans = 0;
for(int i = pos; i > 0; i -= i & (-i)) ans += bit[i];
return ans;
}
};
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
FenwickTree<int> BIT = FenwickTree<int>(n);
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(!Num[i]) {
depth[1] = 1;
node.clear();
dfs(i, 0);
int c = node.size();
for(auto u : node) {
ans += 1ll * (c - sz[u]) * (sz[u] - 1);
if(up_node[u]) {
ans += 1ll * (c - sz[u]) * (c - sz[u] - 1) / 2;
int v = up_node[u];
ans -= 1ll * (c - sz[v] + 1) * (c - sz[v]) / 2;
}
ans += 1ll * (sz[u] - 1) * (sz[u] - 2) / 2;
sort(all(adj[u]), [&](int x, int y) {
return depth[x] > depth[y];
});
for(auto v : adj[u]) {
if(BIT.get(out[v]) - BIT.get(in[v] - 1) == 0) {
ans -= 1ll * sz[v] * (sz[v] - 1) / 2;
}
BIT.update(in[v], 1);
}
for(auto v : tree[u]) {
if(depth[v] > depth[u] && (BIT.get(out[v]) - BIT.get(in[v] - 1)) == 0) {
ans -= 1ll * sz[v] * (sz[v] - 1) / 2;
}
}
for(auto v : adj[u]) BIT.update(in[v], -1);
}
}
}
cout << ans * 2 << '\n';
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:173:17: warning: unused variable 'ddd' [-Wunused-variable]
173 | int tc = 1, ddd = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |