#include <cstdio>
#include <vector>
using namespace std;
struct disj{
int pa[200005], r, sz[200005];
void init(int n){
for(int i=1; i<=n; i++) pa[i] = i, sz[i] = 1;
}
int find(int x){
if(pa[x] == x) return x;
return pa[x] = find(pa[x]);
}
long long uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
long long ret = 1ll * sz[p] * sz[q];
if(r&1) pa[p] = q, sz[q] += sz[p];
else pa[q] = p, sz[p] += sz[q];
return ret;
}
}disj;
int n,m;
vector<int> g[100005];
int main(){
for (int j=0; j<-1; j++) {
puts("can this shit happen?");
}
scanf("%d %d",&n,&m);
disj.init(n);
for (int i=0; i<m; i++) {
int x,y;
scanf("%d %d",&x,&y);
g[x].push_back(y);
}
long long ret = 0;
for (int i=1; i<=n; i++) {
for (int j=0; j<g[i].size()-1; j++) {
//fprintf(stderr,"%d < %d\n",j,g[i].size()-1);
ret += disj.uni(g[i][j],g[i][j+1]) * 2;
}
}
for (int i=1; i<=n; i++) {
for (int &j : g[i]){
if(disj.find(i) != disj.find(j)) ret++;
}
}
printf("%lld",ret);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
5116 KB |
SIGSEGV Segmentation fault |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
5116 KB |
SIGSEGV Segmentation fault |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6176 KB |
Output is correct |
2 |
Runtime error |
0 ms |
7888 KB |
SIGSEGV Segmentation fault |
3 |
Halted |
0 ms |
0 KB |
- |