QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#469774 | #2214. Link Cut Digraph | BalintR | TL | 0ms | 0kb | C++20 | 2.0kb | 2024-07-10 00:54:00 | 2024-07-10 00:54:01 |
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;
int n, m;
vi adjList[MN];
int init[MN], lo[MN], ti;
int stk[MN], si;
ll ans;
void dfs(int n1){
init[n1] = lo[n1] = ++ti;
stk[si++] = n1;
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]){
int sz = 0;
do sz++, lo[stk[--si]] = -1; while(stk[si] != n1);
ans += (ll) sz*(sz-1)/2;
}
}
int main(){
cin.sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
FR(i, m){
int a, b;
cin >> a >> b;
a--; b--;
adjList[a].pb(b);
FR(n1, n) if(!init[n1]) dfs(n1);
cout << ans << '\n';
ans = ti = 0;
FR(n1, n) init[n1] = 0;
}
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded