QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#555801 | #2214. Link Cut Digraph | etohari | TL | 0ms | 0kb | C++20 | 1.3kb | 2024-09-10 10:06:49 | 2024-09-10 10:06:50 |
answer
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> v[100001], st;
int num[100001], low[100001], chk[100001], cnt;
ll ans;
inline ll nC2(int x) {
return 1LL * x * (x - 1) / 2;
}
void dfs(int n) {
chk[n] = 1;
st.push_back(n);
num[n] = ++cnt;
low[n] = cnt;
for (int next : v[n]) {
if (num[next] == 0) {
dfs(next);
low[n] = min(low[n], low[next]);
} else if (chk[next])
low[n] = min(low[n], num[next]);
}
if (num[n] == low[n]) {
int t = 0; // 해당 SCC에 속하는 정점의 개수
while (!st.empty()) {
t++;
int x = st.back();
st.pop_back();
chk[x] = 0;
if (n == x)
break;
}
ans += nC2(t);
}
}
int main() {
#ifdef LOCAL
#define task "a"
#else
#define task ""
#endif
if (fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".ans", "w", stdout);
}
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
ans = cnt = 0; // 초기화
for (int j = 1; j <= n; j++) {
num[j] = low[j] = 0; // 초기화
}
for (int j = 1; j <= n; j++) {
if (!num[j]) dfs(j);
}
printf("%lld\n", ans);
}
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded