QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#555801#2214. Link Cut DigraphetohariTL 0ms0kbC++201.3kb2024-09-10 10:06:492024-09-10 10:06:50

Judging History

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

  • [2024-09-10 10:06:50]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-10 10:06:49]
  • 提交

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